rakuten_web_service 1.12.0 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +12 -0
  3. data/.travis.yml +4 -16
  4. data/CHANGELOG.md +28 -0
  5. data/Gemfile +3 -3
  6. data/README.ja.md +118 -48
  7. data/README.md +30 -6
  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/error.rb +5 -0
  16. data/lib/rakuten_web_service/genre.rb +1 -1
  17. data/lib/rakuten_web_service/gora/course.rb +1 -1
  18. data/lib/rakuten_web_service/gora/course_detail.rb +1 -1
  19. data/lib/rakuten_web_service/gora/plan.rb +1 -1
  20. data/lib/rakuten_web_service/ichiba/item.rb +1 -1
  21. data/lib/rakuten_web_service/ichiba/product.rb +1 -1
  22. data/lib/rakuten_web_service/ichiba/ranking.rb +1 -1
  23. data/lib/rakuten_web_service/ichiba/tag.rb +5 -0
  24. data/lib/rakuten_web_service/ichiba/tag_group.rb +2 -6
  25. data/lib/rakuten_web_service/kobo/ebook.rb +1 -1
  26. data/lib/rakuten_web_service/kobo/genre.rb +1 -1
  27. data/lib/rakuten_web_service/recipe.rb +1 -1
  28. data/lib/rakuten_web_service/recipe/category.rb +3 -1
  29. data/lib/rakuten_web_service/resource.rb +29 -20
  30. data/lib/rakuten_web_service/response.rb +5 -4
  31. data/lib/rakuten_web_service/search_result.rb +14 -15
  32. data/lib/rakuten_web_service/travel/area_class.rb +28 -22
  33. data/lib/rakuten_web_service/travel/hotel.rb +3 -1
  34. data/lib/rakuten_web_service/travel/search_result.rb +1 -1
  35. data/lib/rakuten_web_service/version.rb +1 -1
  36. data/rakuten_web_service.gemspec +6 -4
  37. data/spec/rakuten_web_service/genre_spec.rb +1 -1
  38. data/spec/rakuten_web_service/ichiba/tag_spec.rb +17 -0
  39. data/spec/rakuten_web_service/resource_spec.rb +2 -2
  40. data/spec/rakuten_web_service/response_spec.rb +7 -7
  41. data/spec/rakuten_web_service/travel/search_result_spec.rb +4 -4
  42. metadata +43 -15
  43. data/spec/integration/integration_spec.rb +0 -81
@@ -5,7 +5,7 @@ require 'rakuten_web_service/genre'
5
5
  module RakutenWebService
6
6
  module Books
7
7
  class Genre < RakutenWebService::BaseGenre
8
- set_resource_name 'books_genre'
8
+ self.resource_name = 'books_genre'
9
9
 
10
10
  endpoint 'https://app.rakuten.co.jp/services/api/BooksGenre/Search/20121128'
11
11
 
@@ -6,7 +6,7 @@ require 'rakuten_web_service/books/genre'
6
6
  module RakutenWebService
7
7
  module Books
8
8
  class Resource < RakutenWebService::Resource
9
- set_parser do |response|
9
+ parser do |response|
10
10
  response['Items'].map { |item| new(item) }
11
11
  end
12
12
 
@@ -7,7 +7,7 @@ module RakutenWebService
7
7
  class Total < Books::Resource
8
8
  endpoint 'https://app.rakuten.co.jp/services/api/BooksTotal/Search/20170404'
9
9
 
10
- set_parser do |response|
10
+ parser do |response|
11
11
  response['Items'].map do |item|
12
12
  resource_class = find_resource_by_genre_id(item['booksGenreId'])
13
13
  resource_class.new(item)
@@ -9,6 +9,8 @@ require 'rakuten_web_service/error'
9
9
 
10
10
  module RakutenWebService
11
11
  class Client
12
+ USER_AGENT = "RakutenWebService SDK for Ruby v#{RWS::VERSION}(ruby-#{RUBY_VERSION} [#{RUBY_PLATFORM}])".freeze
13
+
12
14
  attr_reader :url
