genba-ruby 0.1.4 → 0.1.11

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
- SHA1:
3
- metadata.gz: 2e99dbb1f55b724834d4c840f90996c2eb86c0d1
4
- data.tar.gz: 63cc23761d42d2d1b348132162191e0726f09d85
2
+ SHA256:
3
+ metadata.gz: b26858cbcdf74806c55c285a886a950f147311c4c0551315e7b5ab4953c38176
4
+ data.tar.gz: a328a76b1d1165e4ccecc2c9d44b5623321a461a060d81114fdb7e7edd5a8909
5
5
  SHA512:
6
- metadata.gz: 4b8768b0c971dc99c510cc7bf924723cb56bd6a2df11308550f4c730122b1277e0a03338fedab4e6065ac3a3a43d4cd2b5f6a236f5b7c2ede123ab1cf7bd4e2f
7
- data.tar.gz: ae8bcb761b18971fa5ea8254dc6db40e3e66b0d83dbcfed3e24c57e47fd2bbfcfc62cc38d260c98bc2a9697e5b3581270ef84ba53a72da695d2aa911c26432d5
6
+ metadata.gz: 5a7139d2357bae3b9771d5adcdd5fbece38f9e0f07a42e4df4c434bafa093832698b9f947a1987ae7d9a0a7be2545be3ae2a9678dcfb78da049dff3274addfbe
7
+ data.tar.gz: 87256758cf9225fa6465306ccb9012daae5d6436618404ec23ecbeff5d06fa04d2db07e7a7c55eaee539b6014afe6b3fc69ec295a3e36a98566c7d4f7642e109
@@ -0,0 +1,29 @@
1
+ version: 2
2
+
3
+ .build_template: &build_definition
4
+ steps:
5
+ - checkout
6
+ - run:
7
+ name: Install libmcrypt-dev
8
+ command: apt-get update && apt-get install libmcrypt-dev -y
9
+ - run:
10
+ name: Bundle version
11
+ command: gem install bundler
12
+ - run:
13
+ name: Bundle Install
14
+ command: bundle check || bundle install
15
+ - run:
16
+ name: Run tests
17
+ command: |
18
+ bundle exec rspec
19
+ working_directory: ~/app
20
+ workflows:
21
+ version: 2
22
+ build_ruby_versions:
23
+ jobs:
24
+ - build_ruby2_5
25
+ jobs:
26
+ build_ruby2_5:
27
+ <<: *build_definition
28
+ docker:
29
+ - image: ruby:2.3.8-jessie
data/.gitignore CHANGED
@@ -10,3 +10,6 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  .byebug_history
13
+ *.log
14
+ .DS_Store
15
+ *.txt
data/.rspec CHANGED
File without changes
File without changes
File without changes
data/Gemfile CHANGED
@@ -10,6 +10,7 @@ gem 'activemodel', require: 'active_model'
10
10
  group :development do
11
11
  gem 'rake'
12
12
  gem 'webmock'
13
+ gem 'uuidtools'
13
14
 
14
15
  platforms :mri do
15
16
  # to avoid problems, bring Byebug in on just versions of Ruby under which
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- genba-ruby (0.1.3)
5
- oj (~> 2.18)
4
+ genba-ruby (0.1.11)
5
+ oj (~> 3.5)
6
6
  rest-client (~> 2.0)
7
7
  ruby-mcrypt (= 0.2.0)
8
8
 
@@ -32,12 +32,12 @@ GEM
32
32
  i18n (1.0.1)
33
33
  concurrent-ruby (~> 1.0)
34
34
  method_source (0.9.0)
35
- mime-types (3.1)
35
+ mime-types (3.2.2)
36
36
  mime-types-data (~> 3.2015)
37
- mime-types-data (3.2016.0521)
37
+ mime-types-data (3.2019.0331)
38
38
  minitest (5.11.3)
39
39
  netrc (0.11.0)
40
- oj (2.18.5)
40
+ oj (3.7.12)
41
41
  pry (0.11.3)
42
42
  coderay (~> 1.1.0)
43
43
  method_source (~> 0.9.0)
@@ -70,7 +70,8 @@ GEM
70
70
  thread_safe (~> 0.1)
71
71
  unf (0.1.4)
72
72
  unf_ext
73
- unf_ext (0.0.7.5)
73
+ unf_ext (0.0.7.6)
74
+ uuidtools (2.1.5)
74
75
  webmock (3.4.1)
75
76
  addressable (>= 2.3.6)
76
77
  crack (>= 0.3.2)
@@ -88,7 +89,8 @@ DEPENDENCIES
88
89
  pry-byebug
89
90
  rake
90
91
  rspec (~> 3.0)
92
+ uuidtools
91
93
  webmock
92
94
 
93
95
  BUNDLED WITH
94
- 1.16.1
96
+ 1.17.3
File without changes
data/README.md CHANGED
@@ -1,68 +1,94 @@
1
1
  # Genba Ruby
2
2
 
