Skip to content

Commit

Permalink
add category item
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayner Pupo committed Jun 6, 2017
1 parent d324808 commit 40d5598
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
22 changes: 20 additions & 2 deletions Text/Feed/Atom.hs
Expand Up @@ -21,7 +21,8 @@

module Text.Feed.Atom
( -- * Types
Feed (..)
Category (..)
, Feed (..)
, Person (..)
, TypeAttribute (..)
-- * Feed rendering
Expand Down Expand Up @@ -51,13 +52,15 @@ data Feed = Feed
-- ^ The feed title type (Text, Html or XHtml)
, feedAuthors :: ![Person]
-- The feed authors
, feedCategories :: ![Category]
} deriving (Eq, Ord, Show, Read)

instance ToJSON Feed where
toJSON Feed {..} = object
[ "title" .= feedTitle
, "titleType" .= feedTitleType
, "author" .= feedAuthors ]
, "author" .= feedAuthors
, "category" .= feedCategories ]

-- | An enumeration for the Type attribute on Text constructs

Expand Down Expand Up @@ -85,6 +88,21 @@ instance ToJSON Person where
, "uri" .= personUri
]

-- | The category of the element

data Category = Category
{ categoryTerm :: !Text
, categoryScheme :: !(Maybe Text)
, categoryLabel :: !(Maybe Text)
} deriving (Eq, Ord, Show, Read)

instance ToJSON Category where
toJSON Category {..} = object
[ "term" .= categoryTerm
, "scheme" .= categoryScheme
, "label" .= categoryLabel
]

-- | Render a 'Feed' as a lazy 'TL.Text'.

renderFeed :: Feed -> TL.Text
Expand Down
11 changes: 11 additions & 0 deletions templates/atom.mustache
Expand Up @@ -12,4 +12,15 @@
{{/ uri }}
</author>
{{/ author }}
{{# category }}
<category>
<term>{{ term }}</term>
{{# scheme }}
<scheme>{{ . }}</scheme>
{{/ scheme }}
{{# label }}
<label>{{ . }}</label>
{{/ label }}
</category>
{{/ category }}
</feed>
12 changes: 10 additions & 2 deletions tests/Text/Feed/AtomSpec.hs
Expand Up @@ -13,11 +13,10 @@ import qualified Text.Feed.Atom as Atom
import Debug.Trace

spec :: Spec
spec = do
spec =
describe "Atom feed" $ do
it "creates an Atom feed" $ do
let atomFeed = testingAtomFeed

traceShowId(Atom.renderFeed atomFeed) == "fixme"

testingAtomFeed :: Atom.Feed
Expand All @@ -26,6 +25,7 @@ testingAtomFeed =
{ Atom.feedTitle = "Test title"
, Atom.feedTitleType = Just Atom.TextType
, Atom.feedAuthors = [testingAuthor "-1", testingAuthor "-2"]
, Atom.feedCategories = [testingCategory "-a", testingCategory "-b"]
}

testingAuthor :: Text -> Atom.Person
Expand All @@ -34,4 +34,12 @@ testingAuthor suffix =
{ Atom.personName = "Some Guy" <> suffix
, Atom.personEmail = Just ("someguy@some.guy" <> suffix)
, Atom.personUri = Just ("some.guy" <> suffix)
}

testingCategory :: Text -> Atom.Category
testingCategory suffix =
Atom.Category
{ Atom.categoryTerm = "catTerm" <> suffix
, Atom.categoryScheme = Just ("catScheme" <> suffix)
, Atom.categoryLabel = Just ("catLabel" <> suffix)
}

0 comments on commit 40d5598

Please sign in to comment.