rakuten_web_service 0.6.0 → 0.6.1

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: b7bf83276b1e14274a89e838b6e2905d7624966e
4
- data.tar.gz: 47dc5c14d88984c92a574541f2e6a80c80ff86cd
3
+ metadata.gz: 1e64f47f1cefec66d96a51660fc6dae7bdc21d8b
4
+ data.tar.gz: 2d5f161d80bb6c3430a4e49f3d5ee24800f2e2eb
5
5
  SHA512:
6
- metadata.gz: cdf6daa1da7d1dc16c6f7cc5dcf3f66d8e8a22473e7a67bbe064eb3eada58a953aa58bb82f576ff10b3ee10ba4346248798a875448b77e3fb0cfee003866c7bd
7
- data.tar.gz: 3e92ef4587d84890c9556140d40cda7d6709d4feb0f2ae377130610e35eeac67e4f5bf6bfd5c41aaebf18f79cd4bd4f59dd5cb03bb5a76ea11425814259c26b4
6
+ metadata.gz: 715943fd5be6a9f8444802de80c8a76fd44ac88b95edb314812ad0daeaf326c324992c9471967191dda865dec13b34eb541037becdb4ac1cc9f25c8b6df06644
7
+ data.tar.gz: 28db488e9f73ff71be9690669107181aa8ed36757688754ce7843d3ab3cb4dd492a4cf4a6f9aeda08a2a16ec6d6ccd806fff464f544bdf29f76f3a8a5ccc8c9a
@@ -1,6 +1,8 @@
1
1
  require 'faraday'
2
2
  require 'faraday_middleware'
3
3
 
4
+ require 'rakuten_web_service/response'
5
+
4
6
  module RakutenWebService
5
7
  class WrongParameter < StandardError; end
6
8
  class NotFound < StandardError; end
@@ -11,8 +13,9 @@ module RakutenWebService
11
13
  class Client
12
14
  attr_reader :url, :path
13
15
 
14
- def initialize(endpoint)
15
- url = URI.parse(endpoint)
16
+ def initialize(resource_class)
17
+ @resource_class = resource_class
18
+ url = URI.parse(@resource_class.endpoint)
16
19
  @url = "#{url.scheme}://#{url.host}"
17
20
  @path = url.path
18
21
  end
@@ -23,7 +26,7 @@ module RakutenWebService
23
26
  response = connection.get(path, query)
24
27
  case response.status
25
28
  when 200
26
- return response
29
+ return RakutenWebService::Response.new(@resource_class, response.body)
27
30
  when 400
28
31
  raise WrongParameter, response.body['error_description']
29
32
  when 404
@@ -0,0 +1,39 @@
1
+ module RakutenWebService
2
+ class Response
3
+ include Enumerable
4
+
5
+ def initialize(resource_class, json)
6
+ @resource_class = resource_class
7
+ @json = json.dup
8
+ end
9
+
10
+ def [](key)
11
+ @json[key]
12
+ end
13
+
14
+ def each
15
+ resources.each do |resource|
16
+ yield resource
17
+ end
18
+ end
19
+
20
+ %w[count hits page first last carrier pageCount].each do |name|
21
+ method_name = name.gsub(/([a-z])([A-Z]{1})/) { "#{$1}_#{$2.downcase}" }
22
+ define_method method_name do
23
+ self[name]
24
+ end
25
+ end
26
+
27
+ def resources
28
+ @resources ||= @resource_class.parse_response(@json)
29
+ end
30
+
31
+ def has_next_page?
32
+ page && (not last_page?)
33
+ end
34
+
35
+ def last_page?
36
+ page >= page_count
37
+ end
38
+ end
39
+ end
@@ -5,26 +5,23 @@ module RakutenWebService
5
5
  def initialize(params, resource_class)
6
6
  @params = params.dup
7
7
  @resource_class = resource_class
8
- @client = RakutenWebService::Client.new(@resource_class.endpoint)
8
+ @client = RakutenWebService::Client.new(resource_class)
9
9
  end
10
10
 
11
11
  def fetch_result
12
- response = query
13
- @resource_class.parse_response(response.body)
12
+ query
14
13
  end
15
14
 
16
15
  def each
17
16
  params = @params
18
17
  response = query
19
- begin
20
- resources = @resource_class.parse_response(response.body)
21
- resources.each do |resource|
18
+ loop do
19
+ response.each do |resource|
22
20
  yield resource
23
21
  end
24
-
25
- break unless has_next_page?
26
- response = query(params.merge('page' => response.body['page'] + 1))
27
- end while(response)
22
+ break unless response.has_next_page?
23
+ response = query(params.merge('page' => response.page + 1))
24
+ end
28
25
  end
29
26
 
30
27
  def params
@@ -32,10 +29,6 @@ module RakutenWebService
32
29
  @params.dup
33
30
  end
34
31
 
35
- def has_next_page?
36
- @response.body['page'] && @response.body['page'] < @response.body['pageCount']
37
- end
38
-
39
32
  def order(options)
40
33
  new_params = @params.dup
41
34
  if options.is_a? Hash
@@ -1,3 +1,3 @@
1
1
  module RakutenWebService
2
- VERSION = "0.6.0".freeze
2
+ VERSION = "0.6.1".freeze
3
3
  end
@@ -3,7 +3,10 @@ require 'rakuten_web_service'
3
3
 
4
4
  describe RakutenWebService::Client do
5
5
  let(:endpoint) { 'http://api.example.com/resources' }
6
- let(:client) { RakutenWebService::Client.new(endpoint) }
6
+ let(:resource_class) do
7
+ double('resource_class', endpoint: endpoint)
8
+ end
9
+ let(:client) { RakutenWebService::Client.new(resource_class) }
7
10
  let(:application_id) { 'default_application_id' }
8
11
  let(:affiliate_id) { 'default_affiliate_id' }
9
12
  let(:expected_query) do
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.6.0
4
+ version: 0.6.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: 2014-09-28 00:00:00.000000000 Z
11
+ date: 2014-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -148,6 +148,7 @@ files:
148
148
  - lib/rakuten_web_service/kobo/ebook.rb
149
149
  - lib/rakuten_web_service/kobo/genre.rb
150
150
  - lib/rakuten_web_service/resource.rb
151
+ - lib/rakuten_web_service/response.rb
151
152
  - lib/rakuten_web_service/search_result.rb
152
153
  - lib/rakuten_web_service/version.rb
153
154
  - rakuten_web_service.gemspec