rakuten_web_service 1.10.0 → 1.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +12 -0
  3. data/.github/workflows/ci.yml +30 -0
  4. data/CHANGELOG.md +103 -48
  5. data/Gemfile +3 -3
  6. data/README.ja.md +118 -48
  7. data/README.md +35 -10
  8. data/examples/ichiba_item_search.rb +1 -1
  9. data/examples/recipe_search.rb +16 -0
  10. data/lib/rakuten_web_service/all_proxy.rb +2 -1
  11. data/lib/rakuten_web_service/books/genre.rb +1 -1
  12. data/lib/rakuten_web_service/books/resource.rb +1 -1
  13. data/lib/rakuten_web_service/books/total.rb +1 -1
  14. data/lib/rakuten_web_service/client.rb +9 -9
  15. data/lib/rakuten_web_service/configuration.rb +0 -16
  16. data/lib/rakuten_web_service/error.rb +5 -0
  17. data/lib/rakuten_web_service/genre.rb +1 -1
  18. data/lib/rakuten_web_service/gora/course.rb +1 -1
  19. data/lib/rakuten_web_service/gora/course_detail.rb +1 -1
  20. data/lib/rakuten_web_service/gora/plan.rb +1 -1
  21. data/lib/rakuten_web_service/ichiba/item.rb +3 -3
  22. data/lib/rakuten_web_service/ichiba/product.rb +1 -1
  23. data/lib/rakuten_web_service/ichiba/ranking.rb +1 -1
  24. data/lib/rakuten_web_service/ichiba/tag.rb +16 -0
  25. data/lib/rakuten_web_service/ichiba/tag_group.rb +23 -0
  26. data/lib/rakuten_web_service/ichiba.rb +2 -0
  27. data/lib/rakuten_web_service/kobo/ebook.rb +1 -1
  28. data/lib/rakuten_web_service/kobo/genre.rb +1 -1
  29. data/lib/rakuten_web_service/recipe/category.rb +3 -1
  30. data/lib/rakuten_web_service/recipe.rb +1 -1
  31. data/lib/rakuten_web_service/resource.rb +29 -20
  32. data/lib/rakuten_web_service/response.rb +5 -4
  33. data/lib/rakuten_web_service/search_result.rb +14 -15
  34. data/lib/rakuten_web_service/travel/area_class.rb +30 -29
  35. data/lib/rakuten_web_service/travel/hotel.rb +3 -1
  36. data/lib/rakuten_web_service/travel/search_result.rb +1 -1
  37. data/lib/rakuten_web_service/version.rb +1 -1
  38. data/lib/rakuten_web_service.rb +17 -1
  39. data/rakuten_web_service.gemspec +6 -4
  40. data/spec/fixtures/ichiba/item_search_with_no_items.json +8 -0
  41. data/spec/fixtures/ichiba/tag_search.json +15 -0
  42. data/spec/rakuten_web_service/configuration_spec.rb +0 -52
  43. data/spec/rakuten_web_service/genre_spec.rb +1 -1
  44. data/spec/rakuten_web_service/ichiba/item_spec.rb +13 -0
  45. data/spec/rakuten_web_service/ichiba/tag_group_spec.rb +68 -0
  46. data/spec/rakuten_web_service/ichiba/tag_spec.rb +35 -0
  47. data/spec/rakuten_web_service/resource_spec.rb +2 -2
  48. data/spec/rakuten_web_service/response_spec.rb +7 -7
  49. data/spec/rakuten_web_service/travel/search_result_spec.rb +4 -4
  50. data/spec/rakuten_web_service_spec.rb +48 -0
  51. metadata +56 -16
  52. data/.travis.yml +0 -46
  53. data/spec/integration/integration_spec.rb +0 -78
@@ -33,6 +33,7 @@ module RakutenWebService
33
33
  def genre_information
34
34
  return unless @resource_class.respond_to?(:genre_class)
35
35
  return if self['GenreInformation'].empty?