13
15
 
14
16
  def initialize(resource_class)
@@ -20,25 +22,23 @@ module RakutenWebService
20
22
  params = RakutenWebService.configuration.generate_parameters(params)
21
23
  response = request(url.path, params)
22
24
  body = JSON.parse(response.body)
23
-
24
- if Net::HTTPSuccess === response
25
- return RakutenWebService::Response.new(@resource_class, body)
25
+ unless response.is_a?(Net::HTTPSuccess)
26
+ raise RakutenWebService::Error.for(response)
26
27
  end
27
- raise RakutenWebService::Error.repository[response.code.to_i], body['error_description']
28
+
29
+ RakutenWebService::Response.new(@resource_class, body)
28
30
  end
29
31
 
30
32
  private
31
33
 
32
34
  def request(path, params)
33
- http = Net::HTTP.new(@url.host, @url.port)
35
+ http = Net::HTTP.new(url.host, url.port)
34
36
  http.use_ssl = true
35
37
  if RakutenWebService.configuration.debug_mode?
36
38
  http.set_debug_output($stderr)
37
39
  end
38
- path = "#{path}?#{params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')}"
39
- header = {
40
- 'User-Agent' => "RakutenWebService SDK for Ruby v#{RWS::VERSION}(ruby-#{RUBY_VERSION} [#{RUBY_PLATFORM}])"
41
- }
40
+ path = "#{path}?#{URI.encode_www_form(params)}"
41
+ header = { 'User-Agent' => USER_AGENT }
42
42
  http.get(path, header)
43
43
  end
44
44
  end
@@ -6,6 +6,11 @@ module RakutenWebService
6
6
  repository[status_code] = error
7
7
  end
8
8
 
9
+ def self.for(response)
10
+ error_class = repository[response.code.to_i]
11
+ error_class.new(JSON.parse(response.body)['error_description'])
12
+ end
13
+
9
14
  def self.repository
10
15
  @repository ||= {}
11
16
  end
@@ -7,7 +7,7 @@ module RakutenWebService
7
7
  def self.inherited(klass)
8
8
  super
9
9
 
10
- klass.set_parser do |response|
10
+ klass.parser do |response|
11
11
  current = response['current']
12
12
  %w[children parents brothers].each do |type|
13
13
  elements = Array(response[type]).map { |e| klass.new(e) }
@@ -7,7 +7,7 @@ module RakutenWebService
7
7
  class Course < Resource
8
8
  endpoint 'https://app.rakuten.co.jp/services/api/Gora/GoraGolfCourseSearch/20170623'
9
9
 
10
- set_parser do |response|
10
+ parser do |response|
11
11
  response['Items'].map { |item| new(item) }
12
12
  end
13
13
 
@@ -13,7 +13,7 @@ module RakutenWebService
13
13
 
14
14
  endpoint 'https://app.rakuten.co.jp/services/api/Gora/GoraGolfCourseDetail/20170623'
15
15
 
16
- set_parser do |response|
16
+ parser do |response|
17
17
  [new(response['Item'])]
18
18
  end
19
19
 
@@ -7,7 +7,7 @@ module RakutenWebService
7
7
  class Plan < Resource
8
8
  endpoint 'https://app.rakuten.co.jp/services/api/Gora/GoraPlanSearch/20170623'
9
9
 
10
- set_parser do |response|
10
+ parser do |response|
11
11
  response['Items'].map { |item| new(item) }
12
12
  end
13
13
 
@@ -17,7 +17,7 @@ module RakutenWebService
17
17
 
18
18
  endpoint 'https://app.rakuten.co.jp/services/api/IchibaItem/Search/20170706'
19
19
 
20
- set_parser do |response|
20
+ parser do |response|
21
21
  response['Items'].map { |item| Item.new(item) }
22
22
  end
23
23
 
@@ -8,7 +8,7 @@ module RakutenWebService
8
8
  class Product < Resource
9
9
  endpoint 'https://app.rakuten.co.jp/services/api/Product/Search/20170426'
