rakuten_web_service 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 447bd3f0214fb7725a8df7ce8f3394a410404b8c
|
4
|
+
data.tar.gz: 6f9ea9cb5b717c91fd354fa449d686e3b347b963
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9047d70a6e4e987acca0e86960337060e08654d11334bef714807f3d61a1f038e937c9ac2b94ddec066be3dae7e357b61a8775b50085a96fdd5d3fd4ff856f48
|
7
|
+
data.tar.gz: 54cfc3085669aad2165f5d8de9c39e42efc9d10140026662dcee750ed2bb28e155a92ec30b517ca825a1857a289f5d7217e7cff82bb7545024c7dc35e18dba50
|
@@ -1,6 +1,10 @@
|
|
1
1
|
module RakutenWebService
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :application_id, :affiliate_id
|
3
|
+
attr_accessor :application_id, :affiliate_id, :max_retries
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@max_retries = 5
|
7
|
+
end
|
4
8
|
|
5
9
|
def generate_parameters
|
6
10
|
{ :application_id => application_id, :affiliate_id => affiliate_id }
|
@@ -2,35 +2,24 @@ module RakutenWebService
|
|
2
2
|
class SearchResult
|
3
3
|
include Enumerable
|
4
4
|
|
5
|
-
def initialize(params, resource_class
|
5
|
+
def initialize(params, resource_class)
|
6
6
|
@params = params.dup
|
7
7
|
@resource_class = resource_class
|
8
|
-
@client =
|
8
|
+
@client = RakutenWebService::Client.new(@resource_class.endpoint)
|
9
9
|
end
|
10
10
|
|
11
11
|
def each
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
params = @params
|
13
|
+
response = query
|
14
|
+
begin
|
15
|
+
resources = @resource_class.parse_response(response.body)
|
16
|
+
resources.each do |resource|
|
17
|
+
yield resource
|
15
18
|
end
|
16
|
-
else
|
17
|
-
@results = []
|
18
|
-
params = @params
|
19
|
-
response = query
|
20
|
-
begin
|
21
|
-
resources = @resource_class.parse_response(response.body)
|
22
|
-
resources.each do |resource|
|
23
|
-
yield resource
|
24
|
-
@results << resource
|
25
|
-
end
|
26
19
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
response = nil
|
31
|
-
end
|
32
|
-
end while(response)
|
33
|
-
end
|
20
|
+
break unless has_next_page?
|
21
|
+
response = query(params.merge('page' => response.body['page'] + 1))
|
22
|
+
end while(response)
|
34
23
|
end
|
35
24
|
|
36
25
|
def params
|
@@ -38,6 +27,10 @@ module RakutenWebService
|
|
38
27
|
@params.dup
|
39
28
|
end
|
40
29
|
|
30
|
+
def has_next_page?
|
31
|
+
@response.body['page'] && @response.body['page'] < @response.body['pageCount']
|
32
|
+
end
|
33
|
+
|
41
34
|
def order(options)
|
42
35
|
new_params = @params.dup
|
43
36
|
if options.is_a? Hash
|
@@ -54,12 +47,24 @@ module RakutenWebService
|
|
54
47
|
else
|
55
48
|
raise ArgumentError, "Invalid Sort Option: #{options.inspect}"
|
56
49
|
end
|
57
|
-
self.class.new(new_params, @resource_class
|
50
|
+
self.class.new(new_params, @resource_class)
|
58
51
|
end
|
59
52
|
|
60
53
|
private
|
61
54
|
def query(params=nil)
|
62
|
-
|
55
|
+
retries = RakutenWebService.configuration.max_retries
|
56
|
+
begin
|
57
|
+
@response = @client.get(params || @params)
|
58
|
+
rescue RWS::TooManyRequests => e
|
59
|
+
if retries > 0
|
60
|
+
retries -= 1
|
61
|
+
sleep 1
|
62
|
+
retry
|
63
|
+
else
|
64
|
+
raise e
|
65
|
+
end
|
66
|
+
end
|
67
|
+
@response
|
63
68
|
end
|
64
69
|
|
65
70
|
def camelize(str)
|
@@ -74,6 +74,23 @@ describe RakutenWebService::Ichiba::Item do
|
|
74
74
|
expect(@second_request).to have_been_made.once
|
75
75
|
end
|
76
76
|
end
|
77
|
+
|
78
|
+
context 'When TooManyRequest error raised' do
|
79
|
+
let(:client) do
|
80
|
+
c = double('client')
|
81
|
+
c.stub(:get).and_raise(RWS::TooManyRequests)
|
82
|
+
c
|
83
|
+
end
|
84
|
+
|
85
|
+
before do
|
86
|
+
@items.instance_variable_set('@client', client)
|
87
|
+
end
|
88
|
+
|
89
|
+
specify 'retries automatically after sleeping 1 sec' do
|
90
|
+
expect(@items).to receive(:sleep).with(1).exactly(5).times.and_return(*([nil] * 5))
|
91
|
+
expect { @items.first.name }.to raise_error(RakutenWebService::TooManyRequests)
|
92
|
+
end
|
93
|
+
end
|
77
94
|
end
|
78
95
|
end
|
79
96
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rakuten_web_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatsuya Sato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|