From 95f519df298c98d966c6408cac33cdd57b8e8a03 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 20 Mar 2014 03:19:14 +0400 Subject: Add instance ToJSON TrackerEntry --- src/Network/BitTorrent/Internal/Cache.hs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/Network/BitTorrent/Internal') diff --git a/src/Network/BitTorrent/Internal/Cache.hs b/src/Network/BitTorrent/Internal/Cache.hs index 8c74467a..b77e5e82 100644 --- a/src/Network/BitTorrent/Internal/Cache.hs +++ b/src/Network/BitTorrent/Internal/Cache.hs @@ -32,6 +32,7 @@ module Network.BitTorrent.Internal.Cache ) where import Control.Applicative +import Data.Aeson import Data.Monoid import Data.Default import Data.Time @@ -55,6 +56,19 @@ data Cached a = Cached -- INVARIANT: minUpdateInterval <= updateInterval +-- | TODO exsample +instance ToJSON a => ToJSON (Cached a) where + toJSON Cached {..} + | currentTime < expireTime = object + [ "observed" .= posixSecondsToUTCTime lastUpdated + , "expired" .= posixSecondsToUTCTime expireTime + , "data" .= cachedData + ] + | otherwise = String "cached data expired" + where + expireTime = currentTime + updateInterval + currentTime = unsafePerformIO getPOSIXTime + instance Default (Cached a) where def = mempty -- cgit v1.2.3