module Web.Stripe.Utils
(
CustomerId(..)
, SubscriptionId(..)
, Amount(..)
, Count(..)
, Currency(..)
, Description(..)
, Offset(..)
, optionalArgs
, fromSeconds
, toSeconds
, stringToByteString
, textToByteString
, showByteString
, module Export
) where
import qualified Codec.Binary.UTF8.String as CodecUtf8
import Control.Monad (liftM)
import qualified Data.ByteString as B
import Data.Maybe (mapMaybe)
import qualified Data.Text as T
import Data.Time.Clock as Export (UTCTime (..))
import Data.Time.Clock.POSIX (posixSecondsToUTCTime,
utcTimeToPOSIXSeconds)
import Data.Time.Format ()
import Data.Aeson as Export (withObject, FromJSON (..), Value (..), (.:), (.:?))
import Control.Applicative as Export ((<$>), (<*>))
showByteString :: Show a => a -> B.ByteString
showByteString = stringToByteString . show
textToByteString :: T.Text -> B.ByteString
textToByteString = B.pack . CodecUtf8.encode . T.unpack
stringToByteString :: String -> B.ByteString
stringToByteString = B.pack . CodecUtf8.encode
newtype CustomerId = CustomerId { unCustomerId :: T.Text } deriving (Show, Eq)
newtype SubscriptionId = SubscriptionId { unSubscriptionId :: T.Text }
deriving (Show, Eq)
newtype Amount = Amount { unAmount :: Int } deriving (Show, Eq)
newtype Count = Count { unCount :: Int } deriving (Show, Eq)
newtype Currency = Currency { unCurrency :: T.Text } deriving (Show, Eq)
newtype Description = Description { unDescription :: T.Text } deriving (Show, Eq)
newtype Offset = Offset { unOffset :: Int } deriving (Show, Eq)
fromSeconds :: Integer -> UTCTime
fromSeconds = posixSecondsToUTCTime . fromInteger
toSeconds :: UTCTime -> Integer
toSeconds = round . utcTimeToPOSIXSeconds
optionalArgs :: [(a, Maybe b)] -> [(a, b)]
optionalArgs = mapMaybe . uncurry $ liftM . (,)