Class: OPDS::Entry

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/opds/entry.rb

Overview

Represents a catalog entry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, log

Constructor Details

#initialize(browser = OPDS::Support::Browser.new) ⇒ Entry

Returns a new instance of Entry.

Parameters:

  • browser (OPDS::Support::Browser) (defaults to: OPDS::Support::Browser.new)

    an optional compatible browser to use



39
40
41
# File 'lib/opds/entry.rb', line 39

def initialize(browser=OPDS::Support::Browser.new)
	@browser=browser
end

Instance Attribute Details

#authorsArray (readonly)

Returns entry parsed authors.

Returns:

  • (Array)

    entry parsed authors



21
22
23
# File 'lib/opds/entry.rb', line 21

def authors
  @authors
end

#categoriesArray (readonly)

Returns Categories found.

Returns:

  • (Array)

    Categories found



30
31
32
# File 'lib/opds/entry.rb', line 30

def categories
  @categories
end

#contentString (readonly)

Returns content found.

Returns:

  • (String)

    content found



32
33
34
# File 'lib/opds/entry.rb', line 32

def content
  @content
end

#contributorsArray (readonly)

Returns entry parsed contributors.

Returns:

  • (Array)

    entry parsed contributors



23
24
25
# File 'lib/opds/entry.rb', line 23

def contributors
  @contributors
end

#dcmetasHash (readonly)

Returns Hash of found dublin core metadata found in the entry.

Returns:

  • (Hash)

    Hash of found dublin core metadata found in the entry

See Also:



28
29
30
# File 'lib/opds/entry.rb', line 28

def dcmetas
  @dcmetas
end

#idString (readonly)

Returns entry id.

Returns:

  • (String)

    entry id



13
14
15
# File 'lib/opds/entry.rb', line 13

def id
  @id
end

Returns Set of links found in the entry.

Returns:



25
26
27
# File 'lib/opds/entry.rb', line 25

def links
  @links
end

#publishedDate (readonly)

Returns entry published date.

Returns:

  • (Date)

    entry published date



17
18
19
# File 'lib/opds/entry.rb', line 17

def published
  @published
end

#raw_docNokogiri::XML::Document (readonly)

“Raw” Nokogiri document used while parsing. It might useful to access atom foreign markup

Returns:

  • (Nokogiri::XML::Document)

    Parsed document



9
10
11
# File 'lib/opds/entry.rb', line 9

def raw_doc
  @raw_doc
end

#rightsString (readonly)

Returns entry right.

Returns:

  • (String)

    entry right



34
35
36
# File 'lib/opds/entry.rb', line 34

def rights
  @rights
end

#subtitleString (readonly)

Returns entry subtitle.

Returns:

  • (String)

    entry subtitle



36
37
38
# File 'lib/opds/entry.rb', line 36

def subtitle
  @subtitle
end

#summaryString (readonly)

Returns entry summary.

Returns:

  • (String)

    entry summary



19
20
21
# File 'lib/opds/entry.rb', line 19

def summary
  @summary
end

#titleString (readonly)

Returns entry title.

Returns:

  • (String)

    entry title



11
12
13
# File 'lib/opds/entry.rb', line 11

def title
  @title
end

#updatedDate (readonly)

Returns entry updated date.

Returns:

  • (Date)

    entry updated date



15
16
17
# File 'lib/opds/entry.rb', line 15

def updated
  @updated
end

Class Method Details

.from_nokogiri(content, namespaces = nil, browser = OPDS::Support::Browser.new) ⇒ Entry

Create an entry from a nokogiri fragment

Parameters:

  • content (Nokogiri::XML::Element)

    Nokogiri fragment (should be <entry>)

  • namespaces (defaults to: nil)

    Associated document namespaces

  • browser (OPDS::Support::Browser) (defaults to: OPDS::Support::Browser.new)

    an optional compatible browser to use

Returns:



48
49
50
51
52
53
54
# File 'lib/opds/entry.rb', line 48

def self.from_nokogiri(content,namespaces=nil, browser=OPDS::Support::Browser.new)
	z=self.new browser
	z.instance_variable_set('@raw_doc',content)
	z.instance_variable_set('@namespaces',namespaces)
	z.serialize!
	z
end

Instance Method Details

Returns acquisition link subset.

Returns:

  • (Array)

    acquisition link subset



155
156
157
158
159
160
161
162
163
164
# File 'lib/opds/entry.rb', line 155

def acquisition_links
	acq=[]
	rel_start='http://opds-spec.org/acquisition'
	links.by(:rel).each do |k,lnk|
		if !k.nil? && k[0,rel_start.size]==rel_start 
			lnk.each {|l| acq.push l}
		end
	end
	acq
end

#authorHash

First Author

Returns:

  • (Hash)


134
135
136
# File 'lib/opds/entry.rb', line 134

def author
	authors.first
end

#complete_urlString

TODO:

accessor to the complete entry

Returns URL to the complete entry.

Returns:

  • (String)

    URL to the complete entry



148
149
150
151
152
# File 'lib/opds/entry.rb', line 148

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

#inspectObject



166
167
168
# File 'lib/opds/entry.rb', line 166

def inspect
	"#<#{self.class}:0x#{self.object_id.abs.to_s(16)} #{instance_variables.reject{|e| e=='@raw_doc' }.collect{|e| "#{e}=#{instance_variable_get(e).inspect}"}.join(' ')} >"
end

#partial?boolean

Is it a partial atom entry ?

Returns:

  • (boolean)


140
141
142
143
144
# File 'lib/opds/entry.rb', line 140

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

#serialize!Object

TODO:

really make private

Read the provided document into the entry struct



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/opds/entry.rb', line 59

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=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|
		{
			: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 @browser
	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?
		price=nil
		currency=nil
		@namespaces['opds']||='http://opds-spec.org/2010/catalog'
		types=n.search('.//opds:indirectAcquisition',@namespaces).map{|b| b['type']}
		type=[type,types].flatten.compact unless types.nil? || types.empty?
		oprice=n.at('./opds:price',@namespaces)
		if oprice
			price=text(oprice)
			currency=oprice.attributes['currencycode'].value unless oprice.attributes['currencycode'].nil?
		end

		unless n.attributes['rel'].nil?
			n.attributes['rel'].value.split.each do |rel|
				@links.push(rel,link,text,type,price,currency)
			end
		else
			@links.push(nil,link,text,type,price,currency)
		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

	@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