3
3
  [![Build Status](https://travis-ci.org/iscreen/genba-ruby.svg)](https://travis-ci.org/iscreen/genba-ruby)
4
- [![CircleCI](https://circleci.com/gh/iscreen/genba-ruby.svg?style=shield)](https://circleci.com/gh/iscreen/genba-ruby)
4
+ [![CircleCI](https://circleci.com/gh/iscreen/genba-ruby.svg?style=svg)](https://circleci.com/gh/iscreen/genba-ruby)
5
5
  [![Gem Version](https://badge.fury.io/rb/genba-ruby.svg)](https://badge.fury.io/rb/genba-ruby)
6
6
 
7
- The Genba Library provides convenient access to the [Genba API](https://api.genbagames.com/doc/) from applications written in the Ruby language.
7
+ The Genba Library provides convenient access to the [Genba API](https://api.genbagames.com/doc/) from applications written in the Ruby language.
8
8
 
9
9
  ## Basic Usage
10
10
 
11
+ ```ruby
11
12
  require 'genba'
12
13
 
13
14
  genba_client = Genba.client(
14
15
  username: 'api_genba_user',
15
- app_id: '79cf9bb2-e7f5-448f-b996-c52e4b2bb351',
16
- api_key: 'e6a69096fb94d91971ccc69b1770b9f3'
16
+ app_id: '00000000-0000-0000-0000-000000000000',
17
+ api_key: '00000000000000000000000000000000',
18
+ customer_account_id: '00000000-0000-0000-0000-000000000000'
17
19
  )
20
+ ```
18
21
 
19
22
  ## Products
20
23
  [Genba Products API](https://api.genbagames.com/doc/#api-Product)
21
24
 
22
- Call products.get_products to get all products.
25
+ ### Product - Changes
26
+
27
+ Return a list of products which have changed since a given date, including basic or advanced metadata.
23
28
 
24
29
  ```ruby
25
30
  payload = {
26
- countryISO: 'US',
27
- includeMeta: false
31
+ from_date: DateTime.now - 1,
32
+ country_iso: 'US',
33
+ include_meta: false
34
+ }
35
+
36
+ response = genba_client.products.get_changes(payload)
37
+ ```
38
+
39
+ ```ruby
40
+ payload = {
41
+ include_meta: false
42
+ }
43
+
44
+ response = genba_client.products.get_changes(payload)
45
+ ```
46
+
47
+ ### Product - Get
48
+
49
+ Return a list of product objects, including basic or advanced metadata.
50
+
51
+ ```ruby
52
+ payload = {
53
+ include_meta: false
28
54
  }
29
55
 
30
56
  response = genba_client.products.get_products(payload)
31
57
  ```
32
58
 
33
- Retrieve an existing product
59
+ Return a list of product objects with countryISO
34
60
 
35
61
  ```ruby
36
62
  payload = {
37
- skuId: '79cf9bb2-e7f5-448f-b996-c52e4b2bb351',
38
- countryISO: 'US',
39
- includeMeta: false
63
+ country_iso: 'US',
64
+ include_meta: false
40
65
  }
41
-
42
- product = genba_client.products.get_products(payload)
66
+
67
+ response = genba_client.products.get_products(payload)
43
68
  ```
44
69
 
45
- Return a list of products which have changed since a given date, including basic or advanced metadata.
70
+ Retrieve an existing product
46
71
 
47
72
  ```ruby
48
73
  payload = {
49
- fromDate: 1.days.ago.strftime('%FT%T'),
50
- countryISO: 'US',
51
- includeMeta: false
74
+ sku_id: '79cf9bb2-e7f5-448f-b996-c52e4b2bb351',
75
+ include_meta: true
52
76
  }
53
-
54
- response = genba_client.products.get_changes(payload)
77
+
78
+ product = genba_client.products.get_products(payload)
55
79
  ```
56
80
 
81
+ ### Product - Removed
82
+
57
83
  Return a list of products which have been removed since a given date.
58
84
 
59
85
  ```ruby
60
86
  payload = {
61
- fromDate: 1.days.ago.strftime('%FT%T'),
62
- countryISO: 'US',
63
- includeMeta: false
87
+ from_date: DateTime.now - 1,
88
+ country_iso: 'US',
89
+ include_meta: false
64
90
  }
65
-
91
+
66
92
  response = genba_client.products.get_removed(payload)
67
93
  ```
68
94
 
@@ -78,22 +104,39 @@ response = genba_client.prices.get_prices
78
104
  ## Restrictions
79
105
  [Genba Restrictions API](https://api.genbagames.com/doc/#api-Restrictions)
80
106
 
107
+ ### Restrictions - Get
108
+
81
109
  Returns a list of product sku restrictions, whitelist - can only sold in countries, blacklist - cannot be sold in countries.
82
110
 
111
+ Returns a list of product sku restrictions by countryISO
112
+
83
113
  ```ruby
84
114
  payload = {
85
- productId: '84d90a06-f458-4ed8-8f3d-91aa84cc6577',
86
- fromDate: 1.days.ago.strftime('%FT%T'),
87
- countryISO: 'US'
115
+ country_iso: 'US'
88
116
  }
89
117
 
90
- response = genba_client.restrictions.get_restrictions
118
+ response = genba_client.restrictions.get_restrictions(payload)
119
+ response[:productRestriction]
120
+ ```
121
+
122
+ Returns a list of product sku restrictions by countryISO, productID and fromDate
123
+
124
+ ```ruby
125
+ payload = {
126
+ product_id: '84d90a06-f458-4ed8-8f3d-91aa84cc6577',
127
+ from_date: 1.days.ago.strftime('%FT%T'),
128
+ country_iso: 'US'
129
+ }
130
+
131
+ response = genba_client.restrictions.get_restrictions(payload)
91
132
  response[:productRestriction]
92
133
  ```
93
134
 
94
135
  ## Keys
95
136
  [Genba Keys API](https://api.genbagames.com/doc/#api-Keys)
96
137
 
138
+ ### Keys - Get Test Keys
139
+
97
140
  Request Product Test Keys for a SKU
98
141
 
99
142
  ```ruby
@@ -106,8 +149,89 @@ response[:keys]
106
149
  Request Product Keys for a SKU
107
150
 
108
151
  ```ruby
109
- response = genba_client.keys.get_keys('84d90a06-f458-4ed8-8f3d-91aa84cc6577', 1)
152
+
153
+ response = genba_client.keys.get_keys('84d90a06-f458-4ed8-8f3d-91aa84cc6577', 1, customerAccountId)
110
154
 
111
155
  response[:status]
112
156
  response[:keys]
113
157
  ```
158
+
159
+ ### Keys - Report Usage
160
+
161
+ Report usage with keyCode
162
+
163
+ ```ruby
164
+ key = KeyReportRequest.new(
165
+ key: '00000000000000000000000000000000',
166
+ country_iso: 'US',
167
+ sale_date: DateTime.now,
168
+ user_ip_address: '182.212.212.22',
169
+ e_tailer_buying_price: 3.8,
170
+ e_tailer_buying_price_currency_code: 'USD',
171
+ e_tailer_selling_price_net: 3.2,
172
+ e_tailer_selling_price_gross: 3.8,
173
+ e_tailer_selling_price_currency_code: 'USD'
174
+ )
175
+
176
+ response = genba_client.keys.get_report_usage([key])
177
+
178
+ response[:acceptedCount]
179
+ response[:rejectedCount]
180
+ ```
181
+
182
+ Report usage with key
183
+ ```ruby
184
+ key = KeyReportRequest.new(
185
+ key_id: '00000000-ffff-2222-3333-444444444444',
186
+ country_iso: 'US',
187
+ sale_date: DateTime.now,
188
+ user_ip_address: '182.212.212.22',
189
+ e_tailer_buying_price: 3.8,
190
+ e_tailer_buying_price_currency_code: 'USD',
191
+ e_tailer_selling_price_net: 3.2,
192
+ e_tailer_selling_price_gross: 3.8,
193
+ e_tailer_selling_price_currency_code: 'USD'
194
+ )
195
+ key_res = @client.keys.get_report_usage([key])
196
+ response = genba_client.keys.get_report_usage([key])
197
+
198
+ response[:acceptedCount]
199
+ response[:rejectedCount]
200
+ ```
201
+
202
+ ## DirectEntitlement
203
+
204
+ ### DirectEntitlement - Activate
205
+
206
+ Use this method to get and activate keys for a Direct Entitlement SKU. You will be charged at the point of calling this method.
207
+
208
+ ```ruby
209
+ payload = {
210
+ sku_id: 'd972e0c7-5ddb-4e0d-9138-a78a6b269e99',
211
+ country_iso: 'US',
212
+ end_user_ip_address: '182.212.212.22',
213
+ sale_date: DateTime.now,
214
+ end_user_id: '0000001',
215
+ end_user_ticket: 'ticket0000001',
216
+ e_tailer_buying_price: 18.29,
217
+ e_tailer_buying_price_currency_code: 'USD',
218
+ e_tailer_selling_price_net: 19.89,
219
+ e_tailer_selling_price_gross: 20.29,
220
+ e_tailer_selling_price_currency_code: 'USD',
221
+ }
222
+ key_res = @client.direct_entitlements.activate(payload)
223
+ ```
224
+
225
+ ### DirectEntitlement - Redeem
226
+
227
+ Use this method to redeem keys already sold for a Direct Entitlement SKU and link them to the end-user's account. You will not be charged at the point of calling this method.
228
+
229
+ ```ruby
230
+ payload = {
231
+ sku_id: 'd972e0c7-5ddb-4e0d-9138-a78a6b269e99',
232
+ key_id: '00000000-0000-0000-0000-000000000000',
233
+ end_user_id: '00001',
234
+ end_user_ticket: 'ticket00001'
235
+ }
236
+ key_res = @client.direct_entitlements.redeem(payload)
237
+ ```
data/Rakefile CHANGED
File without changes
data/circle.yml CHANGED
File without changes
@@ -30,10 +30,9 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ['lib']
32
32
 
33
+ spec.add_runtime_dependency 'oj', '~> 3.5'
33
34
  spec.add_runtime_dependency 'rest-client', '~> 2.0'
34
35
  spec.add_runtime_dependency 'ruby-mcrypt', '0.2.0'
35
- spec.add_runtime_dependency 'oj', '~> 2.18'
36
-
37
36
 
38
37
  spec.add_development_dependency 'bundler', '~> 1.16'
39
38
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -2,24 +2,74 @@ require 'base64'
2
2
  require 'digest'
3
3
  require 'mcrypt'
4
4
  require 'rest-client'
5
+ require 'logger'
5
6
  require 'oj'
6
7
  require 'active_model'
7
8
 
8
- # Engine (for rails)
9
- require 'genba/engine' if defined?(Rails)
10
-
11
9
  require 'genba/version'
12
10
  require 'genba/key_black_list_request'
13
11
  require 'genba/key_report_request'
12
+ require 'genba/util'
13
+ require 'genba/client/direct_entitlements'
14
14
  require 'genba/client/keys'
15
15
  require 'genba/client/prices'
16
16
  require 'genba/client/products'
17
+ require 'genba/client/reports'
17
18
  require 'genba/client/restrictions'
18
19
  require 'genba/client'
19
20
 
20
21
  # Genba API module
21
22
  module Genba
23
+ @logger = nil
24
+
25
+ # map to the same values as the standard library's logger
26
+ LEVEL_DEBUG = Logger::DEBUG
27
+ LEVEL_ERROR = Logger::ERROR
28
+ LEVEL_INFO = Logger::INFO
29
+
22
30
  def self.client(credentials = {})
23
31
  Client.new(credentials)
24
32
  end
33
+
34
+ # When set prompts the library to log some extra information to $stdout and
35
+ # $stderr about what it's doing. For example, it'll produce information about
36
+ # requests, responses, and errors that are received. Valid log levels are
37
+ # `debug` and `info`, with `debug` being a little more verbose in places.
38
+ #
39
+ # Use of this configuration is only useful when `.logger` is _not_ set. When
40
+ # it is, the decision what levels to print is entirely deferred to the logger.
41
+ def self.log_level
42
+ @log_level
43
+ end
44
+
45
+ def self.log_level=(val)
46
+ # Backwards compatibility for values that we briefly allowed
47
+ if val == 'debug'
48
+ val = LEVEL_DEBUG
49
+ elsif val == 'info'
50
+ val = LEVEL_INFO
51
+ end
52
+
53
+ if !val.nil? && ![LEVEL_DEBUG, LEVEL_ERROR, LEVEL_INFO].include?(val)
54
+ raise ArgumentError, "log_level should only be set to `nil`, `debug` or `info`"
55
+ end
56
+ @log_level = val
57
+ end
58
+
59
+ # Sets a logger to which logging output will be sent. The logger should
60
+ # support the same interface as the `Logger` class that's part of Ruby's
61
+ # standard library (hint, anything in `Rails.logger` will likely be
62
+ # suitable).
63
+ #
64
+ # If `.logger` is set, the value of `.log_level` is ignored. The decision on
65
+ # what levels to print is entirely deferred to the logger.
66
+ def self.logger
67
+ @logger
68
+ end
69
+
70
+ def self.logger=(val)
71
+ @logger = val
72
+ end
25
73
  end
74
+
75
+ Genba.log_level = ENV['GENBA_LOG'] unless ENV['GENBA_LOG'].nil?
@@ -3,6 +3,9 @@
3
3
  module Genba
4
4
  # Genba API Client
5
5
  class Client
6
+ attr_accessor :customer_account_id,
7
+ :open_timeout, :read_timeout, :max_retry, :retry_delay
8
+
6
9
  API_URL = 'https://api.genbagames.com/api'.freeze
7
10
 
8
11
  @expires_on = nil
@@ -15,10 +18,15 @@ module Genba
15
18
  # * +config+ - Genba API credential attribute
16
19
  #
17
20
  # ==== Options
18
- def initialize(config = {})
19
- @app_id = config[:app_id]
20
- @username = config[:username]
21
- @api_key = config[:api_key]
21
+ def initialize(app_id:, username:, api_key:, customer_account_id:, options: {})
22
+ @app_id = app_id.strip
23
+ @username = username.strip
24
+ @api_key = api_key.strip
25
+ @customer_account_id = customer_account_id.strip
26
+ @open_timeout = options[:open_timeout] || 15
27
+ @read_timeout = options[:read_timeout] || 60
28
+ @max_retry = options[:max_retry] || 0
29
+ @retry_delay = options[:retry_delay] || 2
22
30
  end
23
31
 
24
32
  def generate_token
@@ -27,7 +35,7 @@ module Genba
27
35
  response = RestClient.post(
28
36
  "#{API_URL}/token",
29
37
  body,
30
- headers: { accept: 'application/json' }
38
+ headers: { accept: 'application/json', 'Content-Type': 'application/json' }
31
39
  )
32
40
  parsed_response = decode_json(response.body)
33
41
  @id_token = parsed_response['token']
@@ -36,25 +44,71 @@ module Genba
36
44
  raw_token
37
45
  end
38
46
 
39
- def rest_get_with_token(path, query_params = {}, headers = {})
47
+ def rest_get_with_token(path, query_params = {}, headers = {}, options = {})
40
48
  genba_headers = token.merge(headers)
41
- headers[:params] = query_params unless query_params.empty?
42
- response = RestClient.get("#{API_URL}#{path}", genba_headers)
49
+ api_url = "#{API_URL}#{path}"
50
+ api_url += "?#{query_params.to_query}" unless query_params.empty?
51
+ response = execute_request(method: :get, url: api_url,
52
+ headers: genba_headers, options: options)
43
53
  from_rest_client_response(response)
44
54
  end
45
55
 
46
- def rest_put_with_token(path, body = {}, headers = {})
56
+ def rest_put_with_token(path, body = {}, headers = {}, options = {})
47
57
  genba_headers = token.merge(headers)
48
- response = RestClient.put("#{API_URL}#{path}", encode_json(body), genba_headers)
58
+ response = execute_request(method: :put, url: "#{API_URL}#{path}",
59
+ payload: encode_json(body), headers: genba_headers, options: options)
49
60
  from_rest_client_response(response)
50
61
  end
51
62
 
52
- def rest_post_with_token(path, body = {}, headers = {})
63
+ def rest_post_with_token(path, body = {}, headers = {}, options = {})
53
64
  genba_headers = token.merge(headers)
54
- response = RestClient.post("#{API_URL}#{path}", encode_json(body), genba_headers)
65
+ response = execute_request(method: :post, url: "#{API_URL}#{path}",
66
+ payload: encode_json(body), headers: genba_headers, options: options)
55
67
  from_rest_client_response(response)
56
68
  end
57
69
 
70
+ def execute_request(method:, url:, payload: {}, headers: {}, options: {})
71
+ request_opts = {
72
+ headers: headers,
73
+ method: method,
74
+ payload: payload,
75
+ url: url
76
+ }
77
+ other_opts = {
78
+ open_timeout: options[:open_timeout] || @open_timeout,
79
+ read_timeout: options[:read_timeout] || @read_timeout,
80
+ max_retry: options[:max_retry] || @max_retry
81
+ }
82
+
83
+ Genba::Util.log_debug "API Headers: #{headers.inspect}"
84
+ Genba::Util.log_debug "Options: #{other_opts}"
85
+ Genba::Util.log_info "#{method.upcase}: #{url}"
86
+ Genba::Util.log_info "payload: #{payload}" if payload.present?
87
+
88
+ request_opts.merge! other_opts
89
+ execute_request_with_rescues(request_opts, other_opts[:max_retry])
90
+ end
91
+
92
+ def execute_request_with_rescues(request_opts, max_retry)
93
+ num_try = 0
94
+ begin
95
+ response = RestClient::Request.execute(request_opts)
96
+ rescue StandardError => e
97
+ Genba::Util.log_error "#{e.class} => #{e.message}"
98
+
99
+ num_try += 1
100
+ sleep @retry_delay
101
+
102
+ if num_try <= max_retry
103
+ Genba::Util.log_error "retry ====> #{num_try}"
104
+ retry
105
+ end
106
+
107
+ raise e
108
+ end
109
+ response
110
+ end
111
+
58
112
  def products
59
113
  Products.new(self)
60
114
  end
@@ -71,6 +125,14 @@ module Genba
71
125
  Keys.new(self)
72
126
  end
73
127
 
128
+ def direct_entitlements
129
+ DirectEntitlements.new(self)
130
+ end
131
+
132
+ def reports
133
+ Reports.new(self)
134
+ end
135
+
74
136
  private
75
137
 
76
138
  def encode_json(data)
@@ -97,7 +159,8 @@ module Genba
97
159
  {
98
160
  token: @id_token,
99
161
  appId: @app_id,
100
- accept: 'application/json'
162
+ accept: 'application/json',
163
+ 'Content-Type': 'application/json'
101
164
  }
102
165
  else
103
166
  {}
@@ -116,12 +179,15 @@ module Genba
116
179
  def from_rest_client_response(response)
117
180
  if response.code < 200 &&
118
181
  response.code >= 400
182
+ Genba::Util.log_error "Invalid response object from API: #{response.body}" \
183
+ "(HTTP response code was #{response.code})"
119
184
  raise "Invalid response object from API: #{response.body}" \
120
185
  "(HTTP response code was #{response.code})"
121
186
  end
122
187
  # default decode by json
123
188
  return decode_json(response.body) unless response.headers[:content_type]
124
- if (response.headers[:content_type] =~ %r{application\/json}) > 0
189
+ if (response.headers[:content_type] =~ %r{application\/json}) >= 0
190
+ Genba::Util.log_info "response body\n#{response.body}"
125
191
  decode_json(response.body)
126
192
  end
127
193
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Genba
4
+ class Client
5
+ # DirectEntitlements client
6
+ class DirectEntitlements
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ def activate(
12
+ sku_id:,
13
+ country_iso:,
14
+ end_user_ip_address:,
15
+ sale_date: DateTime.now,
16
+ end_user_id: nil,
17
+ end_user_ticket: nil,
18
+ e_tailer_buying_price:,
19
+ e_tailer_buying_price_currency_code:,
20
+ e_tailer_selling_price_net:,
21
+ e_tailer_selling_price_gross:,
22
+ e_tailer_selling_price_currency_code:,
23
+ headers: {},
24
+ options: {}
25
+ )
26
+ params = {
27
+ skuId: sku_id,
28
+ customerAccountId: @client.customer_account_id,
29
+ countryISO: country_iso,
30
+ endUserIpAddress: end_user_ip_address,
31
+ endUserId: end_user_id,
32
+ endUserTicket: end_user_ticket,
33
+ ETailerBuyingPrice: e_tailer_buying_price,
34
+ ETailerBuyingPriceCurrencyCode: e_tailer_buying_price_currency_code,
35
+ ETailerSellingPriceNet: e_tailer_selling_price_net,
36
+ ETailerSellingPriceGross: e_tailer_selling_price_gross,
37
+ ETailerSellingPriceCurrencyCode: e_tailer_selling_price_currency_code
38
+ }.select { |_, v| !v.nil? }
39
+ params[:saleDate] = sale_date.strftime('%FT%T') if sale_date
40
+ Genba::Util.log_debug "DirectEntitlements activate payload: #{params.inspect}"
41
+ @client.rest_get_with_token('/directentitlement/activate', params, headers, options)
42
+ end
43
+
44
+ # Use this method to redeem keys already sold for a Direct Entitlement SKU and link them to the
45
+ # end-user's account. You will not be charged at the point of calling this method.
46
+ def redeem(sku_id:, key_id:, end_user_id:, end_user_ticket:, headers: {}, options: {})
47
+ params = {
48
+ customerAccountId: @client.customer_account_id,
49
+ skuId: sku_id,
50
+ keyId: key_id,
51
+ endUserId: end_user_id,
52
+ endUserTicket: end_user_ticket
53
+ }
54
+ @client.rest_get_with_token('/directentitlement/redeem', params, headers, options)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -15,36 +15,35 @@ module Genba
15
15
  @client.rest_get_with_token('/testKeys', params, headers)
16
16
  end
17
17
 
18
- def get_keys(sku_id, quantity = 1, params = {}, headers = {})
18
+ def get_keys(sku_id, quantity = 1, params = {}, headers = {}, options: {})
19
19
  payload = params.merge(
20
20
  skuId: sku_id,
21
- quantity: quantity
21
+ quantity: quantity,
22
+ customerAccountId: @client.customer_account_id
22
23
  )
23
- @client.rest_get_with_token('/keys', payload, headers)
24
+ @client.rest_get_with_token('/keys', payload, headers, options)
24
25
  end
25
26
 
26
- def get_key_code_status(key_code, params = {}, headers = {})
27
+ def get_key_code_status(key_code, params = {}, headers = {}, options: {})
27
28
  payload = params.merge(
28
29
  keyCode: key_code
29
30
  )
30
- @client.rest_get_with_token('/keys', payload, headers)
31
+ @client.rest_get_with_token('/keys', payload, headers, options)
31
32
  end
32
33
 
33
- def get_key_status(key_id, params = {}, headers = {})
34
- payload = params.merge(
35
- id: key_id
36
- )
37
- @client.rest_get_with_token('/keys', payload, headers)
34
+ def get_key_status(key_id, params = {}, headers = {}, options: {})
35
+ @client.rest_get_with_token("/keys/#{key_id}", params, headers, options)
38
36
  end
39
37
 
40
- def get_report_usage(keys = nil, headers = {})
38
+ def get_report_usage(keys = nil, headers = {}, options: {})
41
39
  raise 'ReportUsage keys should be array' unless keys.is_a?(Array)
42
40
  raise 'ReportUseag keys should be a KeyReportRequest class' unless key_report_request?(keys)
43
41
  payload = keys.map(&:to_genba_json_payload)
44
- @client.rest_post_with_token('/keyReport', payload, headers)
42
+ Genba::Util.log_debug "get_report_usage payload: #{payload.inspect}"
43
+ @client.rest_post_with_token('/keyReport', payload, headers, options)
45
44
  end
46
45
 
47
- def black_list(keys = nil, headers = {})
46
+ def black_list(keys = nil, headers = {}, options: {})
48
47
  raise 'Blacklist keys should be array' unless keys.is_a?(Array)
49
48
  raise 'Blacklist keys should be a KeyBlackListRequest class' unless key_black_list_request?(keys)
50
49
 
@@ -53,7 +52,7 @@ module Genba
53
52
  end
54
53
 
55
54
  payload = keys.map(&:to_genba_json_payload)
56
- @client.rest_post_with_token('/blackListKeys', payload, headers)
55
+ @client.rest_post_with_token('/blackListKeys', payload, headers, options)
57
56
  end
58
57
 
59
58
  private
@@ -8,11 +8,11 @@ module Genba
8
8
  @client = client
9
9
  end
10
10
 
11
- def get_prices(customer_account_id, headers = {})
12
- params = {
13
- customerAccountId: customer_account_id
14
- }
15
- @client.rest_get_with_token('/prices', params, headers)
11
+ def get_prices(params = {}, headers = {})
12
+ payload = params.merge(
13
+ customerAccountId: @client.customer_account_id
14
+ )
15
+ @client.rest_get_with_token('/prices', payload, headers)
16
16
  end
17
17
  end
18
18
  end
@@ -8,19 +8,32 @@ module Genba
8
8
  @client = client
9
9
  end
10
10
 
11
- def get_products(params = {}, headers = {})
12
- @client.rest_get_with_token('/product', params, headers)
11
+ def get_products(sku_id: nil, country_iso: nil, include_meta: false, headers: {})
12
+ payload = {
13
+ skuId: sku_id,
14
+ countryISO: country_iso,
15
+ includeMeta: include_meta
16
+ }.select { |_, v| !v.nil? }
17
+
18
+ @client.rest_get_with_token('/product', payload, headers)
13
19
  end
14
20
 
15
- def get_changes(from_date, params = {}, headers = {})
21
+ def get_changes(from_date: nil, country_iso: nil, include_meta: false, params: {}, headers: {})
16
22
  payload = params.merge(
17
- fromDate: from_date
18
- )
23
+ countryISO: country_iso,
24
+ includeMeta: include_meta
25
+ ).select { |_, v| !v.nil? }
26
+ payload[:fromDate] = from_date.strftime('%FT%T') if from_date
19
27
  @client.rest_get_with_token('/product/changes', payload, headers)
20
28
  end
21
29
 
22
- def get_removed(params = {}, headers = {})
23
- @client.rest_get_with_token('/product/removed', params, headers)
30
+ def get_removed(from_date: nil, country_iso: nil, params: {}, headers: {})
31
+ payload = params.merge(
32
+ countryISO: country_iso,
33
+ customerAccountId: @client.customer_account_id
34
+ ).select { |_, v| !v.nil? }
35
+ payload[:fromDate] = from_date.strftime('%FT%T') if from_date
36
+ @client.rest_get_with_token('/product/removed', payload, headers)
24
37
  end
25
38
  end
26
39
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Genba
4
+ class Client
5
+ # Reports client
6
+ class Reports
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ def publisher_raw_data(year = nil, params = {}, headers = {})
12
+ payload = params.merge({
13
+ Year: year || Time.now.strftime('%Y')
14
+ })
15
+ @client.rest_get_with_token('/report/publisher/rawdata', payload, headers)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -8,8 +8,13 @@ module Genba
8
8
  @client = client
9
9
  end
10
10
 
11
- def get_restrictions(params = {}, headers = {})
12
- @client.rest_get_with_token('/restrictions', params, headers)
11
+ def get_restrictions(product_id: nil, country_iso: nil, from_date: nil, params: {}, headers: {})
12
+ payload = params.merge(
13
+ productId: product_id,
14
+ countryISO: country_iso
15
+ ).select { |_, v| !v.nil? }
16
+ payload[:fromDate] = from_date.strftime('%FT%T') if from_date
17
+ @client.rest_get_with_token('/restrictions', payload, headers)
13
18
  end
14
19
  end
15
20
  end
File without changes
@@ -5,27 +5,44 @@ class KeyReportRequest
5
5
  attr_accessor :key, :key_id, :country_iso, :sale_date, :user_ip_address,
6
6
  :e_tailer_buying_price, :e_tailer_buying_price_currency_code,
7
7
  :e_tailer_selling_price_net, :e_tailer_selling_price_gross,
8
- :e_tailer_selling_price_currency_code
8
+ :e_tailer_selling_price_currency_code,
9
+ :e_tailer_subsidiary
9
10
 
10
- def initialize(data = {})
11
- @key = data[:key]
12
- @key_id = data[:key_id]
13
- @country_iso = data[:country_iso]
14
- @user_ip_address = data[:user_ip_address]
11
+ def initialize(
12
+ key: nil, key_id: nil, country_iso:, user_ip_address:, sale_date: nil,
13
+ e_tailer_buying_price:, e_tailer_buying_price_currency_code:,
14
+ e_tailer_selling_price_net:, e_tailer_selling_price_gross:, e_tailer_selling_price_currency_code:,
15
+ e_tailer_subsidiary: nil
16
+ )
17
+ @key = key
18
+ @key_id = key_id
19
+ @country_iso = country_iso
20
+ @user_ip_address = user_ip_address
21
+ @sale_date = sale_date
22
+ @e_tailer_buying_price = e_tailer_buying_price
23
+ @e_tailer_buying_price_currency_code = e_tailer_buying_price_currency_code
24
+ @e_tailer_selling_price_net = e_tailer_selling_price_net
25
+ @e_tailer_selling_price_gross = e_tailer_selling_price_gross
26
+ @e_tailer_selling_price_currency_code = e_tailer_selling_price_currency_code
27
+ @e_tailer_subsidiary = e_tailer_subsidiary
28
+ raise 'at least one of key or key_id' if key.nil? && key_id.nil?
15
29
  end
16
30
 
17
31
  def to_genba_json_payload
18
- {
32
+ payload ={
19
33
  key: @key,
20
34
  keyId: @key_id,
21
35
  countryISO: @country_iso,
22
- saleDate: @sale_date,
23
36
  userIpAddress: @user_ip_address,
24
37
  ETailerBuyingPrice: @e_tailer_buying_price,
25
38
  ETailerBuyingPriceCurrencyCode: @e_tailer_buying_price_currency_code,
26
39
  ETailerSellingPriceNet: @e_tailer_selling_price_net,
27
40
  ETailerSellingPriceGross: @e_tailer_selling_price_gross,
28
- ETailerSellingPriceCurrencyCode: @e_tailer_selling_price_currency_code
41
+ ETailerSellingPriceCurrencyCode: @e_tailer_selling_price_currency_code,
42
+ EtailerSubsidiary: @e_tailer_subsidiary
29
43
  }.select { |_, v| !v.nil? }
44
+
45
+ payload[:saleDate] = @sale_date.strftime('%FT%T') if @sale_date
46
+ payload
30
47
  end
31
48
  end
@@ -0,0 +1,106 @@
1
+ require 'cgi'
2
+
3
+ module Genba
4
+ module Util
5
+ def self.log_error(message, data = {})
6
+ if !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_ERROR
7
+ log_internal(message, data, color: :cyan,
8
+ level: Genba::LEVEL_ERROR, logger: Genba.logger, out: $stderr)
9
+ end
10
+ end
11
+
12
+ def self.log_info(message, data = {})
13
+ if !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_INFO
14
+ log_internal(message, data, color: :cyan,
15
+ level: Genba::LEVEL_INFO, logger: Genba.logger, out: $stdout)
16
+ end
17
+ end
18
+
19
+ def self.log_debug(message, data = {})
20
+ if !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_DEBUG
21
+ log_internal(message, data, color: :blue,
22
+ level: Genba::LEVEL_DEBUG, logger: Genba.logger, out: $stdout)
23
+ end
24
+ end
25
+
26
+ COLOR_CODES = {
27
+ black: 0, light_black: 60,
28
+ red: 1, light_red: 61,
29
+ green: 2, light_green: 62,
30
+ yellow: 3, light_yellow: 63,
31
+ blue: 4, light_blue: 64,
32
+ magenta: 5, light_magenta: 65,
33
+ cyan: 6, light_cyan: 66,
34
+ white: 7, light_white: 67,
35
+ default: 9
36
+ }.freeze
37
+ private_constant :COLOR_CODES
38
+
39
+ # Uses an ANSI escape code to colorize text if it's going to be sent to a
40
+ # TTY.
41
+ def self.colorize(val, color, isatty)
42
+ return val unless isatty
43
+
44
+ mode = 0 # default
45
+ foreground = 30 + COLOR_CODES.fetch(color)
46
+ background = 40 + COLOR_CODES.fetch(:default)
47
+
48
+ "\033[#{mode};#{foreground};#{background}m#{val}\033[0m"
49
+ end
50
+ private_class_method :colorize
51
+
52
+ # Turns an integer log level into a printable name.
53
+ def self.level_name(level)
54
+ case level
55
+ when LEVEL_DEBUG then 'debug'
56
+ when LEVEL_ERROR then 'error'
57
+ when LEVEL_INFO then 'info'
58
+ else level
59
+ end
60
+ end
61
+ private_class_method :level_name
62
+
63
+ # TODO: Make these named required arguments when we drop support for Ruby
64
+ # 2.0.
65
+ def self.log_internal(message, data = {}, color: nil, level: nil, logger: nil, out: nil)
66
+ data_str = data.reject { |_k, v| v.nil? }
67
+ .map do |(k, v)|
68
+ format("%s=%s", colorize(k, color, logger.nil? && !out.nil? && out.isatty), wrap_logfmt_value(v))
69
+ end.join(' ')
70
+
71
+ if !logger.nil?
72
+ # the library's log levels are mapped to the same values as the
73
+ # standard library's logger
74
+ logger.log(level, message)
75
+ elsif out.isatty
76
+ out.puts format("%s %s %s", colorize(level_name(level)[0, 4].upcase, color, out.isatty), message, data_str)
77
+ else
78
+ out.puts format("message=%s level=%s %s", wrap_logfmt_value(message), level_name(level), data_str)
79
+ end
80
+ end
81
+ private_class_method :log_internal
82
+
83
+ # Wraps a value in double quotes if it looks sufficiently complex so that
84
+ # it can be read by logfmt parsers.
85
+ def self.wrap_logfmt_value(val)
86
+ # If value is any kind of number, just allow it to be formatted directly
87
+ # to a string (this will handle integers or floats).
88
+ return val if val.is_a?(Numeric)
89
+
90
+ # Hopefully val is a string, but protect in case it's not.
91
+ val = val.to_s
92
+
93
+ if %r{[^\w\-/]} =~ val
94
+ # If the string contains any special characters, escape any double
95
+ # quotes it has, remove newlines, and wrap the whole thing in quotes.
96
+ format(%("%s"), val.gsub('"', '\"').delete("\n"))
97
+ else
98
+ # Otherwise use the basic value if it looks like a standard set of
99
+ # characters (and allow a few special characters like hyphens, and
100
+ # slashes)
101
+ val
102
+ end
103
+ end
104
+ private_class_method :wrap_logfmt_value
105
+ end
106
+ end
@@ -1,3 +1,3 @@
1
1
  module Genba
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.1.11'.freeze
3
3
  end
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genba-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Lin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-11 00:00:00.000000000 Z
11
+ date: 2019-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rest-client
14
+ name: oj
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '3.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '3.5'
27
27
  - !ruby/object:Gem::Dependency
28
- name: ruby-mcrypt
28
+ name: rest-client
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.2.0
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.2.0
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: oj
42
+ name: ruby-mcrypt
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: '2.18'
47
+ version: 0.2.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: '2.18'
54
+ version: 0.2.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -101,6 +101,7 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
+ - ".circleci/config.yml"
104
105
  - ".gitignore"
105
106
  - ".rspec"
106
107
  - ".travis.yml"
@@ -113,16 +114,18 @@ files:
113
114
  - bin/console
114
115
  - bin/setup
115
116
  - circle.yml
116
- - genba-ruby.gemspec
117
+ - genba.gemspec
117
118
  - lib/genba.rb
118
119
  - lib/genba/client.rb
120
+ - lib/genba/client/direct_entitlements.rb
119
121
  - lib/genba/client/keys.rb
120
122
  - lib/genba/client/prices.rb
121
123
  - lib/genba/client/products.rb
124
+ - lib/genba/client/reports.rb
122
125
  - lib/genba/client/restrictions.rb
123
- - lib/genba/engine.rb
124
126
  - lib/genba/key_black_list_request.rb
125
127
  - lib/genba/key_report_request.rb
128
+ - lib/genba/util.rb
126
129
  - lib/genba/version.rb
127
130
  homepage: https://api.genbagames.com/doc/
128
131
  licenses:
@@ -145,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
148
  version: '0'
146
149
  requirements: []
147
150
  rubyforge_project:
148
- rubygems_version: 2.6.14
151
+ rubygems_version: 2.7.8
149
152
  signing_key:
150
153
  specification_version: 4
151
154
  summary: genba api for ruby version
@@ -1,5 +0,0 @@
1
- module Genba
2
- class Engine < ::Rails::Engine
3
- isolate_namespace Genba
4
- end
5
- end