10
10
 
11
- set_parser do |response|
11
+ parser do |response|
12
12
  (response['Products'] || []).map { |prod| Product.new(prod) }
13
13
  end
14
14
 
@@ -7,7 +7,7 @@ module RakutenWebService
7
7
  class RankingItem < RakutenWebService::Ichiba::Item
8
8
  endpoint 'https://app.rakuten.co.jp/services/api/IchibaItem/Ranking/20170628'
9
9
 
10
- set_parser do |response|
10
+ parser do |response|
11
11
  response['Items'].map { |item| RankingItem.new(item) }
12
12
  end
13
13
 
@@ -6,6 +6,11 @@ module RakutenWebService
6
6
  module Ichiba
7
7
  class Tag < Resource
8
8
  attribute :tagId, :tagName, :parentTagId
9
+
10
+ def search(params = {})
11
+ params = params.merge(tagId: id)
12
+ RakutenWebService::Ichiba::Item.search(params)
13
+ end
9
14
  end
10
15
  end
11
16
  end
@@ -7,7 +7,7 @@ module RakutenWebService
7
7
  class TagGroup < Resource
8
8
  endpoint 'https://app.rakuten.co.jp/services/api/IchibaTag/Search/20140222'
9
9
 
10
- set_parser do |response|
10
+ parser do |response|
11
11
  response['tagGroups'].map { |tag_group| TagGroup.new(tag_group) }
12
12
  end
13
13
 
@@ -15,11 +15,7 @@ module RakutenWebService
15
15
 
16
16
  def tags
17
17
  get_attribute('tags').map do |tag|
18
- Tag.new(
19
- 'tagId' => tag['tagId'],
20
- 'tagName' => tag['tagName'],
21
- 'parentTagId' => tag['parentTagId']
22
- )
18
+ Tag.new(tag)
23
19
  end
24
20
  end
25
21
  end
@@ -16,7 +16,7 @@ module RakutenWebService
16
16
  :reviewCount, :reviewAverage,
17
17
  :koboGenreId
18
18
 
19
- set_parser do |response|
19
+ parser do |response|
20
20
  response['Items'].map { |i| new(i) }
21
21
  end
22
22
 
@@ -5,7 +5,7 @@ require 'rakuten_web_service/genre'
5
5
  module RakutenWebService
6
6
  module Kobo
7
7
  class Genre < RakutenWebService::BaseGenre
8
- set_resource_name :kobo_genre
8
+ self.resource_name = :kobo_genre
9
9
 
10
10
  root_id '101'
11
11
 
@@ -15,7 +15,7 @@ module RakutenWebService
15
15
  :recipeIndication, :recipeCost,
16
16
  :recipePublishday, :rank
17
17
 
18
- set_parser do |response|
18
+ parser do |response|
19
19
  response['result'].map { |r| Recipe.new(r) }
20
20
  end
21
21
 
@@ -37,7 +37,9 @@ module RakutenWebService
37
37
 
38
38
  def parent_category
39
39
  return nil if parent_category_type.nil?
40
- Recipe.categories(parent_category_type).find { |c| c.id.to_i == parent_category_id.to_i }
40
+ Recipe.categories(parent_category_type).find do |c|
41
+ c.id.to_i == parent_category_id.to_i
42
+ end
41
43
  end
42
44
 
43
45
  def absolute_category_id
@@ -12,6 +12,8 @@ module RakutenWebService
12
12
  using RakutenWebService::StringSupport
13
13
 
14
14
  class << self
15
+ attr_writer :resource_name
16
+
15
17
  def inherited(subclass)
16
18
  @@subclasses ||= []
17
19
  @@subclasses.push(subclass)
@@ -24,20 +26,11 @@ module RakutenWebService
24
26
  def attribute(*attribute_names)
25
27
  attribute_names.each do |attribute_name|
26
28
  attribute_name = attribute_name.to_s
