tchak-restbooks 0.1.1

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/README ADDED
@@ -0,0 +1,26 @@
1
+ INSTALL :
2
+
3
+ gem install tchak-restbooks
4
+
5
+ HOWTO :
6
+
7
+ require "restbooks"
8
+
9
+ client = RestBooks::Client.new( :log => 'stdout' )
10
+ Or
11
+ client = RestBooks::Client.new( { :user => 'toto', :password => 'secret' } )
12
+
13
+ m_books = client.books.all
14
+ m_authors = client.authors.all( { :letter => 'A' } )
15
+ m_authors = client.authors.all( { :page => 2 } )
16
+
17
+ m_author = client.authors.top.first
18
+ m_book = client.authors.books( my_author )
19
+ p m_book.title
20
+ p m_book.description
21
+
22
+ m_comments = client.books.comments( my_book )
23
+ m_comments.each do |comment|
24
+ p comment.title
25
+ p comment.body
26
+ end
data/lib/authors.rb ADDED
@@ -0,0 +1,100 @@
1
+ #
2
+ # authors.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
+ class Authors
26
+ include RestBooks::Api
27
+
28
+ # Endpoint: /authors
29
+ # Formats: Atom
30
+ # Description: List all authors (20 items)
31
+ # Parameters:
32
+ # * letter: display the authors with ... as the first letter of their name
33
+ # * born: authors born in ...
34
+ # * death: author who dies in ...
35
+ # * lang: language of the author
36
+ # * order: normally from A to Z, use desc to reverse order
37
+ def all( options={} )
38
+ create_url( "/authors", options, ORDERED_AUTHOR_PARAMETERS )
39
+ return do_request( :author )
40
+ end
41
+
42
+ # Endpoint: /authors/top
43
+ # Formats: Atom
44
+ # Description: List top authors (20 items)
45
+ # Parameters:
46
+ # * letter: display the authors with ... as the first letter of their name
47
+ # * born: authors born in ...
48
+ # * death: author who dies in ...
49
+ # * lang: language of the author
50
+ # * range: normally overall, week will display the most popular for the previous week and month for the last 4 weeks
51
+ def top( options={} )
52
+ create_url( "/authors/top", options, RANGED_AUTHOR_PARAMETERS )
53
+ return do_request( :author )
54
+ end
55
+
56
+ # Endpoint: /authors/recent
57
+ # Formats: Atom
58
+ # Description: List recent authors (20 items)
59
+ # Parameters:
60
+ # * letter: display the authors with ... as the first letter of their name
61
+ # * born: authors born in ...
62
+ # * death: author who dies in ...
63
+ # * lang: language of the author
64
+ def recent( options={} )
65
+ create_url( "/authors/recent", options, AUTHOR_PARAMETERS )
66
+ return do_request( :author )
67
+ end
68
+
69
+ # Endpoint: /author/:id/books
70
+ # Formats: Atom
71
+ # Description: List all books for an author (id)
72
+ # Parameters: Same as /books
73
+ def books( author_id, options={} )
74
+ author_id = author_id.id if author_id.kind_of? RestBooks::Models::Author
75
+ create_url( "/author/#{author_id}/books", options, ORDERED_BOOK_PARAMETERS )
76
+ return do_request( :book )
77
+ end
78
+
79
+ # Endpoint: /author/:id/books/top
80
+ # Formats: Atom
81
+ # Description: List top books for an author (id)
82
+ # Parameters: Same as /books/top
83
+ def top_books( author_id, options={} )
84
+ author_id = author_id.id if author_id.kind_of? RestBooks::Models::Author
85
+ create_url( "/author/#{author_id}/books/top", options, RANGED_BOOK_PARAMETERS )
86
+ return do_request( :book )
87
+ end
88
+
89
+ # Endpoint: /author/:id/books/recent
90
+ # Formats: Atom, RSS
91
+ # Description: List recent books for an author (id)
92
+ # Parameters: Same as /books/recent
93
+ def recent_books( author_id, options={} )
94
+ author_id = author_id.id if author_id.kind_of? RestBooks::Models::Author
95
+ create_url( "/author/#{author_id}/books/recent", options, BOOK_PARAMETERS )
96
+ return do_request( :book )
97
+ end
98
+
99
+ end
100
+ end
data/lib/books.rb ADDED
@@ -0,0 +1,124 @@
1
+ #
2
+ # books.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
+ class Books
26
+ include RestBooks::Api
27
+
28
+ # Endpoint: /books
29
+ # Formats: Atom
30
+ # Description: List all books (20 items)
31
+ # Parameters:
32
+ # * letter: display the books with ... as the first letter of their name
33
+ # * year: book released in ...
34
+ # * lang: language of the book
35
+ # * order: normally from A to Z, use desc to reverse order or year to sort them by release date
36
+ def all( options={} )
37
+ create_url( "/books", options, ORDERED_BOOK_PARAMETERS )
38
+ return do_request( :book )
39
+ end
40
+
41
+ # Endpoint: /books/top
42
+ # Formats: Atom
43
+ # Description: List top books (20 items)
44
+ # Parameters:
45
+ # * letter: display the books with ... as the first letter of their name
46
+ # * year: book released in ...
47
+ # * lang: language of the book
48
+ # * range: normally overall, week will display the most popular for the previous week and month for the last 4 weeks
49
+ def top( options={} )
50
+ create_url( "/books/top", options, RANGED_BOOK_PARAMETERS )
51
+ return do_request( :book )
52
+ end
53
+
54
+ # Endpoint: /books/recent
55
+ # Formats: Atom, RSS
56
+ # Description: List recent books (20 items)
57
+ # Parameters:
58
+ # * letter: display the books with ... as the first letter of their name
59
+ # * year: book released in ...
60
+ # * lang: language of the book
61
+ def recent( options={} )
62
+ create_url( "/books/recent", options, BOOK_PARAMETERS )
63
+ return do_request( :book )
64
+ end
65
+
66
+ # Endpoint: /books/search
67
+ # Formats: Atom
68
+ # Description: Search for a book
69
+ # Parameters:
70
+ # * query: you can use advanced queries too (for example "type:Horror AND Lovecraft")
71
+ def search( query, options={} )
72
+ options[:query] = query
73
+ create_url( "/books/search", options, [:query] )
74
+ return do_request( :book )
75
+ end
76
+
77
+ # Endpoint: /book/:id/similar
78
+ # Formats: Atom
79
+ # Description: List similar books for a given book
80
+ # Parameters: none
81
+ def similar( book_id, options={} )
82
+ book_id = book_id.id if book_id.kind_of? RestBooks::Models::Book
83
+ create_url( "/book/#{book_id}/similar", options, [:page] )
84
+ return do_request( :book )
85
+ end
86
+
87
+ # Endpoint: /book/:id/lists
88
+ # Formats: Atom
89
+ # Description: View in which list a book is included
90
+ # Parameters:
91
+ # * order: normally the order is based on the number of favorites, use asc or desc to switch this to alphabetical order
92
+ def lists
93
+ book_id = book_id.id if book_id.kind_of? RestBooks::Models::Book
94
+ create_url( "/book/#{book_id}/lists", options, LIST_PARAMETERS )
95
+ return do_request( :list )
96
+ end
97
+
98
+ # Endpoint: /book/:id/comments
99
+ # Formats: Atom
100
+ def comments( book_id, options={} )
101
+ book_id = book_id.id if book_id.kind_of? RestBooks::Models::Book
102
+ create_url( "/book/#{book_id}/comments", options, [:page] )
103
+ return do_request( :comment )
104
+ end
105
+
106
+ # Endpoint: /book/:id/comments
107
+ # Formats: Atom, EPub, Mobipocket, PDF, png
108
+ # Description: Get the book, information or cover
109
+ # Parameters:
110
+ # * size: for png cover you can use smaller
111
+ def get( book_id, options={} )
112
+ book_id = book_id.id if book_id.kind_of? RestBooks::Models::Book
113
+ options.delete(:size) if options[:format] != :png
114
+ create_url( "/book/#{book_id}", options, [:size] )
115
+ if @uri.format.to_sym == :atom
116
+ return do_request( :book )
117
+ elsif [ :epub, :mobi, :pdf, :atom, :png ].include?( @uri.format.to_sym )
118
+ responce = RestClient::Request.execute(:method => :get, :url => @uri.to_s, :headers => {}, :raw_response => true)
119
+ return { :content_type => responce.headers[:content_type], :path => responce.file.path }
120
+ end
121
+ end
122
+
123
+ end
124
+ end
data/lib/categories.rb ADDED
@@ -0,0 +1,68 @@
1
+ #
2
+ # categories.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
+ class Categories
26
+ include RestBooks::Api
27
+
28
+ # Endpoint: /types
29
+ # Formats: Atom
30
+ # Description: View the categories, use pagination
31
+ # Parameters:
32
+ # * order: normally based on the number of favorites, use comments to order them by most discussed lists
33
+ def all( options={} )
34
+ create_url( "/types", options, LIST_PARAMETERS )
35
+ return do_request( :category )
36
+ end
37
+
38
+ # Endpoint: /type/:id/books
39
+ # Formats: Atom
40
+ # Description: List all books from given category, use pagination
41
+ def books( category_id, options={} )
42
+ category_id = category_id.id if category_id.kind_of? RestBooks::Models::Category
43
+ create_url( "/type/#{category_id}/books", options, ORDERED_BOOK_PARAMETERS )
44
+ return do_request( :book )
45
+ end
46
+
47
+ # Endpoint: /type/:id/books/top
48
+ # Formats: Atom
49
+ # Description: List top books for a category (id)
50
+ # Parameters: Same as /books/top
51
+ def top_books( category_id, options={} )
52
+ category_id = category_id.id if category_id.kind_of? RestBooks::Models::Category
53
+ create_url( "/type/#{category_id}/books/top", options, RANGED_BOOK_PARAMETERS )
54
+ return do_request( :book )
55
+ end
56
+
57
+ # Endpoint: /type/:id/books/recent
58
+ # Formats: Atom, RSS
59
+ # Description: List recent books for a category (id)
60
+ # Parameters: Same as /books/recent
61
+ def recent_books( category_id, options={} )
62
+ category_id = category_id.id if category_id.kind_of? RestBooks::Models::Category
63
+ fcreate_url( "/type/#{category_id}/books/recent", options, BOOK_PARAMETERS )
64
+ return do_request( :book )
65
+ end
66
+
67
+ end
68
+ end
data/lib/extend/uri.rb ADDED
@@ -0,0 +1,30 @@
1
+ #
2
+ # uri.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 URI
25
+ class Generic
26
+ def format
27
+ return self.path.split('.').last
28
+ end
29
+ end
30
+ end
data/lib/lists.rb ADDED
@@ -0,0 +1,49 @@
1
+ #
2
+ # lists.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
+ class Lists
26
+ include RestBooks::Api
27
+
28
+ # Endpoint: /lists
29
+ # Formats: Atom
30
+ # Description: View the lists, use pagination
31
+ # Parameters:
32
+ # * favorites: normally, only the lists with at least 2 favorites are displayed. You can change the minimum value using this parameter.
33
+ # * order: normally based on the number of favorites, use comments to order them by most discussed lists
34
+ def all( options={} )
35
+ create_url( "/lists", options, LIST_PARAMETERS )
36
+ return do_request( :list )
37
+ end
38
+
39
+ # Endpoint: /list/:id
40
+ # Formats: Atom
41
+ # Description: View the books on the lists, use pagination
42
+ def books( list_id, options={} )
43
+ list_id = list_id.id if list_id.kind_of? RestBooks::Models::List
44
+ create_url( "/list/#{list_id}", options, LIST_PARAMETERS )
45
+ return do_request( :book )
46
+ end
47
+
48
+ end
49
+ end
data/lib/mixins/api.rb ADDED
@@ -0,0 +1,126 @@
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 )
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 )
74
+ end
75
+ case format
76
+ when :atom
77
+ return process_atom_responce( type, data )
78
+ # when :json
79
+ # #return JSON.parse( data )
80
+ end
81
+ end
82
+
83
+ def process_atom_responce( type, responce )
84
+ result = []
85
+ doc = REXML::Document.new( responce )
86
+ REXML::XPath.each( doc, '//entry' ) do |element|
87
+ result << eval( "Models::#{type.to_s.capitalize}.new( element )" )
88
+ end
89
+ return result
90
+ end
91
+
92
+ def get_etag( path )
93
+ arr = @etags.assoc( path )
94
+ etag = data = nil
95
+ if arr
96
+ etag = arr[1]
97
+ data = arr[2]
98
+ if arr != @etags.first
99
+ @etags.delete(arr)
100
+ @etags.unshift( arr )
101
+ end
102
+ end
103
+ return etag, data
104
+ end
105
+
106
+ def save_etag( path, etag, data )
107
+ @etags.unshift( [path,etag,data] )
108
+ @etags.pop if @etags.length > 99
109
+ end
110
+
111
+ def update_etag( path, etag, data )
112
+ arr = @etags.assoc( path )
113
+ if arr
114
+ if arr == @etags.first
115
+ @etags[0] = [path,etag,data]
116
+ else
117
+ @etags.delete(arr)
118
+ @etags.unshift( [path,etag,data] )
119
+ end
120
+ else
121
+ @etags.unshift( [path,etag,data] )
122
+ end
123
+ end
124
+
125
+ end
126
+ end
@@ -0,0 +1,46 @@
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 title=( str )
28
+ # @title = str
29
+ # @element.elements['title'].text = str
30
+ # end
31
+
32
+ def to_s
33
+ return @element.to_s
34
+ end
35
+
36
+ def to_entry
37
+ return @element
38
+ end
39
+
40
+ # def to_json
41
+ # model = self.class.to_s.split('::').last.downcase.to_sym
42
+ # return JSON.generate( { model => self.to_hash } )
43
+ # end
44
+
45
+ end
46
+ 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.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
+ 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