36
+
36
37
  RWS::GenreInformation.new(self['GenreInformation'][0], @resource_class.genre_class)
37
38
  end
38
39
 
@@ -40,12 +41,12 @@ module RakutenWebService
40
41
  @resources ||= @resource_class.parse_response(@json)
41
42
  end
42
43
 
43
- def has_next_page?
44
- page && (not last_page?)
44
+ def next_page?
45
+ page && !last_page?
45
46
  end
46
47
 
47
- def has_previous_page?
48
- page && (not first_page?)
48
+ def previous_page?
49
+ page && !first_page?
49
50
  end
50
51
 
51
52
  def first_page?
@@ -43,20 +43,19 @@ module RakutenWebService
43
43
 
44
44
  def order(options)
45
45
  new_params = params.dup
46
- if options.is_a? Hash
47
- key, sort_order = *options.to_a.last
48
- key = key.to_s.to_camel
49
- new_params[:sort] = case sort_order.to_s.downcase
50
- when 'desc'
51
- "-#{key}"
52
- when 'asc'
53
- "+#{key}"
54
- end
55
- elsif options.to_s == 'standard'
46
+ if options.to_s == 'standard'
56
47
  new_params[:sort] = 'standard'
57
- else
48
+ return self.class.new(new_params, @resource_class)
49
+ end
50
+ unless options.is_a? Hash
58
51
  raise ArgumentError, "Invalid Sort Option: #{options.inspect}"
59
52
  end
53
+
54
+ key, sort_order = *options.to_a.last
55
+ case sort_order.to_s.downcase
56
+ when 'desc' then new_params[:sort] = "-#{key}".to_camel
57
+ when 'asc' then new_params[:sort] = "+#{key}".to_camel
58
+ end
60
59
  self.class.new(new_params, @resource_class)
61
60
  end
62
61
 
@@ -69,16 +68,16 @@ module RakutenWebService
69
68
  @response ||= query
70
69
  end
71
70
 
72
- def has_next_page?
73
- response.has_next_page?
71
+ def next_page?
72
+ response.next_page?
74
73
  end
75
74
 
76
75
  def next_page
77
76
  search(page: response.page + 1)
78
77
  end
79
78
 
80
- def has_previous_page?
81
- response.has_previous_page?
79
+ def previous_page?
80
+ response.previous_page?
82
81
  end
83
82
 
84
83
  def previous_page
@@ -1,30 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rakuten_web_service/travel/resource'
2
4
 
3
5
  module RakutenWebService
4
6
  module Travel
5
7
  module AreaClass
6
- def search(options={})
8
+ def search(options = {})
7
9
  Base.search(options)
8
10
  end
9
11
 
10
12
  def [](class_code)
11
- LargeClass[class_code] or
12
- MiddleClass[class_code] or
13
- SmallClass[class_code] or
13
+ LargeClass[class_code] ||
14
+ MiddleClass[class_code] ||
15
+ SmallClass[class_code] ||
14
16
  DetailClass[class_code]
15
17
  end
16
18
  module_function :search, :[]
17
- end
18
- end
19
- end
20
19
 
21
- module RakutenWebService
22
- module Travel
23
- module AreaClass
24
20
  class Base < RakutenWebService::Travel::Resource
25
21
  endpoint 'https://app.rakuten.co.jp/services/api/Travel/GetAreaClass/20131024'
26
22
 
27
- set_parser do |response|
23
+ parser do |response|
28
24
  response['areaClasses']['largeClasses'].map do |data|
29
25
  LargeClass.new(data)
30
26
  end
@@ -66,25 +62,17 @@ module RakutenWebService
66
62
 
67
63
  attr_reader :parent, :children
68
64
 
69
- def initialize(data, parent=nil)
65
+ def initialize(class_data, parent = nil)
70
66
  @parent = parent
