restbooks 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/restbooks.rb +3 -3
- data/lib/restbooks/authors.rb +9 -9
- data/lib/restbooks/mixins/api.rb +16 -21
- data/lib/restbooks/mixins/model.rb +4 -4
- data/lib/restbooks/models/author.rb +7 -7
- data/lib/restbooks/models/book.rb +8 -8
- data/lib/restbooks/models/category.rb +5 -5
- data/lib/restbooks/models/comment.rb +7 -7
- data/lib/restbooks/models/list.rb +6 -6
- metadata +33 -9
data/lib/restbooks.rb
CHANGED
@@ -23,11 +23,11 @@
|
|
23
23
|
|
24
24
|
# Gems
|
25
25
|
require 'rubygems'
|
26
|
-
require '
|
27
|
-
|
26
|
+
require 'restclient'
|
27
|
+
require 'nokogiri'
|
28
|
+
require 'json'
|
28
29
|
|
29
30
|
# StdLib
|
30
|
-
require 'rexml/document'
|
31
31
|
require 'cgi'
|
32
32
|
require 'digest/md5'
|
33
33
|
require 'date'
|
data/lib/restbooks/authors.rb
CHANGED
@@ -34,9 +34,9 @@ module RestBooks
|
|
34
34
|
# * death: author who dies in ...
|
35
35
|
# * lang: language of the author
|
36
36
|
# * order: normally from A to Z, use desc to reverse order
|
37
|
-
def all(
|
38
|
-
create_url(
|
39
|
-
return do_request(
|
37
|
+
def all(options={})
|
38
|
+
create_url("/authors", options, ORDERED_AUTHOR_PARAMETERS)
|
39
|
+
return do_request(:author)
|
40
40
|
end
|
41
41
|
|
42
42
|
# Endpoint: /authors/top
|
@@ -48,9 +48,9 @@ module RestBooks
|
|
48
48
|
# * death: author who dies in ...
|
49
49
|
# * lang: language of the author
|
50
50
|
# * range: normally overall, week will display the most popular for the previous week and month for the last 4 weeks
|
51
|
-
def top(
|
52
|
-
create_url(
|
53
|
-
return do_request(
|
51
|
+
def top(options={})
|
52
|
+
create_url("/authors/top", options, RANGED_AUTHOR_PARAMETERS)
|
53
|
+
return do_request(:author)
|
54
54
|
end
|
55
55
|
|
56
56
|
# Endpoint: /authors/recent
|
@@ -61,9 +61,9 @@ module RestBooks
|
|
61
61
|
# * born: authors born in ...
|
62
62
|
# * death: author who dies in ...
|
63
63
|
# * lang: language of the author
|
64
|
-
def recent(
|
65
|
-
create_url(
|
66
|
-
return do_request(
|
64
|
+
def recent(options={})
|
65
|
+
create_url("/authors/recent", options, AUTHOR_PARAMETERS)
|
66
|
+
return do_request(:author)
|
67
67
|
end
|
68
68
|
|
69
69
|
# Endpoint: /author/:id/books
|
data/lib/restbooks/mixins/api.rb
CHANGED
@@ -47,7 +47,7 @@ module RestBooks
|
|
47
47
|
if key == :format
|
48
48
|
format = options.delete(:format)
|
49
49
|
elsif !valid_keys.include?(key)
|
50
|
-
options.delete(
|
50
|
+
options.delete(key)
|
51
51
|
else
|
52
52
|
options_str << "#{key.to_s}=#{value}"
|
53
53
|
end
|
@@ -63,7 +63,7 @@ module RestBooks
|
|
63
63
|
begin
|
64
64
|
data = RestClient.get(@uri.to_s, {'If-None-Match' => etag, 'User-Agent' => @user_agent})
|
65
65
|
etag = data.headers[:etag]
|
66
|
-
|
66
|
+
update_etag(@uri.to_s, etag, data.body)
|
67
67
|
rescue RestClient::NotModified
|
68
68
|
data = saved_data
|
69
69
|
end
|
@@ -83,45 +83,40 @@ module RestBooks
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
def process_atom_responce(
|
86
|
+
def process_atom_responce(type, response)
|
87
87
|
result = []
|
88
|
-
|
89
|
-
|
90
|
-
result << eval( "Models::#{type.to_s.capitalize}.new( element )" )
|
88
|
+
Nokogiri::XML::Document.parse(response).css('entry').each do |element|
|
89
|
+
result << eval("Models::#{type.to_s.capitalize}.new(element)")
|
91
90
|
end
|
92
91
|
return result
|
93
92
|
end
|
94
93
|
|
95
|
-
def get_etag(
|
96
|
-
arr = @etags.assoc(
|
94
|
+
def get_etag(path)
|
95
|
+
arr = @etags.assoc(path)
|
97
96
|
etag = data = nil
|
98
97
|
if arr
|
99
98
|
etag = arr[1]
|
100
99
|
data = arr[2]
|
101
100
|
if arr != @etags.first
|
102
101
|
@etags.delete(arr)
|
103
|
-
@etags.unshift(
|
102
|
+
@etags.unshift(arr)
|
104
103
|
end
|
105
104
|
end
|
106
105
|
return etag, data
|
107
106
|
end
|
108
107
|
|
109
|
-
def save_etag(
|
110
|
-
@etags.unshift(
|
108
|
+
def save_etag(path, etag, data)
|
109
|
+
@etags.unshift([path,etag,data])
|
111
110
|
@etags.pop if @etags.length > 99
|
112
111
|
end
|
113
112
|
|
114
|
-
def update_etag(
|
115
|
-
arr = @etags.assoc(
|
116
|
-
if arr
|
117
|
-
|
118
|
-
@etags[0] = [path,etag,data]
|
119
|
-
else
|
120
|
-
@etags.delete(arr)
|
121
|
-
@etags.unshift( [path,etag,data] )
|
122
|
-
end
|
113
|
+
def update_etag(path, etag, data)
|
114
|
+
arr = @etags.assoc(path)
|
115
|
+
if arr == @etags.first
|
116
|
+
@etags[0] = [path,etag,data]
|
123
117
|
else
|
124
|
-
@etags.
|
118
|
+
@etags.delete(arr)
|
119
|
+
@etags.unshift([path,etag,data])
|
125
120
|
end
|
126
121
|
end
|
127
122
|
|
@@ -32,10 +32,10 @@ module RestBooks
|
|
32
32
|
return @element
|
33
33
|
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
def to_json
|
36
|
+
model = self.class.to_s.split('::').last.downcase.to_sym
|
37
|
+
return JSON.generate( { model => self.to_hash } )
|
38
|
+
end
|
39
39
|
|
40
40
|
end
|
41
41
|
end
|
@@ -25,12 +25,12 @@ module RestBooks
|
|
25
25
|
module Models
|
26
26
|
class Author
|
27
27
|
include RestBooks::Model
|
28
|
-
attr_reader :
|
28
|
+
attr_reader :name, :id, :updated, :born, :death
|
29
29
|
|
30
|
-
def initialize(
|
30
|
+
def initialize(element)
|
31
31
|
if element
|
32
32
|
@element = element
|
33
|
-
@element.
|
33
|
+
@element.xpath('*').each do |tag|
|
34
34
|
case tag.name
|
35
35
|
when 'title'
|
36
36
|
@title = tag.text
|
@@ -40,13 +40,13 @@ module RestBooks
|
|
40
40
|
when 'updated'
|
41
41
|
@updated = Date.parse( tag.text )
|
42
42
|
when 'content'
|
43
|
-
dates = tag.text.strip.split('
|
43
|
+
dates = tag.text.strip.split(' ')
|
44
44
|
@born = dates.first
|
45
45
|
@death = dates.last
|
46
46
|
end
|
47
47
|
end
|
48
48
|
else
|
49
|
-
@element =
|
49
|
+
@element = Nokogiri::XML::Element.new('entry')
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -54,8 +54,8 @@ module RestBooks
|
|
54
54
|
return { :title => @title,
|
55
55
|
:id => @id,
|
56
56
|
:updated => @updated,
|
57
|
-
:born => @
|
58
|
-
:death => @
|
57
|
+
:born => @born,
|
58
|
+
:death => @death }
|
59
59
|
end
|
60
60
|
|
61
61
|
end
|
@@ -27,12 +27,12 @@ module RestBooks
|
|
27
27
|
include RestBooks::Model
|
28
28
|
attr_reader :title, :id, :author, :updated, :language, :date, :subject, :description, :rights
|
29
29
|
|
30
|
-
def initialize(
|
30
|
+
def initialize(element=nil)
|
31
31
|
@updated = Time.now
|
32
32
|
@subject = []
|
33
33
|
if element
|
34
34
|
@element = element
|
35
|
-
@element.
|
35
|
+
@element.xpath('*').each do |tag|
|
36
36
|
case tag.name
|
37
37
|
when 'title'
|
38
38
|
@title = tag.text
|
@@ -40,10 +40,10 @@ module RestBooks
|
|
40
40
|
@id = tag.text.split('/').last
|
41
41
|
@id = @id.split(':').last if @id.split(':').length > 1
|
42
42
|
when 'updated'
|
43
|
-
@updated = Date.parse(
|
43
|
+
@updated = Date.parse(tag.text)
|
44
44
|
when 'author'
|
45
|
-
@author = tag.
|
46
|
-
@author_id = tag.
|
45
|
+
@author = tag.xpath('name').text
|
46
|
+
@author_id = tag.xpath('uri').text.split('/').last
|
47
47
|
when 'language'
|
48
48
|
@language = tag.text
|
49
49
|
when 'date'
|
@@ -51,15 +51,15 @@ module RestBooks
|
|
51
51
|
when 'subject'
|
52
52
|
@subject << tag.text
|
53
53
|
when 'description'
|
54
|
-
@description = tag.text.strip
|
54
|
+
@description = tag.text.strip
|
55
55
|
when 'rights'
|
56
56
|
@rights = tag.text
|
57
57
|
when 'content'
|
58
|
-
@html = tag.text if tag
|
58
|
+
@html = tag.text if tag['type'] == "xhtml"
|
59
59
|
end
|
60
60
|
end
|
61
61
|
else
|
62
|
-
@element =
|
62
|
+
@element = Nokogiri::XML::Element.new('entry')
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
@@ -30,17 +30,17 @@ module RestBooks
|
|
30
30
|
def initialize( element )
|
31
31
|
if element
|
32
32
|
@element = element
|
33
|
-
@element.
|
33
|
+
@element.xpath('*').each do |tag|
|
34
34
|
case tag.name
|
35
35
|
when 'title'
|
36
|
-
@title = tag.text.strip
|
37
|
-
@id = CGI.escape(
|
36
|
+
@title = tag.text.strip
|
37
|
+
@id = CGI.escape(@title)
|
38
38
|
when 'updated'
|
39
|
-
@updated = Date.parse(
|
39
|
+
@updated = Date.parse(tag.text)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
else
|
43
|
-
@element =
|
43
|
+
@element = Nokogiri::XML::Element.new('entry')
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -27,27 +27,27 @@ module RestBooks
|
|
27
27
|
include RestBooks::Model
|
28
28
|
attr_reader :title, :id, :author, :published, :updated, :body
|
29
29
|
|
30
|
-
def initialize(
|
30
|
+
def initialize(element)
|
31
31
|
if element
|
32
32
|
@element = element
|
33
|
-
@element.
|
33
|
+
@element.xpath('*').each do |tag|
|
34
34
|
case tag.name
|
35
35
|
when 'title'
|
36
36
|
@title = tag.text
|
37
37
|
when 'id'
|
38
38
|
@id = tag.text.split('#').last
|
39
39
|
when 'author'
|
40
|
-
@author = tag.
|
40
|
+
@author = tag.xpath('name').text
|
41
41
|
when 'published'
|
42
|
-
@published = Date.parse(
|
42
|
+
@published = Date.parse(tag.text)
|
43
43
|
when 'updated'
|
44
|
-
@updated = Date.parse(
|
44
|
+
@updated = Date.parse(tag.text)
|
45
45
|
when 'content'
|
46
|
-
@body = tag.text.strip
|
46
|
+
@body = tag.text.strip
|
47
47
|
end
|
48
48
|
end
|
49
49
|
else
|
50
|
-
@element =
|
50
|
+
@element = Nokogiri::XML::Element.new('entry')
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -27,24 +27,24 @@ module RestBooks
|
|
27
27
|
include RestBooks::Model
|
28
28
|
attr_reader :title, :id, :updated, :description
|
29
29
|
|
30
|
-
def initialize(
|
30
|
+
def initialize(element)
|
31
31
|
if element
|
32
32
|
@element = element
|
33
|
-
@element.
|
33
|
+
@element.xpath('*').each do |tag|
|
34
34
|
case tag.name
|
35
35
|
when 'title'
|
36
|
-
@title = tag.text.strip
|
36
|
+
@title = tag.text.strip
|
37
37
|
when 'id'
|
38
38
|
@id = tag.text.split('/').last
|
39
39
|
@id = @id.split(':').last if @id.split(':').length > 1
|
40
40
|
when 'updated'
|
41
|
-
@updated = Date.parse(
|
41
|
+
@updated = Date.parse(tag.text)
|
42
42
|
when 'content'
|
43
|
-
@description = tag.text.strip
|
43
|
+
@description = tag.text.strip
|
44
44
|
end
|
45
45
|
end
|
46
46
|
else
|
47
|
-
@element =
|
47
|
+
@element = Nokogiri::XML::Element.new('entry')
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Paul Chavard
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-12 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: rest-client
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
@@ -29,9 +29,33 @@ dependencies:
|
|
29
29
|
- 4
|
30
30
|
- 1
|
31
31
|
version: 1.4.1
|
32
|
-
type: :
|
32
|
+
type: :runtime
|
33
33
|
version_requirements: *id001
|
34
|
-
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: nokogiri
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: json
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
type: :development
|
57
|
+
version_requirements: *id003
|
58
|
+
description: Restbooks is a client library for feedbooks.com api
|
35
59
|
email: paul@chavard.net
|
36
60
|
executables: []
|
37
61
|
|
@@ -42,12 +66,12 @@ extra_rdoc_files: []
|
|
42
66
|
files:
|
43
67
|
- README
|
44
68
|
- LICENSE
|
69
|
+
- lib/uri/uri.rb
|
45
70
|
- lib/restbooks.rb
|
46
71
|
- lib/restbooks/books.rb
|
47
72
|
- lib/restbooks/authors.rb
|
48
73
|
- lib/restbooks/categories.rb
|
49
74
|
- lib/restbooks/lists.rb
|
50
|
-
- lib/uri/uri.rb
|
51
75
|
- lib/restbooks/mixins/api.rb
|
52
76
|
- lib/restbooks/mixins/model.rb
|
53
77
|
- lib/restbooks/models/author.rb
|
@@ -84,6 +108,6 @@ rubyforge_project:
|
|
84
108
|
rubygems_version: 1.3.6
|
85
109
|
signing_key:
|
86
110
|
specification_version: 3
|
87
|
-
summary: Restbooks is a client library for feedbooks.com api
|
111
|
+
summary: Restbooks is a client library for feedbooks.com api
|
88
112
|
test_files: []
|
89
113
|
|