module Prosper.Listing
( Listing(..)
, Offer(..)
, Credit(..)
, Rating(..)
, Category(..)
, Status(..)
) where
import Control.Applicative (pure, (<$>), (<*>))
import Control.Monad (mzero)
import Data.Aeson
import Data.Text.Read as R
import Data.Typeable
import GHC.Generics
import Prosper.Money
data Listing = Listing
{ listingId :: !Int
, status :: !Status
, rating :: !Rating
, score :: !(Maybe Int)
, category :: !Category
, amountRemaining :: !Money
, offer :: !Offer
, credit :: !Credit
} deriving Show
instance Eq Listing where
(Listing { listingId = id1 }) == (Listing { listingId = id2 }) =
id1 == id2
data Offer = Offer
{ requestAmount :: !Money
, rate :: !Double
, termInMonths :: !Int
, yield :: !Double
, effectiveYield :: !Double
, apr :: !Double
} deriving Show
data Credit = Credit
{ fico :: !Int
, bankcardUtilization :: !Double
, isHomeowner :: !Bool
, debtToIncome :: !Double
, monthsEmployed :: !(Maybe Int)
, currentDelinquencies :: !(Maybe Int)
, amountDelinquent :: !(Maybe Money)
, openCreditLines :: !(Maybe Int)
, totOpenRevolvingAccts :: !(Maybe Int)
, revolvingBalance :: !(Maybe Money)
, revolvingAvailableCredit :: !(Maybe Int)
, incomeRange :: !(Maybe (Money, Money))
, statedMonthlyIncome :: !(Maybe Money)
, currentCreditLines :: !(Maybe Int)
, nowDelinquentDerog :: !(Maybe Int)
, wasDelinquentDerog :: !(Maybe Int)
} deriving Show
instance FromJSON Listing where
parseJSON (Object v) = Listing
<$> v .: "ListingNumber"
<*> v .: "ListingStatus"
<*> v .: "ProsperRating"
<*> v .:? "ProsperScore"
<*> v .: "ListingCategory"
<*> v .: "AmountRemaining"
<*> (Offer
<$> v .: "ListingRequestAmount"
<*> v .: "BorrowerRate"
<*> v .: "ListingTerm"
<*> v .: "LenderYield"
<*> v .: "EffectiveYield"
<*> v .: "BorrowerAPR")
<*> (Credit
<$> (unFico <$> v .: "FICOScore")
<*> v .: "BankcardUtilization"
<*> v .: "IsHomeowner"
<*> v .: "DTIwProsperLoan"
<*> v .:? "MonthsEmployed"
<*> v .:? "CurrentDelinquencies"
<*> v .:? "AmountDelinquent"
<*> v .:? "OpenCreditLines"
<*> v .:? "TotalOpenRevolvingAccounts"
<*> v .:? "RevolvingBalance"
<*> v .:? "RevolvingAvailablePercent"
<*> (unIR <$> v .: "IncomeRange")
<*> v .:? "StatedMonthlyIncome"
<*> v .:? "CurrentCreditLines"
<*> v .:? "NowDelinquentDerog"
<*> v .:? "WasDelinquentDerog")
parseJSON _ = mzero
newtype FICO = FICO { unFico :: Int }
deriving Eq
instance Show FICO where
show (FICO x) = show x
instance FromJSON FICO where
parseJSON (String s) = readInt s
where
readInt x = case R.decimal x of
Right (y,_) -> pure $ FICO y
Left _ -> mzero
parseJSON _ = mzero
newtype IncomeRange = IR { unIR :: Maybe (Money, Money) }
deriving Eq
instance Show IncomeRange where
show (IR (Just (l, h))) = "$" ++ show l ++ "-$" ++ show h
show (IR Nothing) = "Not displayed or unemployed"
instance FromJSON IncomeRange where
parseJSON (Number 0) = pure $ IR Nothing
parseJSON (Number 1) = pure $ IR (Just (0, 0))
parseJSON (Number 2) = pure $ IR (Just (1, 24999))
parseJSON (Number 3) = pure $ IR (Just (25000, 49999))
parseJSON (Number 4) = pure $ IR (Just (50000, 74999))
parseJSON (Number 5) = pure $ IR (Just (75000, 99999))
parseJSON (Number 6) = pure $ IR (Just (100000, 500000))
parseJSON (Number 7) = pure $ IR Nothing
parseJSON _ = pure $ IR Nothing
data Rating
= HR
| E
| D
| C
| B
| A
| AA
deriving (Show, Eq, Ord, Typeable, Generic, Read, Enum)
instance FromJSON Rating where
instance ToJSON Rating where
data Category
= NotAvailable
| DebtConsolidation
| HomeImprovement
| Business
| PersonalLoan
| StudentUse
| Auto
| Other
| BabyAdoptionLoans
| Boat
| CosmeticProcedures
| EngagementRingFinancing
| GreenLoans
| HouseholdExpenses
| LargePurchases
| MedicalDental
| Motorcycle
| RV
| Taxes
| Vacation
| WeddingLoans
deriving (Show, Eq, Typeable, Generic, Read)
instance FromJSON Category where
parseJSON (Number 0) = pure NotAvailable
parseJSON (Number 1) = pure DebtConsolidation
parseJSON (Number 2) = pure HomeImprovement
parseJSON (Number 3) = pure Business
parseJSON (Number 4) = pure PersonalLoan
parseJSON (Number 5) = pure StudentUse
parseJSON (Number 6) = pure Auto
parseJSON (Number 7) = pure Other
parseJSON (Number 8) = pure BabyAdoptionLoans
parseJSON (Number 9) = pure Boat
parseJSON (Number 10) = pure CosmeticProcedures
parseJSON (Number 11) = pure EngagementRingFinancing
parseJSON (Number 12) = pure GreenLoans
parseJSON (Number 13) = pure HouseholdExpenses
parseJSON (Number 14) = pure LargePurchases
parseJSON (Number 15) = pure MedicalDental
parseJSON (Number 16) = pure Motorcycle
parseJSON (Number 17) = pure RV
parseJSON (Number 18) = pure Taxes
parseJSON (Number 19) = pure Vacation
parseJSON (Number 20) = pure WeddingLoans
parseJSON _ = pure Other
data Status
= Active
| Withdrawn
| Expired
| ListingCompleted
| ListingCancelled
| Pending
deriving (Show, Eq, Typeable, Generic)
instance FromJSON Status where
parseJSON (Number 2) = pure Active
parseJSON (Number 4) = pure Withdrawn
parseJSON (Number 5) = pure Expired
parseJSON (Number 6) = pure ListingCompleted
parseJSON (Number 7) = pure ListingCancelled
parseJSON (Number 8) = pure Pending
parseJSON _ = mzero