71
- class_data = data
72
- @params, @children = *(case class_data
73
- when Array
74
- class_data.first(2)
75
- when Hash
76
- [class_data, nil]
77
- end)
78
-
79
- if !(children.nil?) and !(children.empty?)
80
- children_class = children.keys.map { |k| k[/\A(\w*)Classes\Z/, 1] }.first rescue data.tapp
81
- @params["#{children_class}Classes"] = children["#{children_class}Classes"].map do |child_data|
82
- Base.area_classes[children_class].new(child_data["#{children_class}Class"], self)
83
- end
84
- @children = @params["#{children_class}Classes"]
85
- else
86
- @children = []
67
+ case class_data
68
+ when Array
69
+ @params = class_data.shift
70
+ @children = class_data.shift
71
+ when Hash
72
+ @params = class_data
73
+ @children = nil
87
74
  end
75
+ parse_children_attributes
88
76
  end
89
77
 
90
78
  def area_level
@@ -105,11 +93,24 @@ module RakutenWebService
105
93
  query
106
94
  end
107
95
 
108
- def search(params={})
96
+ def search(params = {})
109
97
  params = to_query.merge(params)
110
98
 
111
99
  Hotel.search(params)
112
100
  end
101
+
102
+ private
103
+
104
+ def parse_children_attributes
105
+ return @children = [] if children.nil? || children.empty?
106
+
107
+ children_class = children.keys.first[/\A(\w*)Classes\Z/, 1]
108
+ class_name = "#{children_class}Classes"
109
+ params[class_name] = children[class_name].map do |child_data|
110
+ Base.area_classes[children_class].new(child_data["#{children_class}Class"], self)
111
+ end
112
+ @children = params[class_name]
113
+ end
113
114
  end
114
115
 
115
116
  class LargeClass < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rakuten_web_service/travel/resource'
2
4
 
3
5
  module RakutenWebService
@@ -5,7 +7,7 @@ module RakutenWebService
5
7
  class Hotel < RakutenWebService::Travel::Resource
6
8
  endpoint 'https://app.rakuten.co.jp/services/api/Travel/SimpleHotelSearch/20170426'
7
9
 
8
- set_parser do |response|
10
+ parser do |response|
9
11
  response['hotels'].map do |hotel_info|
10
12
  Hotel.new(hotel_info)
11
13
  end
@@ -16,7 +16,7 @@ module RakutenWebService
16
16
  end
17
17
  end
18
18
 
19
- def has_next_page?
19
+ def next_page?
20
20
  (page < page_count)
21
21
  end
22
22
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RakutenWebService
4
- VERSION = '1.10.0'.freeze
4
+ VERSION = '1.13.1'
5
5
  end
@@ -1,6 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module RakutenWebService; end
3
+ require 'rakuten_web_service/configuration'
4
+
5
+ module RakutenWebService
6
+ def configure(&block)
7
+ raise ArgumentError, 'Block is required' unless block
8
+ raise ArgumentError, 'Block is required to have one argument' if block.arity != 1
9
+ yield configuration
10
+
11
+ configuration
12
+ end
13
+
14
+ def configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ module_function :configure, :configuration
19
+ end
4
20
  RWS = RakutenWebService
5
21
 
6
22
  require 'rakuten_web_service/ichiba'
@@ -15,12 +15,14 @@ Gem::Specification.new do |spec|
15
15
  spec.files = `git ls-files`.split($/)
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
  spec.require_paths = ['lib']
18
- spec.required_ruby_version = '>= 2.3.0'
18
+ spec.required_ruby_version = '>= 2.5.0'
19
19
 
20
+ spec.add_dependency 'json', '~> 2.3'
20
21
  spec.add_development_dependency 'bundler'
21
- spec.add_development_dependency 'rake', '~> 12.3.0'
22
- spec.add_development_dependency 'rspec', '~> 3.8.0'
22
+ spec.add_development_dependency 'rake', '~> 13.0'
23
+ spec.add_development_dependency 'rexml', '~> 3.2'
24
+ spec.add_development_dependency 'rspec', '~> 3.9'
23
25
  spec.add_development_dependency 'tapp', '~> 1.5.1'
