goodreads 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/goodreads.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rest-client'
2
+ require 'xmlsimple'
3
+ require 'active_support/all'
4
+
5
+ require 'goodreads/goodreads'
6
+ require 'goodreads/client'
7
+ require 'goodreads/record'
@@ -0,0 +1,52 @@
1
+ module Goodreads
2
+ class Client
3
+ @@config = {}
4
+
5
+ # Initialize the API client
6
+ # You must specify the API key given by goodreads in order to make requests
7
+ # :api_key - Your api key
8
+ def initialize(opts={})
9
+ raise ArgumentError, 'API key required!' unless opts.key?(:api_key)
10
+ @@config[:api_key] = opts[:api_key]
11
+ end
12
+
13
+ # Get most recent reviews
14
+ # :skip_cropped - Select only non-cropped reviews
15
+ def recent_reviews(params={})
16
+ data = request('/review/recent_reviews', params)
17
+ reviews = data['reviews']['review'].collect { |r| Goodreads::Record.new(r) }
18
+ reviews = reviews.select { |r| !r.body.include?(r.url) } if params.key?(:skip_cropped)
19
+ return reviews
20
+ end
21
+
22
+ # Get book (including reviews) by ISBN.
23
+ # :skip_cropped_reviews - Select only non-cropped book reviews
24
+ # :page - Reviews page
25
+ # :per_page - Reviews per page (default to 30)
26
+ def book_by_isbn(isbn, params={})
27
+ params.merge!(:isbn => isbn)
28
+ data = request('/book/isbn', params)
29
+ record = Goodreads::Record.new(data['book'])
30
+ if params.key?(:skip_cropped_reviews)
31
+ record.reviews['review'] = record.reviews.review.select { |r| !r.body.include?(r.url) }
32
+ end
33
+ return record
34
+ end
35
+
36
+ private
37
+
38
+ # Perform an API request
39
+ def request(path, params={})
40
+ params.merge!(:format => 'xml', :key => @@config[:api_key])
41
+ begin
42
+ resp = RestClient.get("#{API_URL}#{path}", :params => params)
43
+ Hash.from_xml(resp)['GoodreadsResponse']
44
+ rescue RestClient::Unauthorized
45
+ raise AuthError, 'Invalid API token!'
46
+ rescue Exception => ex
47
+ raise NotFound, 'Resource was not found!' if ex.http_code == 404
48
+ raise GeneralError, ex.message
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,8 @@
1
+ module Goodreads
2
+ API_URL = 'http://www.goodreads.com'
3
+ API_FORMAT = 'xml'
4
+
5
+ class AuthError < Exception ; end
6
+ class NotFound < Exception ; end
7
+ class GeneralError < Exception ; end
8
+ end
@@ -0,0 +1,38 @@
1
+ module Goodreads
2
+ class Record
3
+ # Initialize the record and set its data if present
4
+ def initialize(data=nil)
5
+ @fields = {}
6
+ setup(data) unless data.nil?
7
+ end
8
+
9
+ # Set or override the existing field value
10
+ def []=(key, value)
11
+ @fields[key] = value
12
+ end
13
+
14
+ # Get the field value by hash key
15
+ def [](key)
16
+ @fields[key]
17
+ end
18
+
19
+ # Load data into the record
20
+ def setup(data)
21
+ data.each_pair do |key, value|
22
+ case value
23
+ when Hash
24
+ @fields[key] = Record.new(value)
25
+ when Array
26
+ @fields[key] = value.collect { |v| Record.new(v) }
27
+ when NilClass
28
+ @fields[key] = nil
29
+ else
30
+ @fields[key] = value
31
+ end
32
+ (class << self; self; end).class_eval do
33
+ define_method(key.to_sym) { return @fields[key] }
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goodreads
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Dan Sosedoff
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-05 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Simplified client to the Goodreads.com API
23
+ email: dan.sosedoff@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/goodreads.rb
32
+ - lib/goodreads/goodreads.rb
33
+ - lib/goodreads/client.rb
34
+ - lib/goodreads/record.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/sosedoff/goodreads
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Simplified client to the Goodreads.com API
69
+ test_files: []
70
+