Skip to content

Commit

Permalink
Parsing entries
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaben committed Aug 24, 2010
1 parent d852815 commit 48ec63b
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 16 deletions.
84 changes: 82 additions & 2 deletions lib/opds/entry.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,87 @@
module OPDS
class Entry
def self.from_nokogiri(content)
self.new
include Logging
attr_reader :raw_doc
attr_reader :title
attr_reader :id
attr_reader :updated_at
attr_reader :summary
attr_reader :authors
attr_reader :links
attr_reader :dcmetas
def self.from_nokogiri(content,namespaces=nil)
z=self.new
z.instance_variable_set('@raw_doc',content)
z.instance_variable_set('@namespaces',namespaces)
z.serialize!
z
end


def serialize!
@namespaces=raw_doc.root.namespaces if @namespaces.nil?
@authors=[]
@raw_doc=raw_doc.at('./xmlns:entry',@namespaces) if raw_doc.at('./xmlns:entry',@namespaces)
@title=text(raw_doc.at('./xmlns:title',@namespaces))
@id=text(raw_doc.at('./xmlns:id',@namespaces))
@summary=text(raw_doc.at('./xmlns:summary',@namespaces))
d=text(raw_doc.at('./xmlns:updated',@namespaces))
@updated_at=DateTime.parse(d) unless d.nil?

@authors=raw_doc.xpath('./xmlns:author',@namespaces).collect do |auth|
{
:name => text(raw_doc.at('./xmlns:author/xmlns:name',@namespaces)),
:uri => text(raw_doc.at('./xmlns:author/xmlns:uri',@namespaces)),
:email => text(raw_doc.at('./xmlns:author/xmlns:email',@namespaces))
}
end