24
26
  spec.add_development_dependency 'terminal-table', '~> 1.8.0'
25
- spec.add_development_dependency 'webmock', '~> 3.5.0'
27
+ spec.add_development_dependency 'webmock', '~> 3.9'
26
28
  end
@@ -0,0 +1,8 @@
1
+ {
2
+ "count": 0,
3
+ "page": 1,
4
+ "first": 0,
5
+ "last": 0,
6
+ "hits": 0,
7
+ "pageCount": 0
8
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "tagGroups": [
3
+ {
4
+ "tags": [
5
+ {
6
+ "parentTagId": 0,
7
+ "tagName": "SS",
8
+ "tagId": 1000317
9
+ }
10
+ ],
11
+ "tagGroupId": 1000041,
12
+ "tagGroupName": "サイズ(S/M/L)"
13
+ }
14
+ ]
15
+ }
@@ -23,58 +23,6 @@ describe RakutenWebService::Configuration do
23
23
  end
24
24
  end
25
25
 
26
- describe '.configure' do
27
- context 'given block has one arity' do
28
- before do
29
- RakutenWebService.configure do |c|
30
- c.affiliate_id = 'dummy_affiliate_id'
31
- c.application_id = 'dummy_application_id'
32
- end
33
- end
34
-
35
- subject { RakutenWebService.configuration }
36
-
37
- describe '#affiliate_id' do
38
- subject { super().affiliate_id }
39
- it { is_expected.to eq('dummy_affiliate_id') }
40
- end
41
-
42
- describe '#application_id' do
43
- subject { super().application_id }
44
- it { is_expected.to eq('dummy_application_id') }
45
- end
46
- end
47
-
48
- context 'given block has more or less one arity' do
49
- specify 'raise ArgumentError' do
50
- expect do
51
- RakutenWebService.configure do
52
- end
53
- end.to raise_error(ArgumentError)
54
-
55
- expect do
56
- RakutenWebService.configure do |c, _|
57
- c.affiliate_id = 'dummy_affiliate_id'
58
- c.application_id = 'dummy_application_id'
59
- end
60
- end.to raise_error(ArgumentError)
61
- end
62
- end
63
- end
64
-
65
- describe '.configuration' do
66
- context 'When calling with a block' do
67
- specify 'should show warning message' do
68
- $stderr = StringIO.new
69
- RakutenWebService.configuration do |c|
70
- c.application_id = 'dummy_affiliate_id'
71
- end
72
- expect($stderr.string).to match(/Warning: /)
73
- $stderr = STDERR
74
- end
75
- end
76
- end
77
-
78
26
  describe '#debug_mode?' do
79
27
  let(:configuration) { RakutenWebService.configuration }
80
28
 
@@ -42,7 +42,7 @@ describe RakutenWebService::BaseGenre do
42
42
 
43
43
  Class.new(RakutenWebService::BaseGenre) do
44
44
  endpoint "https://api.example.com/dummy_genre"
45
- set_resource_name 'dummy_genre'
45
+ self.resource_name = 'dummy_genre'
46
46
  root_id 0
47
47
  attribute :dummyGenreId, :dummyGenreName, :genreLevel
48
48
  end
@@ -130,6 +130,19 @@ describe RakutenWebService::Ichiba::Item do
130
130
  end
131
131
  end
132
132
 
133
+ describe '.search(no items)' do
134
+ before do
135
+ response = JSON.parse(fixture('ichiba/item_search_with_no_items.json'))
136
+ @expected_request = stub_request(:get, endpoint).with(query: expected_query).to_return(body: response.to_json)
137
+ end
138
+
139
+ subject { RakutenWebService::Ichiba::Item.search(keyword: 'Ruby').first(1) }
140
+ specify '' do
141
+ expect { subject }.to_not raise_error
142
+ end
143
+ it { is_expected.to eq [] }
144
+ end
145
+
133
146
  describe '.all' do
134
147
  before do
