31 lines
829 B
Elixir
31 lines
829 B
Elixir
defmodule FeedParser do
|
|
@default_parsers [
|
|
FeedParser.RSS2.Parser,
|
|
FeedParser.Atom.Parser,
|
|
FeedParser.JSONFeed.Parser,
|
|
FeedParser.RSSInJSON.Parser
|
|
]
|
|
|
|
@spec parse(data :: String.t(), content_type :: String.t(), parsers :: [module()]) ::
|
|
{:ok, feed :: FeedParser.Feed.t()} | {:error, reason :: String.t()}
|
|
def parse(data, content_type, parsers \\ @default_parsers) when is_binary(data) do
|
|
parsers
|
|
|> Enum.reduce_while(false, fn parser, acc ->
|
|
case parser.accepts(data, content_type) do
|
|
{true, result} ->
|
|
{:halt, {parser, result}}
|
|
|
|
_ ->
|
|
{:cont, acc}
|
|
end
|
|
end)
|
|
|> case do
|
|
{parser, result} ->
|
|
parser.parse_feed(result)
|
|
|
|
false ->
|
|
{:error, "no parser matched the given content type and data"}
|
|
end
|
|
end
|
|
end
|