27
- method_name = attribute_name.to_snake
28
- method_name = method_name.sub(/^#{resource_name}_(\w+)$/, '\1')
29
-
30
- instance_eval do
31
- define_method method_name do
32
- get_attribute(attribute_name)
33
- end
34
- end
35
- next if method_name !~ /(.+)_flag$/
36
- instance_eval do
37
- define_method "#{$1}?" do
38
- get_attribute(attribute_name) == 1
39
- end
40
- end
29
+
30
+ define_getter_for_attribute(attribute_name)
31
+ next unless attribute_name.end_with?('Flag')
32
+
33
+ define_asking_method_for_attribute(attribute_name)
41
34
  end
42
35
  end
43
36
 
@@ -57,19 +50,36 @@ module RakutenWebService
57
50
  @resource_name ||= name.split('::').last.downcase
58
51
  end
59
52
 
60
- def set_resource_name(name)
61
- @resource_name = name
62
- end
63
-
64
53
  def endpoint(url = nil)
65
54
  @endpoint = url || @endpoint
66
55
  end
67
56
 
68
- def set_parser(&block)
57
+ def parser(&block)
69
58
  instance_eval do
70
59
  define_singleton_method :parse_response, block
71
60
  end
72
61
  end
62
+
63
+ private
64
+
65
+ def define_getter_for_attribute(attribute_name)
66
+ method_name = attribute_name.to_snake
67
+ method_name.sub!(/^#{resource_name}_(\w+)$/, '\1')
68
+
69
+ define_method method_name do
70
+ get_attribute(attribute_name)
71
+ end
72
+ end
73
+
74
+ def define_asking_method_for_attribute(attribute_name)
75
+ method_name = attribute_name.to_snake
76
+ method_name.sub!(/^#{resource_name}_(\w+)$/, '\1')
77
+ method_name.sub!(/(.+)_flag$/, '\1')
78
+
79
+ define_method "#{method_name}?" do
80
+ get_attribute(attribute_name) == 1
81
+ end
82
+ end
73
83
  end
74
84
 
75
85
  def initialize(params)
@@ -79,7 +89,6 @@ module RakutenWebService
79
89
 
80
90
  def [](key)
81
91
  camel_key = key.to_camel
82
- # camel_key[0] = camel_key[0].downcase
83
92
  @params[key] || @params[camel_key]
84
93
  end
85
94
 
@@ -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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rakuten_web_service/travel/resource'
2
4
 
3
5
  module RakutenWebService
@@ -8,9 +10,9 @@ module RakutenWebService
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, :[]
@@ -18,7 +20,7 @@ module RakutenWebService
18
20
  class Base < RakutenWebService::Travel::Resource
19
21
  endpoint 'https://app.rakuten.co.jp/services/api/Travel/GetAreaClass/20131024'
20
22
 
21
- set_parser do |response|
23
+ parser do |response|
22
24
  response['areaClasses']['largeClasses'].map do |data|
23
25
  LargeClass.new(data)
24
26
  end
@@ -60,26 +62,17 @@ module RakutenWebService
60
62
 
61
63
  attr_reader :parent, :children
62
64
 
63
- def initialize(data, parent = nil)
65
+ def initialize(class_data, parent = nil)
64
66
  @parent = parent
65
- class_data = data
66
- @params, @children = *(case class_data
67
- when Array
68
- class_data.first(2)
69
- when Hash
70
- [class_data, nil]
71
- end)
72
-
73
- if !children.nil? && !children.empty?
74
- children_class = children.keys.first[/\A(\w*)Classes\Z/, 1]
75
- class_name = "#{children_class}Classes"
76
- @params[class_name] = children[class_name].map do |child_data|
77
- Base.area_classes[children_class].new(child_data["#{children_class}Class"], self)
78
- end
79
- @children = @params[class_name]
80
- else
81
- @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
82
74
  end
75
+ parse_children_attributes
83
76
  end
84
77
 
85
78
  def area_level
@@ -105,6 +98,19 @@ module RakutenWebService
105
98
 
106
99
  Hotel.search(params)
107
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
108
114
  end
109
115
 
110
116
  class LargeClass < Base