135
148
  response = JSON.parse(fixture('ichiba/item_search_with_keyword_Ruby.json'))
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ require 'rakuten_web_service/ichiba/tag_group'
3
+
4
+ describe RakutenWebService::Ichiba::TagGroup do
5
+ let(:endpoint) { 'https://app.rakuten.co.jp/services/api/IchibaTag/Search/20140222' }
6
+ let(:affiliate_id) { 'affiliate_id' }
7
+ let(:application_id) { 'application_id' }
8
+ let(:expected_query) do
9
+ {
10
+ affiliateId: affiliate_id,
11
+ applicationId: application_id,
12
+ formatVersion: '2',
13
+ tagId: '1000317'
14
+ }
15
+ end
16
+
17
+ before do
18
+ response = JSON.parse(fixture('ichiba/tag_search.json'))
19
+ @expected_request = stub_request(:get, endpoint).
20
+ with(query: expected_query).to_return(body: response.to_json)
21
+
22
+ RakutenWebService.configure do |c|
23
+ c.affiliate_id = affiliate_id
24
+ c.application_id = application_id
25
+ end
26
+ end
27
+
28
+ describe '.search' do
29
+ let(:expected_json) do
30
+ response = JSON.parse(fixture('ichiba/tag_search.json'))
31
+ response['tagGroups'][0]
32
+ end
33
+
34
+ before do
35
+ @tag_group = RakutenWebService::Ichiba::TagGroup.search({tagId: '1000317'}).first
36
+ end
37
+
38
+ subject { @tag_group }
39
+
40
+ specify 'should call the endpoint once' do
41
+ expect(@expected_request).to have_been_made.once
42
+ end
43
+
44
+ specify 'should be access by key' do
45
+ expect(subject['tagGroupId']).to eq(expected_json['tagGroupId'])
46
+ expect(subject['tag_group_id']).to eq(expected_json['tagGroupId'])
47
+ end
48
+ end
49
+
50
+ describe '#tags' do
51
+ let(:response) { JSON.parse(fixture('ichiba/tag_search.json')) }
52
+ let(:expected_tag) { response['tagGroups'][0]['tags'][0] }
53
+
54
+ before do
55
+ @tag_group = RakutenWebService::Ichiba::TagGroup.search(tagId: '1000317').first
56
+ end
57
+
58
+ subject { @tag_group.tags }
59
+
60
+ specify 'responds Tags object' do
61
+ subject.map do |tag|
62
+ expect(tag.id).to eq(expected_tag['tagId'])
63
+ expect(tag.name).to eq(expected_tag['tagName'])
64
+ expect(tag['parentTagId']).to eq(expected_tag['parentTagId'])
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe RakutenWebService::Ichiba::Tag do
4
+ let(:params) do
5
+ { 'tagId' => 100,
6
+ 'tagName' => 'SS',
7
+ 'parentTagId' => 1 }
8
+ end
9
+ let(:tag) { RakutenWebService::Ichiba::Tag.new(params) }
10
+
11
+ describe '.new' do
12
+ specify 'returned object should have methods to fetch values' do
13
+ expect(tag.id).to eq(100)
14
+ expect(tag.name).to eq('SS')
15
+ expect(tag['parentTagId']).to eq(1)
16
+ end
17
+ end
18
+
19
+ describe '#search' do
20
+ context 'When no given additional parameters' do
21
+ specify 'it calls Ichiba::Item.search with its tag id' do
22
+ expect(RWS::Ichiba::Item).to receive(:search).with({tagId: params['tagId']}).and_return([])
23
+
24
+ tag.search
25
+ end
26
+ end
27
+ context 'When given required parameter' do
28
+ specify 'it calls Ichiba::Item.search with the given parameters and its tag id' do
29
+ expect(RWS::Ichiba::Item).to receive(:search).with({keyword: 'Ruby', tagId: params['tagId']}).and_return([])
30
+
31
+ tag.search(keyword: 'Ruby')
32
+ end
33
+ end
34
+ end
35
+ end