module Web.Stripe.Utils
( Amount(..)
, Count(..)
, Currency(..)
, Description(..)
, Offset(..)
, optionalArgs
, UTCTime(..)
, fromSeconds
, toSeconds
, stringToByteString
, textToByteString
, showByteString
) 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 (UTCTime (..))
import Data.Time.Clock.POSIX (posixSecondsToUTCTime,
utcTimeToPOSIXSeconds)
import Data.Time.Format ()
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 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 . (,)