@links=OPDS::Support::LinkSet.new
raw_doc.xpath('./xmlns:link',@namespaces).each do |n|
text=nil
text=n.attributes['title'].value unless n.attributes['title'].nil?
link=n.attributes['href'].value
type=n.attributes['type'].value unless n.attributes['type'].nil?
unless n.attributes['rel'].nil?
n.attributes['rel'].value.split.each do |rel|
@links.push(rel,link,text,type)
end
else
@links.push(nil,link,text,type)
end
end
@dcmetas=Hash.new
prefs=@namespaces.reject{|_,v| !%W[http://purl.org/dc/terms/ http://purl.org/dc/elements/1.1/].include?v}
prefs.keys.map{|p| p.split(':').last}.each do |pref|
raw_doc.xpath('./'+pref+':*',@namespaces).each do |n|
@dcmetas[n.name]=[] unless @dcmetas[n.name]
@dcmetas[n.name].push [n.text, n]
end
end

end


def author
authors.first
end

def partial?
links.by(:rel)['alternate'].any? do |l|
l[3]=='application/atom+xml'||l[3]=='application/atom+xml;type=entry'
end
end

def complete_url
links.by(:rel)['alternate'].any? do |l|
l[3]=='application/atom+xml'
end unless !partial?
end

protected
def text(t)
return t.text unless t.nil?
t
end
end
end
20 changes: 13 additions & 7 deletions lib/opds/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ def self.from_nokogiri(content)
#read xml entries into entry struct
def serialize!
@entries=raw_doc.xpath('/xmlns:feed/xmlns:entry',raw_doc.root.namespaces).map do |el|
OPDS::Entry.from_nokogiri(el)
OPDS::Entry.from_nokogiri(el,raw_doc.root.namespaces)
end
end

def title
raw_doc.at('/xmlns:feed/xmlns:title',raw_doc.root.namespaces).text
text(raw_doc.at('/xmlns:feed/xmlns:title',raw_doc.root.namespaces))
end

def icon
raw_doc.at('/xmlns:feed/xmlns:icon',raw_doc.root.namespaces).text
text(raw_doc.at('/xmlns:feed/xmlns:icon',raw_doc.root.namespaces))
end

def links
Expand All @@ -81,14 +81,14 @@ def links
end

def id
raw_doc.at('/xmlns:feed/xmlns:id',raw_doc.root.namespaces).text
text(raw_doc.at('/xmlns:feed/xmlns:id',raw_doc.root.namespaces))
end

def author
{
:name => raw_doc.at('/xmlns:feed/xmlns:author/xmlns:name',raw_doc.root.namespaces).text,
:uri => raw_doc.at('/xmlns:feed/xmlns:author/xmlns:uri',raw_doc.root.namespaces).text,
:email => raw_doc.at('/xmlns:feed/xmlns:author/xmlns:email',raw_doc.root.namespaces).text
:name => text(raw_doc.at('/xmlns:feed/xmlns:author/xmlns:name',raw_doc.root.namespaces)),
:uri => text(raw_doc.at('/xmlns:feed/xmlns:author/xmlns:uri',raw_doc.root.namespaces)),
:email => text(raw_doc.at('/xmlns:feed/xmlns:author/xmlns:email',raw_doc.root.namespaces))
}
end

Expand Down Expand Up @@ -121,5 +121,11 @@ def prev_page
Feed.parse_url(prev_page_url,@browser)
end

protected
def text(t)
return t.text unless t.nil?
t
end

end
end
18 changes: 14 additions & 4 deletions lib/opds/support/linkset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def initialize
@rel_store=Hash.new
@txt_store=Hash.new
@lnk_store=Hash.new
@typ_store=Hash.new
@store=[]
end

Expand All @@ -14,10 +15,12 @@ def []=(k,v)
i=@store.size-1
@rel_store[k]=[] unless @rel_store[k]
@rel_store[k].push i
@txt_store[v.last]=[] unless @txt_store[v.last]
@txt_store[v.last].push i
@txt_store[v[1]]=[] unless @txt_store[v[1]]
@txt_store[v[1]].push i
@lnk_store[v.first]=[] unless @lnk_store[v.first]
@lnk_store[v.first].push i
@typ_store[v.last]=[] unless @typ_store[v.last]
@typ_store[v.last].push i

end

Expand All @@ -29,8 +32,8 @@ def each(&block)
@store.each(&block)
end

def push(rel,link,text=nil)
self[rel]=[link,text]
def push(rel,link,text=nil,type=nil)
self[rel]=[link,text,type]
end

def link_url(k)
Expand All @@ -50,6 +53,12 @@ def link_text(k)
t=remap(collection(ty)[v])
t.first[2] unless t.nil?
end

def link_type(k)
ty,v=k.first
t=remap(collection(ty)[v])
t.first[3] unless t.nil?
end

def size
@store.size
Expand Down Expand Up @@ -77,6 +86,7 @@ def collection(type)
when 'link' then @lnk_store
when 'rel' then @rel_store
when 'txt' then @txt_store
when 'type' then @typ_store
end
end

Expand Down
46 changes: 46 additions & 0 deletions spec/entry_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

def sample(type)
File.open( File.expand_path(File.dirname(__FILE__) + "/../samples/#{type}.txt"))
end

describe OPDS::Entry do


subject do
OPDS::Entry.from_nokogiri(Nokogiri::XML(sample(:entry)))
end

it "should have a title "do
subject.title.size.should_not be(0)
end

it "should have an id"do
subject.id.size.should_not be(0)
end

it "should have a summary "do
subject.summary.size.should_not be(0)
end

it "should have a update date "do
subject.updated_at.day.should be(13)
end

it "should have an author "do
subject.author.size.should_not be(0)
end

it "should have links "do
subject.links.size.should_not be(0)
end

it "should have dc:meta" do

subject.dcmetas.size.should_not be(0)
end

it "should not be partial" do
subject.should_not be_partial()
end
end
6 changes: 3 additions & 3 deletions spec/linkset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

describe OPDS::Support::LinkSet do
before(:each) do
subject.push('root','http://feedbooks.com','Racine')
subject.push('subsection','http://feedbooks.com/publicdomain','Domaine pub')
subject.push('subsection','http://feedbooks.com/original','Original')
subject.push('root','http://feedbooks.com','Racine','application/atom+xml')
subject.push('subsection','http://feedbooks.com/publicdomain','Domaine pub','application/atom+xml')
subject.push('subsection','http://feedbooks.com/original','Original','application/atom+xml')
subject.push('subsection','http://feedbooks.com/feed','feeds')
subject.push('http://opds-spec.org/shelf','http://feedbooks.com/shelf','shelf')
subject.push('related','http://feedbooks.com/shelf',nil)
Expand Down
5 changes: 5 additions & 0 deletions spec/opdsparser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def sample(type)
it "should have entries" do
subject.parse(sample(feed_type)).entries.size.should_not be(0)
end



end
Expand All @@ -59,6 +60,10 @@ def sample(type)
subject.parse(sample(:acquisition)).should be_first_page()
end

it "should have partial entries" do
subject.parse(sample(:acquisition)).entries.any?(&:partial?).should be()
end

it do
feed=nil
lambda { feed=subject.parse(sample(:acquisition)).next_page }.should_not raise_error
Expand Down

0 comments on commit 48ec63b

Please sign in to comment.