Compare commits

..

No commits in common. "a36dcc9535d701cf2d98848e5692cbc9a53c23bf" and "4b404c1de1e976964500e4966230ca6fedf08a13" have entirely different histories.

4 changed files with 17 additions and 41 deletions

View File

@ -23,13 +23,7 @@ defmodule FeedParser do
"""
@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)
def parse(nil, _, _) do
{:error, "no data"}
end
def parse(data, content_type, parsers) when is_binary(data) do
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

View File

@ -12,22 +12,15 @@ defmodule FeedParser.Parser.Atom do
def accepts(data, content_type) do
case content_type do
"application/atom+xml" ->
case XML.parse(data) do
{:error, _} -> false
{:ok, doc} -> {true, doc}
end
{true, XML.parse(data)}
_ when content_type in ["text/xml", "application/xml"] ->
case XML.parse(data) do
{:error, _} ->
false
doc = XML.parse(data)
{:ok, doc} ->
if XML.xmlElement(doc, :name) == :feed do
{true, doc}
else
false
end
if XML.xmlElement(doc, :name) == :feed do
{true, doc}
else
false
end
_ ->

View File

@ -12,22 +12,15 @@ defmodule FeedParser.Parser.RSS2 do
def accepts(data, content_type) do
cond do
content_type in ["application/rss+xml", "text/rss+xml"] ->
case XML.parse(data) do
{:error, _} -> false
{:ok, doc} -> {true, doc}
end
{true, XML.parse(data)}
content_type in ["text/xml", "application/xml"] ->
case XML.parse(data) do
{:error, _} ->
false
doc = XML.parse(data)
{:ok, doc} ->
if XML.xmlElement(doc, :name) == :rss do
{true, doc}
else
false
end
if XML.xmlElement(doc, :name) == :rss do
{true, doc}
else
false
end
true ->

View File

@ -9,16 +9,12 @@ defmodule FeedParser.XML do
defrecord :xmlAttribute, extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
defrecord :xmlText, extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
@spec parse(data :: String.t()) :: {:ok, tuple()} | {:error, String.t()}
@spec parse(data :: String.t()) :: tuple()
def parse(data) do
{doc, _} =
try do
data
|> :binary.bin_to_list()
|> :xmerl_scan.string()
catch
:exit, reason -> {:error, "parsing XML failed: #{inspect(reason)}"}
end
data
|> :binary.bin_to_list()
|> :xmerl_scan.string()
doc
end