openlibrary 2.1.2 → 2.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f578f43980200e2e5124b4abcdc0b516dd0c2a2
4
- data.tar.gz: ef2dc76ccf9cf3f8e530b3369784ff0a2a3f9d24
3
+ metadata.gz: f339172bc4d24e372b7413f8ce04f2fcc96a3dff
4
+ data.tar.gz: 821e0818118023cf063562060b82bba2417f7e24
5
5
  SHA512:
6
- metadata.gz: 44db922fb27831d196913dd53151389f90e43d15a849e0a233712a5d95ed66cd96077cceeff7d492ecef6088c2bfcc8c1578bf343f4c23c24b4154a5e4d2f6b0
7
- data.tar.gz: 4fd2b5adf5f82e08058e69f8132975ebb24c76fcddcd563179efab2a3e38f287a9d91037c06ae3ec49181740400fbf0e91cfd9d0067ee919817b49eb7b47c082
6
+ metadata.gz: 291516ab8c9c71e98e1d645e7ccc8eb5b4fba7231a73e683169570c2da373597b6e653728531ff3c6e4df80aa92ebd382c917b1a2998cc3d6a03d52f5099141d
7
+ data.tar.gz: 3761380fe93b763c17d105c9c439cae8f03208bb6ae9b90d6f7cc16c75853595f238b832609a18002221da86e9ef76eb0e3f75b6f4fe5d5d58d016df783d3425
data/.gitignore CHANGED
@@ -5,3 +5,4 @@ pkg/*
5
5
  .rvmrc
6
6
  *~
7
7
  *.swp
8
+ .rbenv-version
@@ -146,7 +146,7 @@ NOTICE: Before you can actually save changes to Open Library, you need to regist
146
146
 
147
147
  == Books Client
148
148
 
149
- There are two classes in the openlibrary gem you can use to access the {Books API}[http://openlibrary.org/dev/docs/api/books]. Use Openlibrary::View to look up Open Library listing information, and Openlibrary::Data to get a book's full metadata details.
149
+ There are three classes in the openlibrary gem you can use to access the {Books API}[http://openlibrary.org/dev/docs/api/books]. Use Openlibrary::View to look up Open Library listing information, Openlibrary::Data to get a book's full metadata details, and Openlibrary::Details to get book details in addition to what the Openlibrary::View class provides.
150
150
 
151
151
  === Openlibrary::View
152
152
 
@@ -183,16 +183,35 @@ Instantiate the class:
183
183
 
184
184
  Look up a book by its ISBN-10 or ISBN-13:
185
185
 
186
- book_details = data.find_by_isbn("0451526538")
186
+ book_data = data.find_by_isbn("0451526538")
187
187
 
188
- book_details.title # book's title
189
- book_details.authors # array of authors
188
+ book_data.title # book's title
189
+ book_data.authors # array of authors
190
190
 
191
191
  Other built-in finder methods:
192
192
 
193
- view.find_by_lccn # Library of Congress catalog number
194
- view.find_by_oclc # Worldcat Control Number
195
- view.find_by_olid # Open Library ID
193
+ data.find_by_lccn # Library of Congress catalog number
194
+ data.find_by_oclc # Worldcat Control Number
195
+ data.find_by_olid # Open Library ID
196
+
197
+ === Openlibrary::Details
198
+
199
+ Instantiate the class:
200
+
201
+ details = Openlibrary::Details
202
+
203
+ Look up a book by its ISBN-10 or ISBN-13:
204
+
205
+ book_details = details.find_by_isbn("0451526538")
206
+
207
+ book_details.info_url # book's URL on Open Library
208
+ book_details.details # additional details, such as description
209
+
210
+ Other built-in finder methods:
211
+
212
+ details.find_by_lccn # Library of Congress catalog number
213
+ details.find_by_oclc # Worldcat Control Number
214
+ details.find_by_olid # Open Library ID
196
215
 
197
216
  == CONTRIBUTORS
198
217
 
@@ -204,6 +223,8 @@ Other built-in finder methods:
204
223
  * Alex Grant https://github.com/grantovich
205
224
  * Bryan L. Fordham https://github.com/bfordham
206
225
  * Kyle Corbitt https://github.com/kcorbitt
226
+ * Matt Dressel https://github.com/dresselm
227
+ * Scott Lesser https://github.com/okcscott
207
228
 
208
229
  == LICENSE
209
230
 
@@ -4,6 +4,7 @@ require_relative 'openlibrary/view'
4
4
  require_relative 'openlibrary/client'
5
5
  require_relative 'openlibrary/errors'
6
6
  require_relative 'openlibrary/request'
7
+ require_relative 'openlibrary/details'
7
8
 
8
9
  module Openlibrary
9
10
  # Create a new Openlibrary::Client instance
@@ -0,0 +1,53 @@
1
+ module Openlibrary
2
+
3
+ class Details
4
+ attr_accessor :info_url
5
+ attr_accessor :bib_key
6
+ attr_accessor :preview_url
7
+ attr_accessor :thumbnail_url
8
+ attr_accessor :details
9
+
10
+ def self.find_by_isbn(key)
11
+ find("ISBN",key)
12
+ end
13
+
14
+ def self.find_by_lccn(key)
15
+ find("LCCN",key)
16
+ end
17
+
18
+ def self.find_by_oclc(key)
19
+ find("OCLC",key)
20
+ end
21
+
22
+ def self.find_by_olid(key)
23
+ find("OLID",key)
24
+ end
25
+
26
+ def self.find(type,key)
27
+ type_for_uri = URI.encode_www_form_component(type)
28
+ key_for_uri = URI.encode_www_form_component(key)
29
+
30
+ response = RestClient.get "http://openlibrary.org/api/books" +
31
+ "?bibkeys=#{type_for_uri}:#{key_for_uri}&format=json&jscmd=data"
32
+ response_data = JSON.parse(response)
33
+ book = response_data["#{type}:#{key}"]
34
+
35
+ if book
36
+ book_meta = new
37
+
38
+ book_meta.info_url = book["info_url"]
39
+ book_meta.bib_key = book["bib_key"]
40
+ book_meta.preview_url = book["preview_url"]
41
+ book_meta.thumbnail_url = book["thumbnail_url"]
42
+ book_meta.details = book["details"]
43
+
44
+ book_meta
45
+ else
46
+ nil
47
+ end
48
+ end
49
+ end
50
+
51
+
52
+ end
53
+
@@ -2,4 +2,5 @@ module Openlibrary
2
2
  class Error < StandardError; end
3
3
  class Unauthorized < Error; end
4
4
  class NotFound < Error; end
5
+ class Redirect < Error; end
5
6
  end
@@ -6,51 +6,40 @@ require 'hashie'
6
6
 
7
7
  module Openlibrary
8
8
  module Request
9
- API_URL = 'http://openlibrary.org'
9
+ API_URL = 'https://openlibrary.org'
10
10
 
11
11
  protected
12
12
 
13
+ HANDLE_REST_CLIENT_RESPONSE = lambda do |response, request, result, &block|
14
+ case response.code
15
+ when 200
16
+ response.return!(request, result, &block)
17
+ when 401
18
+ raise Openlibrary::Unauthorized
19
+ when 404
20
+ raise Openlibrary::NotFound
21
+ when 302
22
+ raise Openlibrary::Redirect
23
+ end
24
+ end
25
+
13
26
  # Perform a GET request
14
27
  #
15
28
  # path - Request path
16
29
  #
17
30
  def request(path, params={})
18
- params.merge!(accept: :json)
19
- url = "#{API_URL}#{path}"
20
-
21
- resp = RestClient.get(url, params) do |response, request, result, &block|
22
- case response.code
23
- when 200
24
- response.return!(request, result, &block)
25
- when 401
26
- raise Openlibrary::Unauthorized
27
- when 404
28
- raise Openlibrary::NotFound
29
- end
30
- end
31
- parse(resp)
31
+ request_url = "#{API_URL}#{path}"
32
+ perform_get_request(request_url, params)
32
33
  end
33
34
 
34
35
 
35
36
  # Get the history of an object in Open Library
36
- #
37
+ #
37
38
  # object - Object key, e.g., '/books/OL1M'
38
39
  #
39
40
  def history(object, params={})
40
- params.merge!(accept: :json)
41
- url = "#{API_URL}#{object}.json?m=history"
42
-
43
- resp = RestClient.get(url, params) do |response, request, result, &block|
44
- case response.code
45
- when 200
46
- response.return!(request, result, &block)
47
- when 401
48
- raise Openlibrary::Unauthorized
49
- when 404
50
- raise Openlibrary::NotFound
51
- end
52
- end
53
- parse(resp)
41
+ history_url = "#{API_URL}#{object}.json?m=history"
42
+ perform_get_request(history_url, params)
54
43
  end
55
44
 
56
45
  # Perform a query using the Query API
@@ -58,24 +47,13 @@ module Openlibrary
58
47
  # query - Query path, e.g. "type=/type/edition&isbn_10=XXXXXXXXXX"
59
48
  #
60
49
  def query(query, params={})
61
- params.merge!(accept: :json)
62
- url = "#{API_URL}/query.json?#{query}"
63
- resp = RestClient.get(url, params) do |response, request, result, &block|
64
- case response.code
65
- when 200
66
- response.return!(request, result, &block)
67
- when 401
68
- raise Openlibrary::Unauthorized
69
- when 404
70
- raise Openlibrary::NotFound
71
- end
72
- end
73
- parse(resp)
50
+ query_url = "#{API_URL}/query.json?#{query}"
51
+ perform_get_request(query_url, params)
74
52
  end
75
53
 
76
54
  def protected_login(username, password, params={})
77
55
  params.merge!(content_type: :json, accept: :json)
78
- url = "#{API_URL}/account/login"
56
+ url = "#{API_URL}/account/login"
79
57
  login = { 'username' => username, 'password' => password }.to_json
80
58
 
81
59
  resp = RestClient.post(url, login, params) do |response|
@@ -87,11 +65,13 @@ module Openlibrary
87
65
  raise Openlibrary::Unauthorized
88
66
  when 404
89
67
  raise Openlibrary::NotFound
68
+ when 302
69
+ raise Openlibrary::Redirect
90
70
  end
91
71
  end
92
72
  end
93
73
 
94
- # Update an Open Library object
74
+ # Update an Open Library object
95
75
  #
96
76
  # key - Object key the Open Library resource
97
77
  # cookie - Cookie retrieved by the login method
@@ -100,31 +80,29 @@ module Openlibrary
100
80
  #
101
81
  def update(key, cookie, update, comment, params={})
102
82
  cookie_header = { 'Cookie' => cookie }
103
- comment_header = {
83
+ comment_header = {
104
84
  'Opt' => '"http://openlibrary.org/dev/docs/api"; ns=42',
105
- '42-comment' => comment
85
+ '42-comment' => comment
106
86
  }
107
87
  params.merge!(cookie_header)
108
88
  params.merge!(comment_header)
109
89
  params.merge!(content_type: :json, accept: :json)
110
- url = "#{API_URL}#{key}"
90
+ update_url = "#{API_URL}#{key}"
111
91
 
112
- resp = RestClient.put(url, update, params) do |response, request, result, &block|
113
- case response.code
114
- when 200
115
- response.return!(request, result, &block)
116
- when 401
117
- raise Openlibrary::Unauthorized
118
- when 404
119
- raise Openlibrary::NotFound
120
- end
121
- end
122
- parse(resp)
92
+ response = RestClient.put(update_url, update, params, &HANDLE_REST_CLIENT_RESPONSE)
93
+ parse(response)
123
94
  end
124
95
 
125
- def parse(resp)
126
- object = JSON.parse(resp.body)
127
- object
96
+ def parse(response)
97
+ JSON.parse(response.body)
98
+ end
99
+ private :parse
100
+
101
+ def perform_get_request(url, params)
102
+ params.merge!(accept: :json)
103
+ response = RestClient.get(url, params, &HANDLE_REST_CLIENT_RESPONSE)
104
+ parse(response)
128
105
  end
106
+ private :perform_get_request
129
107
  end
130
108
  end
@@ -1,3 +1,3 @@
1
1
  module Openlibrary
2
- VERSION = "2.1.2"
2
+ VERSION = "2.1.3"
3
3
  end
@@ -44,8 +44,8 @@ describe 'Client' do
44
44
  book.subjects[1].should eq 'First loves -- Fiction'
45
45
  book.subjects[0].should eq 'Traffic accidents -- Fiction'
46
46
 
47
- # Because of a conflict with the internal `key?` method of
48
- # Hashie::Mash, any key actually named 'key' must be referenced
47
+ # Because of a conflict with the internal `key?` method of
48
+ # Hashie::Mash, any key actually named 'key' must be referenced
49
49
  # with a bang (!) to get the value.
50
50
  book.key!.should eq '/books/OL23109860M'
51
51
  book.languages[0].key!.should eq '/languages/eng'
@@ -56,7 +56,7 @@ describe 'Client' do
56
56
  before do
57
57
  isbn_10 = '046503912X'
58
58
  type = '/type/edition'
59
- stub_get("/query.json?type=#{type}&isbn_10=#{isbn_10}",
59
+ stub_get("/query.json?type=#{type}&isbn_10=#{isbn_10}",
60
60
  'book_by_isbn.json')
61
61
  end
62
62
  it 'returns array of books' do
@@ -66,7 +66,7 @@ describe 'Client' do
66
66
 
67
67
  books = client.book_by_isbn('046503912X')
68
68
 
69
- books.should be_a Array
69
+ books.should be_a Array
70
70
  books[0].should be_a Hash
71
71
 
72
72
  books[0]['key'].should eq '/books/OL6807502M'
@@ -115,7 +115,7 @@ describe 'Client' do
115
115
 
116
116
  it 'returns author details' do
117
117
  expect { client.author('OL1A') }.not_to raise_error
118
-
118
+
119
119
  author = client.author('OL1A')
120
120
 
121
121
  author.should be_a Hashie::Mash
@@ -128,8 +128,8 @@ describe 'Client' do
128
128
  author.id.should eq 97
129
129
  author.revision.should eq 6
130
130
 
131
- # Because of a conflict with the internal `key?` method of
132
- # Hashie::Mash, any key actually named 'key' must be referenced
131
+ # Because of a conflict with the internal `key?` method of
132
+ # Hashie::Mash, any key actually named 'key' must be referenced
133
133
  # with a bang (!) to get the value.
134
134
  author.key!.should eq '/authors/OL1A'
135
135
  end
@@ -219,7 +219,7 @@ describe 'Client' do
219
219
 
220
220
  describe '#login' do
221
221
  before do
222
- stub_http_request(:post, "openlibrary.org/account/login").
222
+ stub_http_request(:post, "https://openlibrary.org/account/login").
223
223
  with( body: "{\"username\":\"username\",\"password\":\"password\"}" ).
224
224
  to_return( status: 200, headers: {'Set-Cookie' => 'session=cookie'} )
225
225
  end
@@ -51,3 +51,19 @@ describe 'Openlibrary::Data' do
51
51
  it { should respond_to(:pages) }
52
52
  it { should respond_to(:weight) }
53
53
  end
54
+
55
+ describe 'Openlibrary::Details' do
56
+ before do
57
+ @book_details = Openlibrary::Details.new
58
+ end
59
+
60
+ subject { @book_details }
61
+
62
+ it { should_not respond_to(:some_random_thing) }
63
+
64
+ it { should respond_to(:info_url) }
65
+ it { should respond_to(:bib_key) }
66
+ it { should respond_to(:preview_url) }
67
+ it { should respond_to(:thumbnail_url) }
68
+ it { should respond_to(:details) }
69
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openlibrary
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Fajardo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-08 00:00:00.000000000 Z
11
+ date: 2014-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -158,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
158
  version: '0'
159
159
  requirements: []
160
160
  rubyforge_project: openlibrary
161
- rubygems_version: 2.0.3
161
+ rubygems_version: 2.2.2
162
162
  signing_key:
163
163
  specification_version: 4
164
164
  summary: Ruby Interface for the Openlibrary.org API