Skip to content

Allow users to set an expiration time for custom endpoints #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion spec/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mockedTests = do
feedTests :: Spec
feedTests =
describe "rss feed" $ do
it "should make a feed" $ do
it "should make a feed using embedded guest authors" $ do
ctxt <- initFauxRequestNoCache
let wpfeed = WPFeed
"https://myurl.com/feed"
Expand All @@ -62,6 +62,19 @@ feedTests =
(renderFeedContent ctxt)
ft <- toXMLFeed (_wordpress ctxt) wpfeed
ft `shouldBe` "<?xml version='1.0' ?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\">\n <id>https://myurl.com/feed</id>\n <title type=\"text\">My Blog</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <entry>\n <id>https://myurl.com/2014/10/foo-bar/</id>\n <title type=\"html\">&lt;i&gt;Foo&lt;/i&gt; bar</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <published>2014-10-20T07:00:00Z</published>\n <summary type=\"html\">summary</summary>\n <content type=\"html\">This is the title: &lt;i&gt;Foo&lt;/i&gt; bar</content>\n <author>\n <name>Emma Goldman</name>\n </author>\n <link href=\"https://myurl.com/2014/10/foo-bar/\" title=\"&lt;i&gt;Foo&lt;/i&gt; bar\" />\n </entry>\n</feed>\n"
{- it "should make a feed using list of author ids" $ do
ctxt <- initFauxRequestNoCache
let wpfeed = WPFeed
"https://myurl.com/feed"
"My Blog"
Nothing
Nothing
"https://myurl.com"
buildEntryLinks
GuestAuthorsViaReq
(renderFeedContent ctxt)
ft <- toXMLFeed (_wordpress ctxt) wpfeed
ft `shouldBe` "<?xml version='1.0' ?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\">\n <id>https://myurl.com/feed</id>\n <title type=\"text\">My Blog</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <entry>\n <id>https://myurl.com/2014/10/foo-bar/</id>\n <title type=\"html\">&lt;i&gt;Foo&lt;/i&gt; bar</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <published>2014-10-20T07:00:00Z</published>\n <summary type=\"html\">summary</summary>\n <content type=\"html\">This is the title: &lt;i&gt;Foo&lt;/i&gt; bar</content>\n <author>\n <name>Lucy Parsons</name>\n </author>\n <author>\n <name>Emma Goldman</name>\n </author>\n <link href=\"https://myurl.com/2014/10/foo-bar/\" title=\"&lt;i&gt;Foo&lt;/i&gt; bar\" />\n </entry>\n</feed>\n" -}

larcenyFillTests :: Spec
larcenyFillTests = do
Expand Down
8 changes: 4 additions & 4 deletions src/Web/Offset/Cache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ cacheSwitch b k = cacheSet b (formatKey k)
cacheSet :: CacheBehavior -> Text -> Text -> Redis Bool
cacheSet b k v =
case b of
(CacheSeconds n) -> rsetex k n v
CacheForever -> rset k v
CacheSeconds n -> rsetex k n v
CacheForever n -> rset k v
NoCache -> return True

cacheSetAlwaysExpire :: CacheBehavior -> Text -> Text -> Redis Bool
cacheSetAlwaysExpire b k v =
case b of
(CacheSeconds n) -> rsetex k n v
CacheForever -> rsetex k (10 * 60) v -- cache 10 minutes
CacheSeconds n -> rsetex k n v
CacheForever n -> rsetex k n v
NoCache -> return True

wpExpireAggregatesInt :: RunRedis -> IO Bool
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Offset/Cache/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module Web.Offset.Cache.Types where

import Database.Redis (Redis)

data CacheBehavior = NoCache | CacheSeconds Int | CacheForever deriving (Show, Eq)
data CacheBehavior = NoCache | CacheSeconds Int | CacheForever Int deriving (Show, Eq)
type RunRedis = forall a. Redis a -> IO a
18 changes: 17 additions & 1 deletion src/Web/Offset/Feed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data WPFeed =
, wpGetAuthors :: WPAuthorStyle
, wpRenderEntry :: Object -> IO (Maybe T.Text) }

data WPAuthorStyle = GuestAuthors | DefaultAuthor
data WPAuthorStyle = GuestAuthors | GuestAuthorsViaReq | DefaultAuthor

toXMLFeed :: Wordpress b -> WPFeed -> IO T.Text
toXMLFeed wp wpFeed@(WPFeed uri title icon logo _ _ _ _) = do
Expand Down Expand Up @@ -101,6 +101,7 @@ toEntry wp wpFeed entry@WPEntry{..} = do
let baseEntry = makeEntry guid (TextHTML wpEntryTitle) wpEntryUpdated
authors <- case wpGetAuthors wpFeed of
GuestAuthors -> getAuthorsInline wpEntryJSON
GuestAuthorsViaReq -> getAuthorsViaReq wp wpEntryJSON
DefaultAuthor -> getAuthorViaReq wp wpEntryJSON
return $ baseEntry { entryPublished = Just wpEntryPublished
, entrySummary = Just (TextHTML wpEntrySummary)
Expand Down Expand Up @@ -166,6 +167,21 @@ getAuthorViaReq wp v =
Nothing -> return []
Just authorName ->return (maybeToList authorName)

getAuthorsViaReq :: Wordpress b -> Object -> IO [WPPerson]
getAuthorsViaReq wp v =
do let mAuthorId = parseMaybe (\obj -> obj .: "authors") v :: Maybe [Int]
case mAuthorId of
Nothing -> return []
Just authorIds ->
do let authList = map (\id -> ("include[]", tshow id)) authorIds
eRespError <- cachingGetRetry wp (EndpointKey ("wp/v2/authors") authList)
case eRespError of
Left _ -> return []
Right resp ->
case decodeWPResponseBody resp of
Nothing -> return []
Just list -> return list

entryGuid :: T.Text -> Int -> Object -> URI
entryGuid baseURI wpId wpJSON =
unsafeURI $ T.unpack $
Expand Down