sucker 1.2.0 → 1.3.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.md +45 -20
- data/lib/sucker/parameters.rb +11 -7
- data/lib/sucker/parameters.rbc +723 -0
- data/lib/sucker/request.rb +52 -154
- data/lib/sucker/request.rbc +2719 -0
- data/lib/sucker/response.rb +14 -36
- data/lib/sucker/response.rbc +1307 -0
- data/lib/sucker/version.rb +3 -1
- data/lib/sucker/version.rbc +130 -0
- data/lib/sucker.rb +4 -2
- data/lib/sucker.rbc +286 -0
- data/spec/fixtures/cassette_library/0394751221.yml +26 -0
- data/spec/fixtures/cassette_library/0415246334.yml +26 -0
- data/spec/fixtures/cassette_library/0679753354.yml +26 -0
- data/spec/fixtures/cassette_library/0816614024-0007218095.yml +26 -0
- data/spec/fixtures/cassette_library/0816614024-0143105825.yml +26 -0
- data/spec/fixtures/cassette_library/0816614024.yml +26 -0
- data/spec/fixtures/cassette_library/482224816x.yml +26 -0
- data/spec/fixtures/cassette_library/816614024.yml +151 -0
- data/spec/fixtures/cassette_library/a2h6nh4sqyfz4m.yml +26 -0
- data/spec/fixtures/cassette_library/a2jyso6w6kep83.yml +26 -0
- data/spec/fixtures/cassette_library/author-lacan-or-deleuze-and-not-fiction.yml +26 -0
- data/spec/fixtures/cassette_library/b000aspues.yml +26 -0
- data/spec/fixtures/cassette_library/deleuze-binding-kindle.yml +26 -0
- data/spec/fixtures/cassette_library/deleuze.yml +26 -0
- data/spec/fixtures/cassette_library/george-orwell.yml +26 -0
- data/spec/fixtures/cassette_library/spec/sucker/request.yml +4 -4
- data/spec/fixtures/cassette_library/spec/sucker/response.yml +3 -3
- data/spec/spec_helper.rbc +230 -0
- data/spec/sucker/parameters_spec.rbc +1476 -0
- data/spec/sucker/request_spec.rb +34 -217
- data/spec/sucker/request_spec.rbc +2473 -0
- data/spec/sucker/response_spec.rb +46 -95
- data/spec/sucker/response_spec.rbc +2854 -0
- data/spec/sucker_spec.rbc +287 -0
- data/spec/support/amazon_credentials.rbc +154 -0
- data/spec/support/asins.rbc +335 -0
- data/spec/support/vcr.rbc +356 -0
- metadata +72 -96
- data/CHANGELOG.md +0 -47
@@ -1,160 +1,111 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
3
4
|
|
4
5
|
module Sucker
|
5
6
|
describe Response do
|
6
|
-
use_vcr_cassette
|
7
|
+
use_vcr_cassette 'spec/sucker/response', :record => :new_episodes
|
7
8
|
|
8
|
-
let(:asins) { [
|
9
|
+
let(:asins) { ['0816614024', '0143105825'] }
|
9
10
|
|
10
11
|
let(:response) do
|
11
12
|
worker = Sucker.new(
|
12
|
-
:locale =>
|
13
|
-
:key => amazon[
|
14
|
-
:secret => amazon[
|
13
|
+
:locale => :us,
|
14
|
+
:key => amazon['key'],
|
15
|
+
:secret => amazon['secret'])
|
15
16
|
worker << {
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
'Operation' => 'ItemLookup',
|
18
|
+
'IdType' => 'ASIN',
|
19
|
+
'ItemId' => asins }
|
19
20
|
worker.get
|
20
21
|
end
|
21
22
|
|
22
|
-
describe
|
23
|
-
it
|
23
|
+
describe '.new' do
|
24
|
+
it 'initializes the response body' do
|
24
25
|
response.body.should be_an_instance_of String
|
25
26
|
end
|
26
27
|
|
27
|
-
it
|
28
|
-
response.code.should == 200
|
29
|
-
end
|
30
|
-
|
31
|
-
it "initializes the response time" do
|
32
|
-
response.time.should be_an_instance_of Float
|
33
|
-
end
|
34
|
-
|
35
|
-
it "initializes the request URI" do
|
36
|
-
response.uri.should be_an_instance_of String
|
28
|
+
it 'initializes the response code' do
|
29
|
+
response.code.should == '200'
|
37
30
|
end
|
38
31
|
end
|
39
32
|
|
40
|
-
|
41
|
-
context "when a block is given" do
|
42
|
-
it "yields each match to a block" do
|
43
|
-
has_yielded = false
|
44
|
-
|
45
|
-
response.each("ItemAttributes") do |item|
|
46
|
-
has_yielded = true
|
47
|
-
item.should be_an_instance_of Hash
|
48
|
-
end
|
49
|
-
|
50
|
-
has_yielded.should be_true
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context "when no block is given" do
|
55
|
-
it "raises error" do
|
56
|
-
expect do
|
57
|
-
response.each("ItemAttributes")
|
58
|
-
end.to raise_error(LocalJumpError)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context "when response contains errors" do
|
33
|
+
context 'when response contains errors' do
|
64
34
|
before do
|
65
35
|
response.body = "<?xml version=\"1.0\" ?><ItemLookupResponse xmlns=\"http://webservices.amazon.com/AWSECommerceService/2010-09-01\"><Errors><Error>foo</Error><Error>bar</Error></Errors>"
|
66
36
|
end
|
67
37
|
|
68
|
-
describe
|
69
|
-
it
|
38
|
+
describe '#errors' do
|
39
|
+
it 'returns an array of errors' do
|
70
40
|
response.errors.count.should eql 2
|
71
41
|
end
|
72
42
|
end
|
73
43
|
|
74
|
-
describe
|
75
|
-
it
|
44
|
+
describe '#has_errors?' do
|
45
|
+
it 'returns true if the response has errors' do
|
76
46
|
response.should have_errors
|
77
47
|
end
|
78
48
|
end
|
79
49
|
end
|
80
50
|
|
81
|
-
describe
|
82
|
-
context
|
83
|
-
|
84
|
-
|
85
|
-
response.find("ASIN").should eql asins
|
51
|
+
describe '#find' do
|
52
|
+
context 'when there are matches' do
|
53
|
+
it 'returns an array of matching nodes' do
|
54
|
+
response.find('ASIN').should eql asins
|
86
55
|
end
|
87
56
|
end
|
88
57
|
|
89
|
-
context
|
90
|
-
it
|
91
|
-
node = response.find(
|
58
|
+
context 'when there are no matches' do
|
59
|
+
it 'returns an empty array' do
|
60
|
+
node = response.find('Foo')
|
92
61
|
node.should eql []
|
93
62
|
end
|
94
63
|
end
|
95
64
|
end
|
96
65
|
|
97
|
-
describe
|
98
|
-
|
99
|
-
it "yields each match to a block and maps returned values" do
|
100
|
-
titles = response.map("ItemAttributes") { |item| item["Title"] }
|
101
|
-
|
102
|
-
titles.count.should eql 2
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
context "when no block is given" do
|
107
|
-
it "raises error" do
|
108
|
-
expect do
|
109
|
-
response.map("ItemAttributes")
|
110
|
-
end.to raise_error(LocalJumpError)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
describe "#to_hash" do
|
116
|
-
it "returns a hash" do
|
66
|
+
describe '#to_hash' do
|
67
|
+
it 'returns a hash' do
|
117
68
|
response.to_hash.should be_an_instance_of Hash
|
118
69
|
end
|
119
70
|
|
120
|
-
it
|
121
|
-
response.body =
|
122
|
-
response.to_hash[
|
71
|
+
it 'converts a content hash to string' do
|
72
|
+
response.body = '<book><title>A Thousand Plateaus</title></book>'
|
73
|
+
response.to_hash['book']['title'].should be_an_instance_of String
|
123
74
|
end
|
124
75
|
|
125
|
-
it
|
76
|
+
it 'renders French' do
|
126
77
|
response.body = "<Title>L'archéologie du savoir</Title>"
|
127
|
-
response.to_hash[
|
78
|
+
response.to_hash['Title'].should eql "L'archéologie du savoir"
|
128
79
|
end
|
129
80
|
|
130
|
-
it
|
131
|
-
response.body =
|
132
|
-
response.to_hash[
|
81
|
+
it 'renders German' do
|
82
|
+
response.body = '<Title>Kafka: Für eine kleine Literatur</Title>'
|
83
|
+
response.to_hash['Title'].should eql 'Kafka: Für eine kleine Literatur'
|
133
84
|
end
|
134
85
|
|
135
|
-
it
|
136
|
-
response.body =
|
137
|
-
response.to_hash[
|
86
|
+
it 'renders Japanese' do
|
87
|
+
response.body = '<Title>スティーブ・ジョブズ 驚異のプレゼン―人々を惹きつける18の法則</Title>'
|
88
|
+
response.to_hash['Title'].should eql 'スティーブ・ジョブズ 驚異のプレゼン―人々を惹きつける18の法則'
|
138
89
|
end
|
139
90
|
end
|
140
91
|
|
141
|
-
describe
|
142
|
-
context
|
143
|
-
it
|
92
|
+
describe '#valid?' do
|
93
|
+
context 'when HTTP status is OK' do
|
94
|
+
it 'returns true' do
|
144
95
|
response.should be_valid
|
145
96
|
end
|
146
97
|
end
|
147
98
|
|
148
|
-
context
|
149
|
-
it
|
99
|
+
context 'when HTTP status is not OK' do
|
100
|
+
it 'returns false' do
|
150
101
|
response.code = 403
|
151
102
|
response.should_not be_valid
|
152
103
|
end
|
153
104
|
end
|
154
105
|
end
|
155
106
|
|
156
|
-
describe
|
157
|
-
it
|
107
|
+
describe '#xml' do
|
108
|
+
it 'returns a Nokogiri document' do
|
158
109
|
response.xml.should be_an_instance_of Nokogiri::XML::Document
|
159
110
|
end
|
160
111
|
end
|