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: ea05eb23e2ac23b7a3498d463d5ad258e83a02ac
4
- data.tar.gz: bdcbfbceb2961c96a35bc83d4bf692b1ff40678a
3
+ metadata.gz: 447bd3f0214fb7725a8df7ce8f3394a410404b8c
4
+ data.tar.gz: 6f9ea9cb5b717c91fd354fa449d686e3b347b963
5
5
  SHA512:
6
- metadata.gz: 319d83fdfd80b0510cb23c6a6003bcb32b57f30e886a7af6a58b4cb8309e91ef5caa637339842d6a2d45566d971ad24f6211df088d513fe3fa3936b2aa742963
7
- data.tar.gz: 259b56dd3e38712a54ccc88a32c90eaf2de3f40e7f84e6e614f025eb76d4ebbc6932db664f53ccf08e8c6f357934a7e93797b0f5d44d4be78865018a4ef821d6
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 }
@@ -26,7 +26,7 @@ module RakutenWebService
26
26
  end
27
27
 
28
28
  def search(options)
29
- SearchResult.new(options, self, client)
29
+ SearchResult.new(options, self)
30
30
  end
31
31
 
32
32
  def resource_name
@@ -2,35 +2,24 @@ module RakutenWebService
2
2
  class SearchResult
3
3
  include Enumerable
4
4
 
5
- def initialize(params, resource_class, client)
5
+ def initialize(params, resource_class)
6
6
  @params = params.dup
7
7
  @resource_class = resource_class
8
- @client = client
8
+ @client = RakutenWebService::Client.new(@resource_class.endpoint)
9
9
  end
10
10
 
11
11
  def each
12
- if @results
13
- @results.each do |item|
14
- yield item
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
- if response.body['page'] && response.body['page'] < response.body['pageCount']
28
- response = query(params.merge('page' => response.body['page'] + 1))
29
- else
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, @client)
50
+ self.class.new(new_params, @resource_class)
58
51
  end
59
52
 
60
53
  private
61
54
  def query(params=nil)
62
- @client.get(params || @params)
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)
@@ -1,3 +1,3 @@
1
1
  module RakutenWebService
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.3.1".freeze
3
3
  end
@@ -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.0
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-03 00:00:00.000000000 Z
11
+ date: 2013-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday