restbooks 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/README
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
WARNING !
|
2
|
+
I will not guaranty API consistency until 0.5 release!
|
3
|
+
|
4
|
+
Main Features :
|
5
|
+
- API mapping for "books", "authors", "lists" and "categories" using atom format
|
6
|
+
- ETag support (the same instance fetch only ones)
|
7
|
+
|
8
|
+
Roadmap :
|
9
|
+
- Atom pub
|
10
|
+
- User Books API
|
11
|
+
|
12
|
+
INSTALL :
|
13
|
+
|
14
|
+
gem install tchak-restbooks
|
15
|
+
|
16
|
+
DOC :
|
17
|
+
http://restbooks.tchak.net
|
18
|
+
|
19
|
+
HOWTO :
|
20
|
+
|
21
|
+
require 'rubygems'
|
22
|
+
require 'restbooks'
|
23
|
+
|
24
|
+
client = RestBooks::Client.new( :log => 'stdout' )
|
25
|
+
|
26
|
+
m_books = client.books.all
|
27
|
+
m_authors = client.authors.all( { :letter => 'A' } )
|
28
|
+
m_authors = client.authors.all( { :page => 2 } )
|
29
|
+
|
30
|
+
m_author = client.authors.top.first
|
31
|
+
m_book = client.authors.books( m_author ).first
|
32
|
+
p m_book.title
|
33
|
+
p m_book.description
|
34
|
+
|
35
|
+
m_comments = client.books.comments( m_book )
|
36
|
+
m_comments.each do |comment|
|
37
|
+
p comment.title
|
38
|
+
p comment.body
|
39
|
+
end
|
data/lib/restbooks.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
#
|
2
|
+
# restbooks.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
|
+
# Gems
|
25
|
+
require 'rubygems'
|
26
|
+
require 'rest_client'
|
27
|
+
#require 'json'
|
28
|
+
|
29
|
+
# StdLib
|
30
|
+
require 'rexml/document'
|
31
|
+
require 'cgi'
|
32
|
+
require 'digest/md5'
|
33
|
+
require 'date'
|
34
|
+
|
35
|
+
# Lib
|
36
|
+
require 'uri/uri'
|
37
|
+
require 'restbooks/mixins/api'
|
38
|
+
require 'restbooks/mixins/model'
|
39
|
+
require 'restbooks/models/author'
|
40
|
+
require 'restbooks/models/book'
|
41
|
+
require 'restbooks/models/comment'
|
42
|
+
require 'restbooks/models/list'
|
43
|
+
require 'restbooks/models/category'
|
44
|
+
require 'restbooks/authors'
|
45
|
+
require 'restbooks/books'
|
46
|
+
require 'restbooks/lists'
|
47
|
+
require 'restbooks/categories'
|
48
|
+
|
49
|
+
module RestBooks
|
50
|
+
class Client
|
51
|
+
attr_reader :uri, :user_agent
|
52
|
+
|
53
|
+
def initialize( options={} )
|
54
|
+
# Set host
|
55
|
+
host = options.has_key?( :host ) ? options.delete(:host) : 'http://feedbooks.com'
|
56
|
+
|
57
|
+
# Set user-agent
|
58
|
+
@user_agent = options.has_key?( :user_agent ) ? options.delete( :user_agent ) : "RestBooks 0.1"
|
59
|
+
|
60
|
+
# Set service URI
|
61
|
+
@uri = URI.parse( host )
|
62
|
+
|
63
|
+
# Set authentification
|
64
|
+
if options.has_key?( :user ) && options.has_key?( :password )
|
65
|
+
@uri.user = options.delete( :user )
|
66
|
+
@uri.password = Digest::MD5.hexdigest( options.delete( :password ) )
|
67
|
+
end
|
68
|
+
|
69
|
+
# Set logger output
|
70
|
+
RestClient.log = options.delete( :log ) if options.has_key?( :log )
|
71
|
+
|
72
|
+
# Init services
|
73
|
+
@books = false
|
74
|
+
@authors = false
|
75
|
+
@lists = false
|
76
|
+
@categories = false
|
77
|
+
end
|
78
|
+
|
79
|
+
# Use this object to acess books resource
|
80
|
+
def books
|
81
|
+
@books = Books.new( self ) if !@books
|
82
|
+
return @books
|
83
|
+
end
|
84
|
+
|
85
|
+
# Use this object to acess authors resource
|
86
|
+
def authors
|
87
|
+
@authors = Authors.new( self ) if !@authors
|
88
|
+
return @authors
|
89
|
+
end
|
90
|
+
|
91
|
+
# Use this object to acess lists resource
|
92
|
+
def lists
|
93
|
+
@lists = Lists.new( self ) if !@lists
|
94
|
+
return @lists
|
95
|
+
end
|
96
|
+
|
97
|
+
# Use this object to acess categories resource
|
98
|
+
def categories
|
99
|
+
@categories = Categories.new( self ) if !@categories
|
100
|
+
return @categories
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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
|