Skip to content

Commit

Permalink
Parser extracts encoding from feed
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Jan 17, 2010
1 parent 67f0bd1 commit b6a6f11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/feed_me/feed_parser.rb
Expand Up @@ -25,12 +25,13 @@ def open(file)
# parses the passed feed and identifeis what kind of feed it is
# then returns a parser object
def parse(feed)
if root = Nokogiri::XML(feed).root
document = Nokogiri::XML(feed)
if root = document.root
root.add_namespace_definition('atom', 'http://www.w3.org/2005/Atom')
parsers.each do |parser|
node = root.xpath(parser.root_node).first
if node
return parser.new(node)
return parser.new(node, document.encoding)
end
end
end
Expand All @@ -40,10 +41,13 @@ def parse(feed)

end # class << self

def initialize(xml)
def initialize(xml, encoding)
@xml = xml
@encoding = encoding
end

attr_reader :encoding

end

class AtomFeedParser < FeedParser
Expand Down
10 changes: 10 additions & 0 deletions spec/feed_parser_spec.rb
Expand Up @@ -55,6 +55,16 @@
it_should_behave_like "all parsing methods"
end

describe '#encoding' do
it "should return the encoding for an atom feed" do
@atom.encoding.should == 'UTF-8'
end

it "should return the encoding for an atom feed" do
@rss2.encoding.should == 'iso-8859-1'
end
end

describe '#title' do
it "should be valid for an atom feed" do
@atom.title.should == "Test feed"
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/welformed.rss2
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
<title>Lift Off News</title>
Expand Down

0 comments on commit b6a6f11

Please sign in to comment.