1 module Cryptsy
.API
.Public
.OrderBook
2 ( GOrderBook
(..), OrderBook
3 , withComponents
, withOrder
4 , module Cryptsy
.API
.Public
.Order
5 -- |Re-export from Cyptsy.API.Public
7 -- |Re-exported from Data.Aeson
8 , FromJSON
(..), Value
, Object
9 -- |Re-export from Data.Text
11 -- |Re-export from Data.Vector
17 import Data
.Aeson
(FromJSON
(..), Value
, Object
, withObject
, (.:))
18 import Data
.Aeson
.Types
(Parser
)
21 import Control
.Applicative
((<*>))
22 import Control
.Monad
((>>=))
23 import Data
.Function
(($))
24 import Data
.Functor
((<$>))
25 import Data
.Traversable
(mapM)
27 import Text
.Show (Show)
30 import Data
.Text
(Text
, pack
)
33 import Data
.Vector
(Vector
)
36 import Cryptsy
.API
.Public
(CryptsyNum
(..), withNullableArray
)
37 import Cryptsy
.API
.Public
.Order
(GOrder
(Order
), Order
)
39 -- |general order book
40 data GOrderBook p q t
= OrderBook
45 , secondaryname
:: Text
46 , secondarycode
:: Text
47 , sellorders
:: Vector
(GOrder p q t
)
48 , buyorders
:: Vector
(GOrder p q t
)
51 -- |default order book
52 type OrderBook
= GOrderBook CryptsyNum CryptsyNum CryptsyNum
54 instance (FromJSON p
, FromJSON q
, FromJSON t
) =>
55 FromJSON
(GOrderBook p q t
)
57 parseJSON
= withObject
"OrderBook" $ withComponents parseJSON
59 -- |Build parser for orderbook from parser for a single order.
60 withComponents
:: (Value
-> Parser
(GOrder p q t
)) -- ^ order parser
61 -> Object
-> Parser
(GOrderBook p q t
)
62 withComponents parseOrder o
=
64 o
.: pack
"marketid" <*>
66 o
.: pack
"primaryname" <*>
67 o
.: pack
"primarycode" <*>
68 o
.: pack
"secondaryname" <*>
69 o
.: pack
"secondarycode" <*>
70 (o
.: pack
"sellorders" >>= parseSellOrders
) <*>
71 (o
.: pack
"buyorders" >>= parseBuyOrders
)
73 parseOrders
= mapM parseOrder
74 parseSellOrders
= withNullableArray
"sellorders" $ parseOrders
75 parseBuyOrders
= withNullableArray
"buyorders" $ parseOrders
77 -- |Build parser for orderbook from parser for a single order object.
78 withOrder
:: (Object
-> Parser
(GOrder p q t
)) -- ^ order parser
79 -> Object
-> Parser
(GOrderBook p q t
)
80 withOrder parseOrder
= withComponents
(withObject
"Order" parseOrder
)