goodreads 0.4.3 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -0
- data/.travis.yml +2 -2
- data/Gemfile +2 -2
- data/README.md +18 -18
- data/Rakefile +5 -5
- data/examples/oauth.md +3 -3
- data/goodreads.gemspec +14 -14
- data/lib/goodreads.rb +22 -21
- data/lib/goodreads/client.rb +11 -13
- data/lib/goodreads/client/authorized.rb +1 -3
- data/lib/goodreads/client/authors.rb +5 -5
- data/lib/goodreads/client/books.rb +9 -9
- data/lib/goodreads/client/friends.rb +2 -2
- data/lib/goodreads/client/groups.rb +5 -5
- data/lib/goodreads/client/reviews.rb +12 -13
- data/lib/goodreads/client/shelves.rb +11 -13
- data/lib/goodreads/client/users.rb +3 -3
- data/lib/goodreads/errors.rb +5 -5
- data/lib/goodreads/request.rb +30 -29
- data/lib/goodreads/version.rb +1 -1
- data/spec/authentication_spec.rb +16 -16
- data/spec/client_spec.rb +205 -201
- data/spec/goodreads_spec.rb +31 -31
- data/spec/spec_helper.rb +13 -13
- metadata +8 -8
@@ -1,26 +1,24 @@
|
|
1
1
|
module Goodreads
|
2
2
|
module Shelves
|
3
|
-
|
4
3
|
# Get books from a user's shelf
|
5
|
-
def shelf(user_id, shelf_name, options={})
|
6
|
-
options = options.merge(:
|
4
|
+
def shelf(user_id, shelf_name, options = {})
|
5
|
+
options = options.merge(shelf: shelf_name, v: 2)
|
7
6
|
data = request("/review/list/#{user_id}.xml", options)
|
8
|
-
reviews = data[
|
7
|
+
reviews = data["reviews"]["review"]
|
9
8
|
|
10
9
|
books = []
|
11
10
|
unless reviews.nil?
|
12
11
|
# one-book results come back as a single hash
|
13
|
-
reviews = [reviews]
|
14
|
-
books = reviews.map {|e| Hashie::Mash.new(e)}
|
12
|
+
reviews = [reviews] unless reviews.instance_of?(Array)
|
13
|
+
books = reviews.map { |e| Hashie::Mash.new(e) }
|
15
14
|
end
|
16
15
|
|
17
|
-
Hashie::Mash.new(
|
18
|
-
:
|
19
|
-
:
|
20
|
-
:
|
21
|
-
:
|
22
|
-
|
16
|
+
Hashie::Mash.new(
|
17
|
+
start: data["reviews"]["start"].to_i,
|
18
|
+
end: data["reviews"]["end"].to_i,
|
19
|
+
total: data["reviews"]["total"].to_i,
|
20
|
+
books: books
|
21
|
+
)
|
23
22
|
end
|
24
|
-
|
25
23
|
end
|
26
24
|
end
|
data/lib/goodreads/errors.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Goodreads
|
2
|
-
class Error
|
3
|
-
class ConfigurationError < Error
|
4
|
-
class Forbidden
|
5
|
-
class Unauthorized
|
6
|
-
class NotFound
|
2
|
+
class Error < StandardError; end
|
3
|
+
class ConfigurationError < Error; end
|
4
|
+
class Forbidden < Error; end
|
5
|
+
class Unauthorized < Error; end
|
6
|
+
class NotFound < Error; end
|
7
7
|
end
|
data/lib/goodreads/request.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "active_support/core_ext/hash"
|
2
|
+
require "rest-client"
|
3
|
+
require "hashie"
|
4
4
|
|
5
5
|
module Goodreads
|
6
6
|
module Request
|
7
|
-
API_URL =
|
8
|
-
API_FORMAT =
|
7
|
+
API_URL = "https://www.goodreads.com"
|
8
|
+
API_FORMAT = "xml"
|
9
9
|
|
10
10
|
protected
|
11
11
|
|
@@ -14,26 +14,24 @@ module Goodreads
|
|
14
14
|
# path - Request path
|
15
15
|
# params - Parameters hash
|
16
16
|
#
|
17
|
-
def request(path, params={})
|
17
|
+
def request(path, params = {})
|
18
18
|
token = api_key || Goodreads.configuration[:api_key]
|
19
19
|
|
20
|
-
if token.nil?
|
21
|
-
raise Goodreads::ConfigurationError, 'API key required.'
|
22
|
-
end
|
20
|
+
fail(Goodreads::ConfigurationError, "API key required.") if token.nil?
|
23
21
|
|
24
|
-
params.merge!(:
|
22
|
+
params.merge!(format: API_FORMAT, key: token)
|
25
23
|
url = "#{API_URL}#{path}"
|
26
24
|
|
27
|
-
resp = RestClient.get(url, :
|
25
|
+
resp = RestClient.get(url, params: params) do |response, request, result, &block|
|
28
26
|
case response.code
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
27
|
+
when 200
|
28
|
+
response.return!(&block)
|
29
|
+
when 401
|
30
|
+
fail(Goodreads::Unauthorized)
|
31
|
+
when 403
|
32
|
+
fail(Goodreads::Forbidden)
|
33
|
+
when 404
|
34
|
+
fail(Goodreads::NotFound)
|
37
35
|
end
|
38
36
|
end
|
39
37
|
|
@@ -45,24 +43,27 @@ module Goodreads
|
|
45
43
|
# path - Request path
|
46
44
|
# params - Parameters hash
|
47
45
|
#
|
48
|
-
def oauth_request(path, params=nil)
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
def oauth_request(path, params = nil)
|
47
|
+
fail "OAuth access token required!" unless @oauth_token
|
48
|
+
if params
|
49
|
+
url_params = params.map { |k, v| "#{k}=#{v}" }.join("&")
|
50
|
+
path = "#{path}?#{url_params}"
|
51
|
+
end
|
52
|
+
resp = @oauth_token.get(path, "Accept" => "application/xml")
|
52
53
|
|
53
54
|
case resp
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
when Net::HTTPUnauthorized
|
56
|
+
fail Goodreads::Unauthorized
|
57
|
+
when Net::HTTPNotFound
|
58
|
+
fail Goodreads::NotFound
|
58
59
|
end
|
59
60
|
|
60
61
|
parse(resp)
|
61
62
|
end
|
62
63
|
|
63
64
|
def parse(resp)
|
64
|
-
hash = Hash.from_xml(resp.body)[
|
65
|
-
hash.delete(
|
65
|
+
hash = Hash.from_xml(resp.body)["GoodreadsResponse"]
|
66
|
+
hash.delete("Request")
|
66
67
|
hash
|
67
68
|
end
|
68
69
|
end
|
data/lib/goodreads/version.rb
CHANGED
data/spec/authentication_spec.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe "Client" do
|
4
4
|
before :each do
|
5
5
|
Goodreads.reset_configuration
|
6
6
|
end
|
7
|
-
|
8
|
-
it
|
7
|
+
|
8
|
+
it "raises Goodreads::ConfigurationError if API key was not provided" do
|
9
9
|
client = Goodreads::Client.new
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
|
11
|
+
expect { client.book_by_isbn("0307463745") }
|
12
|
+
.to raise_error(Goodreads::ConfigurationError, "API key required.")
|
13
13
|
end
|
14
|
-
|
15
|
-
it
|
16
|
-
client = Goodreads::Client.new(:
|
17
|
-
|
18
|
-
stub_request(:get, "
|
19
|
-
to_return(:
|
20
|
-
|
21
|
-
|
22
|
-
|
14
|
+
|
15
|
+
it "raises Goodreads::Unauthorized if API key is not valid" do
|
16
|
+
client = Goodreads::Client.new(api_key: "INVALID_KEY")
|
17
|
+
|
18
|
+
stub_request(:get, "https://www.goodreads.com/book/isbn?format=xml&isbn=054748250711&key=INVALID_KEY")
|
19
|
+
.to_return(status: 401)
|
20
|
+
|
21
|
+
expect { client.book_by_isbn("054748250711") }
|
22
|
+
.to raise_error(Goodreads::Unauthorized)
|
23
23
|
end
|
24
24
|
end
|
data/spec/client_spec.rb
CHANGED
@@ -1,333 +1,337 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "spec_helper"
|
2
|
+
require "oauth"
|
3
3
|
|
4
|
-
describe
|
5
|
-
let(:client) { Goodreads::Client.new(:
|
4
|
+
describe "Client" do
|
5
|
+
let(:client) { Goodreads::Client.new(api_key: "SECRET_KEY") }
|
6
6
|
before(:each) { Goodreads.reset_configuration }
|
7
7
|
|
8
|
-
describe
|
9
|
-
it
|
10
|
-
expect { Goodreads::Client.new(nil) }
|
11
|
-
to raise_error
|
8
|
+
describe "#new" do
|
9
|
+
it "requires an argument" do
|
10
|
+
expect { Goodreads::Client.new(nil) }
|
11
|
+
.to raise_error(ArgumentError, "Options hash required.")
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
15
|
-
expect { Goodreads::Client.new(
|
16
|
-
to raise_error
|
14
|
+
it "requires a hash argument" do
|
15
|
+
expect { Goodreads::Client.new("foo") }
|
16
|
+
.to raise_error(ArgumentError, "Options hash required.")
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
describe
|
21
|
-
before { stub_with_key_get(
|
20
|
+
describe "#book_by_isbn" do
|
21
|
+
before { stub_with_key_get("/book/isbn", { isbn: "0307463745" }, "book.xml") }
|
22
22
|
|
23
|
-
it
|
24
|
-
book = client.book_by_isbn(
|
23
|
+
it "returns a book by isbn" do
|
24
|
+
book = client.book_by_isbn("0307463745")
|
25
25
|
|
26
|
-
book.
|
27
|
-
book.
|
26
|
+
expect(book).to respond_to :id
|
27
|
+
expect(book).to respond_to :title
|
28
28
|
end
|
29
29
|
|
30
|
-
context
|
30
|
+
context "when book does not exist" do
|
31
31
|
before do
|
32
|
-
stub_request(:get, "
|
33
|
-
to_return(:
|
32
|
+
stub_request(:get, "https://www.goodreads.com/book/isbn?format=xml&isbn=123456789&key=SECRET_KEY")
|
33
|
+
.to_return(status: 404, body: "", headers: {})
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
37
|
-
expect { client.book_by_isbn(
|
36
|
+
it "raises Goodreads::NotFound" do
|
37
|
+
expect { client.book_by_isbn("123456789") }.to raise_error(Goodreads::NotFound)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
describe
|
43
|
-
before { stub_with_key_get(
|
42
|
+
describe "#search_books" do
|
43
|
+
before { stub_with_key_get("/search/index", { q: "Rework" }, "search_books_by_name.xml") }
|
44
44
|
|
45
|
-
it
|
46
|
-
result = client.search_books(
|
45
|
+
it "returns book search results" do
|
46
|
+
result = client.search_books("Rework")
|
47
47
|
|
48
|
-
result.
|
49
|
-
result.
|
50
|
-
result.
|
51
|
-
result.
|
52
|
-
result.results.
|
53
|
-
result.query.
|
54
|
-
result.results.work.size.
|
55
|
-
result.results.work.first.id.
|
48
|
+
expect(result).to be_a(Hashie::Mash)
|
49
|
+
expect(result).to respond_to(:query)
|
50
|
+
expect(result).to respond_to(:total_results)
|
51
|
+
expect(result).to respond_to(:results)
|
52
|
+
expect(result.results).to respond_to(:work)
|
53
|
+
expect(result.query).to eq("Rework")
|
54
|
+
expect(result.results.work.size).to eq(3)
|
55
|
+
expect(result.results.work.first.id).to eq(6_928_276)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
describe
|
60
|
-
before { stub_with_key_get(
|
59
|
+
describe "#book" do
|
60
|
+
before { stub_with_key_get("/book/show", { id: "6732019" }, "book.xml") }
|
61
61
|
|
62
|
-
it
|
63
|
-
expect { client.book(
|
62
|
+
it "returns a book by goodreads id" do
|
63
|
+
expect { client.book("6732019") }.not_to raise_error
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
describe
|
68
|
-
before { stub_with_key_get(
|
67
|
+
describe "#book_by_title" do
|
68
|
+
before { stub_with_key_get("/book/title", { title: "Rework" }, "book.xml") }
|
69
69
|
|
70
|
-
it
|
71
|
-
expect { client.book_by_title(
|
70
|
+
it "returns a book by title" do
|
71
|
+
expect { client.book_by_title("Rework") }.not_to raise_error
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
|
-
describe
|
76
|
-
before { stub_with_key_get(
|
75
|
+
describe "#recent_reviews" do
|
76
|
+
before { stub_with_key_get("/review/recent_reviews", {}, "recent_reviews.xml") }
|
77
77
|
|
78
|
-
it
|
78
|
+
it "returns recent reviews" do
|
79
79
|
reviews = client.recent_reviews
|
80
80
|
|
81
|
-
reviews.
|
82
|
-
reviews.
|
83
|
-
reviews.first.
|
81
|
+
expect(reviews).to be_an(Array)
|
82
|
+
expect(reviews).to_not be_empty
|
83
|
+
expect(reviews.first).to respond_to(:id)
|
84
84
|
end
|
85
85
|
|
86
|
-
context
|
87
|
-
before { stub_with_key_get(
|
88
|
-
|
89
|
-
it
|
90
|
-
reviews = client.recent_reviews(:
|
91
|
-
reviews.
|
92
|
-
reviews.
|
86
|
+
context "with skip_cropped: true" do
|
87
|
+
before { stub_with_key_get("/review/recent_reviews", {}, "recent_reviews.xml") }
|
88
|
+
|
89
|
+
it "returns only full reviews" do
|
90
|
+
reviews = client.recent_reviews(skip_cropped: true)
|
91
|
+
expect(reviews).to be_an(Array)
|
92
|
+
expect(reviews).to_not be_empty
|
93
93
|
end
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
-
describe
|
98
|
-
before { stub_with_key_get(
|
97
|
+
describe "#review" do
|
98
|
+
before { stub_with_key_get("/review/show", { id: "166204831" }, "review.xml") }
|
99
99
|
|
100
|
-
it
|
101
|
-
review = client.review(
|
100
|
+
it "returns review details" do
|
101
|
+
review = client.review("166204831")
|
102
102
|
|
103
|
-
review.
|
104
|
-
review.id.
|
103
|
+
expect(review).to be_a(Hashie::Mash)
|
104
|
+
expect(review.id).to eq("166204831")
|
105
105
|
end
|
106
106
|
|
107
|
-
context
|
107
|
+
context "when review does not exist" do
|
108
108
|
before do
|
109
|
-
stub_request(:get, "
|
110
|
-
to_return(:
|
109
|
+
stub_request(:get, "https://www.goodreads.com/review/show?format=xml&id=12345&key=SECRET_KEY")
|
110
|
+
.to_return(status: 404, body: "", headers: {})
|
111
111
|
end
|
112
112
|
|
113
|
-
it
|
114
|
-
expect { client.review(
|
113
|
+
it "raises Goodreads::NotFound" do
|
114
|
+
expect { client.review("12345") }.to raise_error(Goodreads::NotFound)
|
115
115
|
end
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
-
describe
|
119
|
+
describe "#reviews" do
|
120
120
|
subject { client.reviews(id: user_id, shelf: shelf) }
|
121
|
-
let(:user_id) {
|
122
|
-
let(:shelf) {
|
123
|
-
|
124
|
-
before { stub_with_key_get(
|
125
|
-
let(:response_fixture) {
|
126
|
-
|
127
|
-
it
|
128
|
-
subject.
|
129
|
-
subject.count.
|
130
|
-
subject.first.
|
131
|
-
subject.first.keys.
|
132
|
-
subject.map(&:id).
|
121
|
+
let(:user_id) { "5076380" }
|
122
|
+
let(:shelf) { "to-read" }
|
123
|
+
|
124
|
+
before { stub_with_key_get("/review/list", { v: "2", id: user_id, shelf: shelf }, response_fixture) }
|
125
|
+
let(:response_fixture) { "reviews.xml" }
|
126
|
+
|
127
|
+
it "returns a list of reviews" do
|
128
|
+
expect(subject).to be_an(Array)
|
129
|
+
expect(subject.count).to eq(2)
|
130
|
+
expect(subject.first).to be_a(Hashie::Mash)
|
131
|
+
expect(subject.first.keys).to include(*%w(book rating started_at read_at))
|
132
|
+
expect(subject.map(&:id)).to eq(%w(1371624338 1371623371))
|
133
133
|
end
|
134
134
|
|
135
|
-
context
|
136
|
-
let(:response_fixture) {
|
135
|
+
context "when there are no more reviews" do
|
136
|
+
let(:response_fixture) { "reviews_empty.xml" }
|
137
137
|
|
138
|
-
it
|
139
|
-
subject.
|
140
|
-
subject.count.
|
138
|
+
it "returns an empty array" do
|
139
|
+
expect(subject).to be_an(Array)
|
140
|
+
expect(subject.count).to eq(0)
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
144
|
-
context
|
144
|
+
context "when user does not exist" do
|
145
145
|
before do
|
146
|
-
|
147
|
-
|
146
|
+
url_params = "v=2&format=xml&id=#{user_id}&key=SECRET_KEY&shelf=#{shelf}"
|
147
|
+
stub_request(:get, "https://www.goodreads.com/review/list?#{url_params}")
|
148
|
+
.to_return(status: 404, body: "", headers: {})
|
148
149
|
end
|
149
150
|
|
150
|
-
it
|
151
|
-
expect { subject }.to raise_error
|
151
|
+
it "raises Goodreads::NotFound" do
|
152
|
+
expect { subject }.to raise_error(Goodreads::NotFound)
|
152
153
|
end
|
153
154
|
end
|
154
155
|
end
|
155
156
|
|
156
|
-
describe
|
157
|
-
before { stub_with_key_get(
|
158
|
-
|
159
|
-
it
|
160
|
-
author = client.author(
|
161
|
-
|
162
|
-
author.
|
163
|
-
author.id.
|
164
|
-
author.name.
|
165
|
-
author.link.
|
166
|
-
author.fans_count.
|
167
|
-
author.image_url.
|
168
|
-
author.small_image_url.
|
169
|
-
author.about.
|
170
|
-
author.influences.
|
171
|
-
author.works_count.
|
172
|
-
author.gender.
|
173
|
-
author.hometown.
|
174
|
-
author.born_at.
|
175
|
-
author.died_at.
|
157
|
+
describe "#author" do
|
158
|
+
before { stub_with_key_get("/author/show", { id: "18541" }, "author.xml") }
|
159
|
+
|
160
|
+
it "returns author details" do
|
161
|
+
author = client.author("18541")
|
162
|
+
|
163
|
+
expect(author).to be_a(Hashie::Mash)
|
164
|
+
expect(author.id).to eq("18541")
|
165
|
+
expect(author.name).to eq("Tim O'Reilly")
|
166
|
+
expect(author.link).to eq("http://www.goodreads.com/author/show/18541.Tim_O_Reilly")
|
167
|
+
expect(author.fans_count).to eq(109)
|
168
|
+
expect(author.image_url).to eq("http://photo.goodreads.com/authors/1199698411p5/18541.jpg")
|
169
|
+
expect(author.small_image_url).to eq("http://photo.goodreads.com/authors/1199698411p2/18541.jpg")
|
170
|
+
expect(author.about).to eq("")
|
171
|
+
expect(author.influences).to eq("")
|
172
|
+
expect(author.works_count).to eq("34")
|
173
|
+
expect(author.gender).to eq("male")
|
174
|
+
expect(author.hometown).to eq("Cork")
|
175
|
+
expect(author.born_at).to eq("1954/06/06")
|
176
|
+
expect(author.died_at).to be_nil
|
176
177
|
end
|
177
178
|
|
178
|
-
context
|
179
|
+
context "when author does not exist" do
|
179
180
|
before do
|
180
|
-
stub_request(:get, "
|
181
|
-
to_return(:
|
181
|
+
stub_request(:get, "https://www.goodreads.com/author/show?format=xml&id=12345&key=SECRET_KEY")
|
182
|
+
.to_return(status: 404, body: "", headers: {})
|
182
183
|
end
|
183
184
|
|
184
|
-
it
|
185
|
-
expect { client.author(
|
185
|
+
it "raises Goodreads::NotFound" do
|
186
|
+
expect { client.author("12345") }.to raise_error(Goodreads::NotFound)
|
186
187
|
end
|
187
188
|
end
|
188
189
|
end
|
189
190
|
|
190
|
-
describe
|
191
|
+
describe "#author_by_name" do
|
191
192
|
before do
|
192
|
-
stub_with_key_get(
|
193
|
+
stub_with_key_get("/api/author_url/Orson%20Scott%20Card", { id: "Orson Scott Card" }, "author_by_name.xml")
|
193
194
|
end
|
194
195
|
|
195
|
-
it
|
196
|
-
author = client.author_by_name(
|
196
|
+
it "returns author details" do
|
197
|
+
author = client.author_by_name("Orson Scott Card")
|
197
198
|
|
198
|
-
author.
|
199
|
-
author.id.
|
200
|
-
author.name.
|
201
|
-
author.link.
|
199
|
+
expect(author).to be_a(Hashie::Mash)
|
200
|
+
expect(author.id).to eq("589")
|
201
|
+
expect(author.name).to eq("Orson Scott Card")
|
202
|
+
expect(author.link).to eq("http://www.goodreads.com/author/show/589.Orson_Scott_Card?utm_medium=api&utm_source=author_link")
|
202
203
|
end
|
203
204
|
end
|
204
205
|
|
205
|
-
describe
|
206
|
-
before { stub_with_key_get(
|
206
|
+
describe "#user" do
|
207
|
+
before { stub_with_key_get("/user/show", { id: "878044" }, "user.xml") }
|
207
208
|
|
208
|
-
it
|
209
|
-
user = client.user(
|
209
|
+
it "returns user details" do
|
210
|
+
user = client.user("878044")
|
210
211
|
|
211
|
-
user.
|
212
|
-
user.id.
|
213
|
-
user.name.
|
214
|
-
user.user_name.
|
212
|
+
expect(user).to be_a(Hashie::Mash)
|
213
|
+
expect(user.id).to eq("878044")
|
214
|
+
expect(user.name).to eq("Jan")
|
215
|
+
expect(user.user_name).to eq("janmt")
|
215
216
|
end
|
216
217
|
|
217
|
-
context
|
218
|
+
context "when user does not exist" do
|
218
219
|
before do
|
219
|
-
stub_request(:get, "
|
220
|
-
to_return(:
|
220
|
+
stub_request(:get, "https://www.goodreads.com/user/show?format=xml&id=12345&key=SECRET_KEY")
|
221
|
+
.to_return(status: 404, body: "", headers: {})
|
221
222
|
end
|
222
223
|
|
223
|
-
it
|
224
|
-
expect { client.user(
|
224
|
+
it "raises Goodreads::NotFound" do
|
225
|
+
expect { client.user("12345") }.to raise_error(Goodreads::NotFound)
|
225
226
|
end
|
226
227
|
end
|
227
228
|
end
|
228
229
|
|
229
|
-
describe
|
230
|
-
before
|
230
|
+
describe "#friends" do
|
231
|
+
before do
|
232
|
+
allow(client).to receive(:oauth_request)
|
233
|
+
.and_return(Hash.from_xml(fixture("friends.xml"))["GoodreadsResponse"])
|
234
|
+
end
|
231
235
|
|
232
|
-
it
|
233
|
-
friends = client.friends(
|
236
|
+
it "returns friend details" do
|
237
|
+
friends = client.friends("878044")
|
234
238
|
|
235
|
-
friends.
|
236
|
-
friends.
|
237
|
-
friends.user.size.
|
238
|
-
friends.user.first.
|
239
|
+
expect(friends).to be_an_instance_of(Hashie::Mash)
|
240
|
+
expect(friends).to respond_to(:user)
|
241
|
+
expect(friends.user.size).to eq(friends.end.to_i)
|
242
|
+
expect(friends.user.first).to respond_to(:name)
|
239
243
|
end
|
240
244
|
end
|
241
245
|
|
242
|
-
describe
|
246
|
+
describe "#shelf" do
|
243
247
|
it "returns list of books for a user's specified shelf" do
|
244
|
-
stub_with_key_get(
|
248
|
+
stub_with_key_get("/review/list/1.xml", { shelf: "to-read", v: "2" }, "to-read.xml")
|
245
249
|
|
246
|
-
shelf = client.shelf(
|
250
|
+
shelf = client.shelf("1", "to-read")
|
247
251
|
|
248
|
-
shelf.
|
249
|
-
shelf.
|
250
|
-
shelf.
|
251
|
-
shelf.
|
252
|
+
expect(shelf).to respond_to(:start)
|
253
|
+
expect(shelf).to respond_to(:end)
|
254
|
+
expect(shelf).to respond_to(:total)
|
255
|
+
expect(shelf).to respond_to(:books)
|
252
256
|
|
253
|
-
shelf.start.
|
254
|
-
shelf.end.
|
255
|
-
shelf.total.
|
256
|
-
shelf.books.length.
|
257
|
-
shelf.books.first.id.
|
258
|
-
shelf.books.first.book.title.strip.
|
257
|
+
expect(shelf.start).to eq(1)
|
258
|
+
expect(shelf.end).to eq(20)
|
259
|
+
expect(shelf.total).to eq(40)
|
260
|
+
expect(shelf.books.length).to eq(20)
|
261
|
+
expect(shelf.books.first.id).to eq("45590939")
|
262
|
+
expect(shelf.books.first.book.title.strip).to eq("The Demon-Haunted World: Science as a Candle in the Dark")
|
259
263
|
end
|
260
264
|
|
261
265
|
it "paginates book lists from a user's shelf" do
|
262
|
-
stub_with_key_get(
|
266
|
+
stub_with_key_get("/review/list/1.xml", { shelf: "to-read", v: "2", page: "2" }, "to-read-p2.xml")
|
263
267
|
|
264
|
-
shelf = client.shelf(
|
268
|
+
shelf = client.shelf("1", "to-read", page: 2)
|
265
269
|
|
266
|
-
shelf.start.
|
267
|
-
shelf.end.
|
268
|
-
shelf.total.
|
269
|
-
shelf.books.length.
|
270
|
-
shelf.books.first.id.
|
271
|
-
shelf.books.first.book.title.
|
270
|
+
expect(shelf.start).to eq(21)
|
271
|
+
expect(shelf.end).to eq(40)
|
272
|
+
expect(shelf.total).to eq(40)
|
273
|
+
expect(shelf.books.length).to eq(20)
|
274
|
+
expect(shelf.books.first.id).to eq("107804211")
|
275
|
+
expect(shelf.books.first.book.title).to match(/Your Money or Your Life/)
|
272
276
|
end
|
273
277
|
|
274
278
|
it "returns an empty array when shelf is empty" do
|
275
|
-
stub_with_key_get(
|
279
|
+
stub_with_key_get("/review/list/1.xml", { shelf: "to-read", v: "2" }, "empty.xml")
|
276
280
|
|
277
|
-
shelf = client.shelf(
|
281
|
+
shelf = client.shelf("1", "to-read")
|
278
282
|
|
279
|
-
shelf.start.
|
280
|
-
shelf.end.
|
281
|
-
shelf.total.
|
282
|
-
shelf.books.length.
|
283
|
+
expect(shelf.start).to eq(0)
|
284
|
+
expect(shelf.end).to eq(0)
|
285
|
+
expect(shelf.total).to eq(0)
|
286
|
+
expect(shelf.books.length).to eq(0)
|
283
287
|
end
|
284
288
|
end
|
285
289
|
|
286
|
-
describe
|
287
|
-
let(:consumer) { OAuth::Consumer.new(
|
288
|
-
let(:token) { OAuth::AccessToken.new(consumer,
|
290
|
+
describe "#user_id" do
|
291
|
+
let(:consumer) { OAuth::Consumer.new("API_KEY", "SECRET_KEY", site: "https://www.goodreads.com") }
|
292
|
+
let(:token) { OAuth::AccessToken.new(consumer, "ACCESS_TOKEN", "ACCESS_SECRET") }
|
289
293
|
|
290
294
|
before do
|
291
|
-
stub_request(:get, "
|
292
|
-
to_return(:
|
295
|
+
stub_request(:get, "https://www.goodreads.com/api/auth_user")
|
296
|
+
.to_return(status: 200, body: fixture("oauth_response.xml"), headers: {})
|
293
297
|
end
|
294
298
|
|
295
|
-
it
|
296
|
-
client = Goodreads::Client.new(:
|
297
|
-
client.user_id.
|
299
|
+
it "returns id of the user with oauth authentication" do
|
300
|
+
client = Goodreads::Client.new(api_key: "SECRET_KEY", oauth_token: token)
|
301
|
+
expect(client.user_id).to eq("2003928")
|
298
302
|
end
|
299
303
|
end
|
300
304
|
|
301
|
-
describe
|
302
|
-
before { stub_with_key_get(
|
305
|
+
describe "#group" do
|
306
|
+
before { stub_with_key_get("/group/show", { id: "1" }, "group.xml") }
|
303
307
|
|
304
308
|
it "returns group details" do
|
305
|
-
group = client.group(
|
306
|
-
|
307
|
-
group.
|
308
|
-
group.id.
|
309
|
-
group.title.
|
310
|
-
group.access.
|
311
|
-
group.location.
|
312
|
-
group.category.
|
313
|
-
group.subcategory.
|
314
|
-
group.group_users_count.
|
309
|
+
group = client.group("1")
|
310
|
+
|
311
|
+
expect(group).to be_a(Hashie::Mash)
|
312
|
+
expect(group.id).to eq("1")
|
313
|
+
expect(group.title).to eq("Goodreads Feedback")
|
314
|
+
expect(group.access).to eq("public")
|
315
|
+
expect(group.location).to eq("")
|
316
|
+
expect(group.category).to eq("Business")
|
317
|
+
expect(group.subcategory).to eq("Companies")
|
318
|
+
expect(group.group_users_count).to eq("10335")
|
315
319
|
end
|
316
320
|
end
|
317
321
|
|
318
|
-
describe
|
319
|
-
before { stub_with_key_get(
|
322
|
+
describe "#group_list" do
|
323
|
+
before { stub_with_key_get("/group/list", { id: "1", sort: "my_activity" }, "group_list.xml") }
|
320
324
|
|
321
325
|
it "returns groups a given user is a member of" do
|
322
|
-
group_list = client.group_list(
|
323
|
-
|
324
|
-
group_list.
|
325
|
-
group_list.total.
|
326
|
-
group_list.group.count.
|
327
|
-
group_list.group[0].id.
|
328
|
-
group_list.group[0].title.
|
329
|
-
group_list.group[1].id.
|
330
|
-
group_list.group[2].users_count.
|
326
|
+
group_list = client.group_list("1")
|
327
|
+
|
328
|
+
expect(group_list).to be_a(Hashie::Mash)
|
329
|
+
expect(group_list.total).to eq("107")
|
330
|
+
expect(group_list.group.count).to eq(50)
|
331
|
+
expect(group_list.group[0].id).to eq("1")
|
332
|
+
expect(group_list.group[0].title).to eq("Goodreads Feedback")
|
333
|
+
expect(group_list.group[1].id).to eq("220")
|
334
|
+
expect(group_list.group[2].users_count).to eq("530")
|
331
335
|
end
|
332
336
|
end
|
333
337
|
end
|