Compare commits
No commits in common. "a36dcc9535d701cf2d98848e5692cbc9a53c23bf" and "4b404c1de1e976964500e4966230ca6fedf08a13" have entirely different histories.
a36dcc9535
...
4b404c1de1
|
@ -23,13 +23,7 @@ defmodule FeedParser do
|
||||||
"""
|
"""
|
||||||
@spec parse(data :: String.t(), content_type :: String.t(), parsers :: [module()]) ::
|
@spec parse(data :: String.t(), content_type :: String.t(), parsers :: [module()]) ::
|
||||||
{:ok, feed :: FeedParser.Feed.t()} | {:error, reason :: String.t()}
|
{:ok, feed :: FeedParser.Feed.t()} | {:error, reason :: String.t()}
|
||||||
def parse(data, content_type, parsers \\ @default_parsers)
|
def parse(data, content_type, parsers \\ @default_parsers) when is_binary(data) do
|
||||||
|
|
||||||
def parse(nil, _, _) do
|
|
||||||
{:error, "no data"}
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse(data, content_type, parsers) when is_binary(data) do
|
|
||||||
parsers
|
parsers
|
||||||
|> Enum.reduce_while(false, fn parser, acc ->
|
|> Enum.reduce_while(false, fn parser, acc ->
|
||||||
case parser.accepts(data, content_type) do
|
case parser.accepts(data, content_type) do
|
||||||
|
|
|
@ -12,22 +12,15 @@ defmodule FeedParser.Parser.Atom do
|
||||||
def accepts(data, content_type) do
|
def accepts(data, content_type) do
|
||||||
case content_type do
|
case content_type do
|
||||||
"application/atom+xml" ->
|
"application/atom+xml" ->
|
||||||
case XML.parse(data) do
|
{true, XML.parse(data)}
|
||||||
{:error, _} -> false
|
|
||||||
{:ok, doc} -> {true, doc}
|
|
||||||
end
|
|
||||||
|
|
||||||
_ when content_type in ["text/xml", "application/xml"] ->
|
_ when content_type in ["text/xml", "application/xml"] ->
|
||||||
case XML.parse(data) do
|
doc = XML.parse(data)
|
||||||
{:error, _} ->
|
|
||||||
false
|
|
||||||
|
|
||||||
{:ok, doc} ->
|
if XML.xmlElement(doc, :name) == :feed do
|
||||||
if XML.xmlElement(doc, :name) == :feed do
|
{true, doc}
|
||||||
{true, doc}
|
else
|
||||||
else
|
false
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
|
|
|
@ -12,22 +12,15 @@ defmodule FeedParser.Parser.RSS2 do
|
||||||
def accepts(data, content_type) do
|
def accepts(data, content_type) do
|
||||||
cond do
|
cond do
|
||||||
content_type in ["application/rss+xml", "text/rss+xml"] ->
|
content_type in ["application/rss+xml", "text/rss+xml"] ->
|
||||||
case XML.parse(data) do
|
{true, XML.parse(data)}
|
||||||
{:error, _} -> false
|
|
||||||
{:ok, doc} -> {true, doc}
|
|
||||||
end
|
|
||||||
|
|
||||||
content_type in ["text/xml", "application/xml"] ->
|
content_type in ["text/xml", "application/xml"] ->
|
||||||
case XML.parse(data) do
|
doc = XML.parse(data)
|
||||||
{:error, _} ->
|
|
||||||
false
|
|
||||||
|
|
||||||
{:ok, doc} ->
|
if XML.xmlElement(doc, :name) == :rss do
|
||||||
if XML.xmlElement(doc, :name) == :rss do
|
{true, doc}
|
||||||
{true, doc}
|
else
|
||||||
else
|
false
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
|
|
12
lib/xml.ex
12
lib/xml.ex
|
@ -9,16 +9,12 @@ defmodule FeedParser.XML do
|
||||||
defrecord :xmlAttribute, extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
|
defrecord :xmlAttribute, extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
|
||||||
defrecord :xmlText, extract(:xmlText, 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
|
def parse(data) do
|
||||||
{doc, _} =
|
{doc, _} =
|
||||||
try do
|
data
|
||||||
data
|
|> :binary.bin_to_list()
|
||||||
|> :binary.bin_to_list()
|
|> :xmerl_scan.string()
|
||||||
|> :xmerl_scan.string()
|
|
||||||
catch
|
|
||||||
:exit, reason -> {:error, "parsing XML failed: #{inspect(reason)}"}
|
|
||||||
end
|
|
||||||
|
|
||||||
doc
|
doc
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue