restbooks 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +674 -0
- data/README +39 -0
- data/lib/restbooks.rb +104 -0
- data/lib/restbooks/authors.rb +100 -0
- data/lib/restbooks/books.rb +124 -0
- data/lib/restbooks/categories.rb +68 -0
- data/lib/restbooks/lists.rb +49 -0
- data/lib/restbooks/mixins/api.rb +129 -0
- data/lib/restbooks/mixins/model.rb +41 -0
- data/lib/restbooks/models/author.rb +63 -0
- data/lib/restbooks/models/book.rb +85 -0
- data/lib/restbooks/models/category.rb +55 -0
- data/lib/restbooks/models/comment.rb +65 -0
- data/lib/restbooks/models/list.rb +60 -0
- data/lib/uri/uri.rb +30 -0
- metadata +89 -0
@@ -0,0 +1,129 @@
|
|
1
|
+
#
|
2
|
+
# api.rb
|
3
|
+
# restbooks
|
4
|
+
#
|
5
|
+
# Created by Paul Chavard on 2009-09-12.
|
6
|
+
# Copyright 2009
|
7
|
+
#
|
8
|
+
# This file is part of RestBooks.
|
9
|
+
#
|
10
|
+
# RestBooks is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU General Public License as published by
|
12
|
+
# the Free Software Foundation, either version 3 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# RestBooks is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with RestBooks. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
#
|
23
|
+
|
24
|
+
module RestBooks
|
25
|
+
module Api
|
26
|
+
|
27
|
+
BOOK_PARAMETERS = [ :letter, :year, :lang, :page ]
|
28
|
+
AUTHOR_PARAMETERS = [ :letter, :born, :death, :lang, :page ]
|
29
|
+
LIST_PARAMETERS = [ :order, :page ]
|
30
|
+
ORDERED_BOOK_PARAMETERS = BOOK_PARAMETERS + [ :order ]
|
31
|
+
RANGED_BOOK_PARAMETERS = BOOK_PARAMETERS + [ :range ]
|
32
|
+
ORDERED_AUTHOR_PARAMETERS = AUTHOR_PARAMETERS + [ :order ]
|
33
|
+
RANGED_AUTHOR_PARAMETERS = AUTHOR_PARAMETERS + [ :range ]
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def initialize( client )
|
38
|
+
@user_agent = client.user_agent
|
39
|
+
@uri = client.uri
|
40
|
+
@etags = []
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_url( path, options, valid_keys )
|
44
|
+
format = :atom
|
45
|
+
options_str = []
|
46
|
+
options.each do |key,value|
|
47
|
+
if key == :format
|
48
|
+
format = options.delete(:format)
|
49
|
+
elsif !valid_keys.include?(key)
|
50
|
+
options.delete( key )
|
51
|
+
else
|
52
|
+
options_str << "#{key.to_s}=#{value}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
options = (options_str.length > 0 ? options_str.join('&') : nil)
|
56
|
+
@uri.path = "#{path}.#{format.to_s}"
|
57
|
+
@uri.query = options
|
58
|
+
end
|
59
|
+
|
60
|
+
def do_request(type, format = :atom)
|
61
|
+
etag, saved_data = get_etag(@uri.to_s)
|
62
|
+
if etag
|
63
|
+
begin
|
64
|
+
data = RestClient.get(@uri.to_s, {'If-None-Match' => etag, 'User-Agent' => @user_agent})
|
65
|
+
etag = data.headers[:etag]
|
66
|
+
save_etag(@uri.to_s, etag, data.body)
|
67
|
+
rescue RestClient::NotModified
|
68
|
+
data = saved_data
|
69
|
+
end
|
70
|
+
else
|
71
|
+
data = RestClient.get(@uri.to_s, {'User-Agent' => @user_agent})
|
72
|
+
etag = data.headers[:etag]
|
73
|
+
save_etag(@uri.to_s, etag, data.body)
|
74
|
+
end
|
75
|
+
|
76
|
+
case format
|
77
|
+
when :atom
|
78
|
+
return process_atom_responce(type, data.body)
|
79
|
+
# when :json
|
80
|
+
# #return JSON.parse( data )
|
81
|
+
else
|
82
|
+
return data
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def process_atom_responce( type, response )
|
87
|
+
result = []
|
88
|
+
doc = REXML::Document.new( response )
|
89
|
+
REXML::XPath.each( doc, '//entry' ) do |element|
|
90
|
+
result << eval( "Models::#{type.to_s.capitalize}.new( element )" )
|
91
|
+
end
|
92
|
+
return result
|
93
|
+
end
|
94
|
+
|
95
|
+
def get_etag( path )
|
96
|
+
arr = @etags.assoc( path )
|
97
|
+
etag = data = nil
|
98
|
+
if arr
|
99
|
+
etag = arr[1]
|
100
|
+
data = arr[2]
|
101
|
+
if arr != @etags.first
|
102
|
+
@etags.delete(arr)
|
103
|
+
@etags.unshift( arr )
|
104
|
+
end
|
105
|
+
end
|
106
|
+
return etag, data
|
107
|
+
end
|
108
|
+
|
109
|
+
def save_etag( path, etag, data )
|
110
|
+
@etags.unshift( [path,etag,data] )
|
111
|
+
@etags.pop if @etags.length > 99
|
112
|
+
end
|
113
|
+
|
114
|
+
def update_etag( path, etag, data )
|
115
|
+
arr = @etags.assoc( path )
|
116
|
+
if arr
|
117
|
+
if arr == @etags.first
|
118
|
+
@etags[0] = [path,etag,data]
|
119
|
+
else
|
120
|
+
@etags.delete(arr)
|
121
|
+
@etags.unshift( [path,etag,data] )
|
122
|
+
end
|
123
|
+
else
|
124
|
+
@etags.unshift( [path,etag,data] )
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#
|
2
|
+
# model.rb
|
3
|
+
# restbooks
|
4
|
+
#
|
5
|
+
# Created by Paul Chavard on 2009-09-12.
|
6
|
+
# Copyright 2009
|
7
|
+
#
|
8
|
+
# This file is part of RestBooks.
|
9
|
+
#
|
10
|
+
# RestBooks is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU General Public License as published by
|
12
|
+
# the Free Software Foundation, either version 3 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# RestBooks is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with RestBooks. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
#
|
23
|
+
|
24
|
+
module RestBooks
|
25
|
+
module Model
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
return @element.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_entry
|
32
|
+
return @element
|
33
|
+
end
|
34
|
+
|
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
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#
|
2
|
+
# author.rb
|
3
|
+
# restbooks
|
4
|
+
#
|
5
|
+
# Created by Paul Chavard on 2009-09-12.
|
6
|
+
# Copyright 2009
|
7
|
+
#
|
8
|
+
# This file is part of RestBooks.
|
9
|
+
#
|
10
|
+
# RestBooks is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU General Public License as published by
|
12
|
+
# the Free Software Foundation, either version 3 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# RestBooks is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with RestBooks. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
#
|
23
|
+
|
24
|
+
module RestBooks
|
25
|
+
module Models
|
26
|
+
class Author
|
27
|
+
include RestBooks::Model
|
28
|
+
attr_reader :title, :id, :updated, :born, :death
|
29
|
+
|
30
|
+
def initialize( element )
|
31
|
+
if element
|
32
|
+
@element = element
|
33
|
+
@element.elements.each do |tag|
|
34
|
+
case tag.name
|
35
|
+
when 'title'
|
36
|
+
@title = tag.text
|
37
|
+
when 'id'
|
38
|
+
@id = tag.text.scan(/author\/(\d+)\/books/).to_s
|
39
|
+
@id = @id.split(':').last if @id.split(':').length > 1
|
40
|
+
when 'updated'
|
41
|
+
@updated = Date.parse( tag.text )
|
42
|
+
when 'content'
|
43
|
+
dates = tag.text.strip.split(' - ')
|
44
|
+
@born = dates.first
|
45
|
+
@death = dates.last
|
46
|
+
end
|
47
|
+
end
|
48
|
+
else
|
49
|
+
@element = REXML::Element.new( 'entry' )
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_hash
|
54
|
+
return { :title => @title,
|
55
|
+
:id => @id,
|
56
|
+
:updated => @updated,
|
57
|
+
:born => @date,
|
58
|
+
:death => @subject }
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
#
|
2
|
+
# book.rb
|
3
|
+
# restbooks
|
4
|
+
#
|
5
|
+
# Created by Paul Chavard on 2009-09-12.
|
6
|
+
# Copyright 2009
|
7
|
+
#
|
8
|
+
# This file is part of RestBooks.
|
9
|
+
#
|
10
|
+
# RestBooks is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU General Public License as published by
|
12
|
+
# the Free Software Foundation, either version 3 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# RestBooks is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with RestBooks. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
#
|
23
|
+
|
24
|
+
module RestBooks
|
25
|
+
module Models
|
26
|
+
class Book
|
27
|
+
include RestBooks::Model
|
28
|
+
attr_reader :title, :id, :author, :updated, :language, :date, :subject, :description, :rights
|
29
|
+
|
30
|
+
def initialize( element=nil )
|
31
|
+
@updated = Time.now
|
32
|
+
@subject = []
|
33
|
+
if element
|
34
|
+
@element = element
|
35
|
+
@element.elements.each do |tag|
|
36
|
+
case tag.name
|
37
|
+
when 'title'
|
38
|
+
@title = tag.text
|
39
|
+
when 'id'
|
40
|
+
@id = tag.text.split('/').last
|
41
|
+
@id = @id.split(':').last if @id.split(':').length > 1
|
42
|
+
when 'updated'
|
43
|
+
@updated = Date.parse( tag.text )
|
44
|
+
when 'author'
|
45
|
+
@author = tag.elements['name'].text
|
46
|
+
@author_id = tag.elements['uri'].text.split('/').last
|
47
|
+
when 'language'
|
48
|
+
@language = tag.text
|
49
|
+
when 'date'
|
50
|
+
@date = tag.text
|
51
|
+
when 'subject'
|
52
|
+
@subject << tag.text
|
53
|
+
when 'description'
|
54
|
+
@description = tag.text.strip()
|
55
|
+
when 'rights'
|
56
|
+
@rights = tag.text
|
57
|
+
when 'content'
|
58
|
+
@html = tag.text if tag.attributes['type'] == "xhtml"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
else
|
62
|
+
@element = REXML::Element.new( 'entry' )
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_html
|
67
|
+
return @html
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_hash
|
71
|
+
return { :title => @title,
|
72
|
+
:id => @id,
|
73
|
+
:author => @author,
|
74
|
+
:author_id => @author_id,
|
75
|
+
:updated => @updated,
|
76
|
+
:language => @language,
|
77
|
+
:date => @date,
|
78
|
+
:subject => @subject,
|
79
|
+
:description => @description,
|
80
|
+
:rights => @rights }
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#
|
2
|
+
# category.rb
|
3
|
+
# restbooks
|
4
|
+
#
|
5
|
+
# Created by Paul Chavard on 2009-09-12.
|
6
|
+
# Copyright 2009
|
7
|
+
#
|
8
|
+
# This file is part of RestBooks.
|
9
|
+
#
|
10
|
+
# RestBooks is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU General Public License as published by
|
12
|
+
# the Free Software Foundation, either version 3 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# RestBooks is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with RestBooks. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
#
|
23
|
+
|
24
|
+
module RestBooks
|
25
|
+
module Models
|
26
|
+
class Category
|
27
|
+
include RestBooks::Model
|
28
|
+
attr_reader :title, :id, :updated, :description
|
29
|
+
|
30
|
+
def initialize( element )
|
31
|
+
if element
|
32
|
+
@element = element
|
33
|
+
@element.elements.each do |tag|
|
34
|
+
case tag.name
|
35
|
+
when 'title'
|
36
|
+
@title = tag.text.strip()
|
37
|
+
@id = CGI.escape( @title )
|
38
|
+
when 'updated'
|
39
|
+
@updated = Date.parse( tag.text )
|
40
|
+
end
|
41
|
+
end
|
42
|
+
else
|
43
|
+
@element = REXML::Element.new( 'entry' )
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_hash
|
48
|
+
return { :title => @title,
|
49
|
+
:id => @id,
|
50
|
+
:updated => @updated }
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#
|
2
|
+
# comment.rb
|
3
|
+
# restbooks
|
4
|
+
#
|
5
|
+
# Created by Paul Chavard on 2009-09-12.
|
6
|
+
# Copyright 2009
|
7
|
+
#
|
8
|
+
# This file is part of RestBooks.
|
9
|
+
#
|
10
|
+
# RestBooks is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU General Public License as published by
|
12
|
+
# the Free Software Foundation, either version 3 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# RestBooks is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with RestBooks. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
#
|
23
|
+
|
24
|
+
module RestBooks
|
25
|
+
module Models
|
26
|
+
class Comment
|
27
|
+
include RestBooks::Model
|
28
|
+
attr_reader :title, :id, :author, :published, :updated, :body
|
29
|
+
|
30
|
+
def initialize( element )
|
31
|
+
if element
|
32
|
+
@element = element
|
33
|
+
@element.elements.each do |tag|
|
34
|
+
case tag.name
|
35
|
+
when 'title'
|
36
|
+
@title = tag.text
|
37
|
+
when 'id'
|
38
|
+
@id = tag.text.split('#').last
|
39
|
+
when 'author'
|
40
|
+
@author = tag.elements['name'].text
|
41
|
+
when 'published'
|
42
|
+
@published = Date.parse( tag.text )
|
43
|
+
when 'updated'
|
44
|
+
@updated = Date.parse( tag.text )
|
45
|
+
when 'content'
|
46
|
+
@body = tag.text.strip()
|
47
|
+
end
|
48
|
+
end
|
49
|
+
else
|
50
|
+
@element = REXML::Element.new( 'entry' )
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_hash
|
55
|
+
return { :title => @title,
|
56
|
+
:id => @id,
|
57
|
+
:author => @author,
|
58
|
+
:published => @published,
|
59
|
+
:updated => @updated,
|
60
|
+
:body => @date }
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#
|
2
|
+
# list.rb
|
3
|
+
# restbooks
|
4
|
+
#
|
5
|
+
# Created by Paul Chavard on 2009-09-12.
|
6
|
+
# Copyright 2009
|
7
|
+
#
|
8
|
+
# This file is part of RestBooks.
|
9
|
+
#
|
10
|
+
# RestBooks is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU General Public License as published by
|
12
|
+
# the Free Software Foundation, either version 3 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# RestBooks is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU General Public License
|
21
|
+
# along with RestBooks. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
#
|
23
|
+
|
24
|
+
module RestBooks
|
25
|
+
module Models
|
26
|
+
class List
|
27
|
+
include RestBooks::Model
|
28
|
+
attr_reader :title, :id, :updated, :description
|
29
|
+
|
30
|
+
def initialize( element )
|
31
|
+
if element
|
32
|
+
@element = element
|
33
|
+
@element.elements.each do |tag|
|
34
|
+
case tag.name
|
35
|
+
when 'title'
|
36
|
+
@title = tag.text.strip()
|
37
|
+
when 'id'
|
38
|
+
@id = tag.text.split('/').last
|
39
|
+
@id = @id.split(':').last if @id.split(':').length > 1
|
40
|
+
when 'updated'
|
41
|
+
@updated = Date.parse( tag.text )
|
42
|
+
when 'content'
|
43
|
+
@description = tag.text.strip()
|
44
|
+
end
|
45
|
+
end
|
46
|
+
else
|
47
|
+
@element = REXML::Element.new( 'entry' )
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_hash
|
52
|
+
return { :title => @title,
|
53
|
+
:id => @id,
|
54
|
+
:updated => @updated,
|
55
|
+
:description => @description }
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|