Skip to content

Commit

Permalink
abstracting links
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaben committed Aug 25, 2010
1 parent 48ec63b commit d9ee8a2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
40 changes: 36 additions & 4 deletions lib/opds/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ class Entry
attr_reader :raw_doc
attr_reader :title
attr_reader :id
attr_reader :updated_at
attr_reader :updated
attr_reader :published
attr_reader :summary
attr_reader :authors
attr_reader :links
attr_reader :dcmetas
attr_reader :categories
attr_reader :content
attr_reader :rights
attr_reader :subtitle

def self.from_nokogiri(content,namespaces=nil)
z=self.new
z.instance_variable_set('@raw_doc',content)
Expand All @@ -26,7 +32,9 @@ def serialize!
@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?
@updated=DateTime.parse(d) unless d.nil?
d=text(raw_doc.at('./xmlns:published',@namespaces))
@published=DateTime.parse(d) unless d.nil?

@authors=raw_doc.xpath('./xmlns:author',@namespaces).collect do |auth|
{
Expand Down Expand Up @@ -59,6 +67,23 @@ def serialize!
end
end

@categories=raw_doc.xpath('./xmlns:category',@namespaces).collect do |n|
[text(n.attributes['label']),text(n.attributes['term'])]
end

@content=raw_doc.at('./xmlns:content',@namespaces).to_s

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

@rights=text(raw_doc.at('./xmlns:rights',@namespaces))
@subtitle=text(raw_doc.at('./xmlns:rights',@namespaces))

end


Expand All @@ -73,11 +98,18 @@ def partial?
end

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

def acquisition_links
rel_start='http://opds-spec.org/acquisition'
[*links.by(:rel).reject do |k,_|
k[0,rel_start.size]!=rel_start unless k.nil?
end.values]
end

protected
def text(t)
return t.text unless t.nil?
Expand Down
16 changes: 14 additions & 2 deletions lib/opds/support/linkset.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
module OPDS
module Support
class Link < Array
attr_accessor :browser

def navigate
Feed.parse_url(self[1],browser)
end
end


class LinkSet
include Enumerable
def initialize
def initialize(browser=OPDS::Support::Browser.new)
@browser=browser
@rel_store=Hash.new
@txt_store=Hash.new
@lnk_store=Hash.new
Expand All @@ -11,7 +21,9 @@ def initialize
end

def []=(k,v)
@store.push [k]+v
link=Link.new([k]+v)
link.browser=@browser
@store.push link
i=@store.size-1
@rel_store[k]=[] unless @rel_store[k]
@rel_store[k].push i
Expand Down
7 changes: 6 additions & 1 deletion spec/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def sample(type)
end

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

it "should have an author "do
Expand All @@ -40,6 +40,11 @@ def sample(type)
subject.dcmetas.size.should_not be(0)
end

it "should have categories" do

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

it "should not be partial" do
subject.should_not be_partial()
end
Expand Down

0 comments on commit d9ee8a2

Please sign in to comment.