genba 0.1.9 → 0.1.10

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
  SHA256:
3
- metadata.gz: f5008fd2a2caff600d01dabd759250bd042f79161810139434ad10d97b39cb54
4
- data.tar.gz: 0a82e0e2cc12ed8319eadb0c3feea05d2457bf6c0864bb0153662963dd1387b8
3
+ metadata.gz: 2549766d6aa78a612a97e0dfde945efb957a5d7ed5fed603b94a063cb09aa89f
4
+ data.tar.gz: 615bb36c8c85adec6543f009f92feda96ff4efcd503ff9b4b6324474f7bf81ea
5
5
  SHA512:
6
- metadata.gz: 5929edb64582e34f60093337c76bd70f2e172837c34e4afeaad6a6cf722976149c07b3b1cc1184f1ab8337a5d492c48aa2a25730590eeb6a84978e1f9c6740b8
7
- data.tar.gz: c3b2fcd0539b9d7649c3723e9b8ca5bbda1391611c036bc259bfec34f081c5d4e54c48c50f64e6cdb7750d023cb2d8c15678ab8db62102b6f440e96e1ab7e40a
6
+ metadata.gz: 4fa85bc6bf5e698ed5afb5c51ef550a271d0ca4fd9e2c0973c91a3bbc692e7683e0b887058127d9e80f362ebb46a0f1bda26eb860025f6ee96fbf0e81fa5e511
7
+ data.tar.gz: 107cd3b48f5980531389d8a38e88f55d296ebc90d5d739fafb1d297c5a6937691568b58d35dbeb8e48479f4177754d7696ae6ffe2de7405605e9e5f9c15c2dfc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- genba (0.1.9)
4
+ genba (0.1.10)
5
5
  oj (~> 3.5)
6
6
  rest-client (~> 2.0)
7
7
  ruby-mcrypt (= 0.2.0)
@@ -70,7 +70,7 @@ 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
74
  uuidtools (2.1.5)
75
75
  webmock (3.4.1)
76
76
  addressable (>= 2.3.6)
@@ -93,4 +93,4 @@ DEPENDENCIES
93
93
  webmock
94
94
 
95
95
  BUNDLED WITH
96
- 1.17.1
96
+ 1.17.3
@@ -20,7 +20,8 @@ module Genba
20
20
  e_tailer_selling_price_net:,
21
21
  e_tailer_selling_price_gross:,
22
22
  e_tailer_selling_price_currency_code:,
23
- headers: {}
23
+ headers: {},
24
+ options: {}
24
25
  )
25
26
  params = {
26
27
  skuId: sku_id,
@@ -37,12 +38,12 @@ module Genba
37
38
  }.select { |_, v| !v.nil? }
38
39
  params[:saleDate] = sale_date.strftime('%FT%T') if sale_date
39
40
  Genba::Util.log_debug "DirectEntitlements activate payload: #{params.inspect}"
40
- @client.rest_get_with_token('/directentitlement/activate', params, headers)
41
+ @client.rest_get_with_token('/directentitlement/activate', params, headers, options)
41
42
  end
42
43
 
43
44
  # Use this method to redeem keys already sold for a Direct Entitlement SKU and link them to the
44
45
  # end-user's account. You will not be charged at the point of calling this method.
45
- def redeem(sku_id:, key_id:, end_user_id:, end_user_ticket:, headers: {})
46
+ def redeem(sku_id:, key_id:, end_user_id:, end_user_ticket:, headers: {}, options: {})
46
47
  params = {
47
48
  customerAccountId: @client.customer_account_id,
48
49
  skuId: sku_id,
@@ -50,7 +51,7 @@ module Genba
50
51
  endUserId: end_user_id,
51
52
  endUserTicket: end_user_ticket
52
53
  }
53
- @client.rest_get_with_token('/directentitlement/redeem', params, headers)
54
+ @client.rest_get_with_token('/directentitlement/redeem', params, headers, options)
54
55
  end
55
56
  end
56
57
  end
@@ -15,35 +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
21
  quantity: quantity,
22
22
  customerAccountId: @client.customer_account_id
23
23
  )
24
- @client.rest_get_with_token('/keys', payload, headers)
24
+ @client.rest_get_with_token('/keys', payload, headers, options)
25
25
  end
26
26
 
27
- def get_key_code_status(key_code, params = {}, headers = {})
27
+ def get_key_code_status(key_code, params = {}, headers = {}, options: {})
28
28
  payload = params.merge(
29
29
  keyCode: key_code
30
30
  )
31
- @client.rest_get_with_token('/keys', payload, headers)
31
+ @client.rest_get_with_token('/keys', payload, headers, options)
32
32
  end
33
33
 
34
- def get_key_status(key_id, params = {}, headers = {})
35
- @client.rest_get_with_token("/keys/#{key_id}", params, headers)
34
+ def get_key_status(key_id, params = {}, headers = {}, options: {})
35
+ @client.rest_get_with_token("/keys/#{key_id}", params, headers, options)
36
36
  end
37
37
 
38
- def get_report_usage(keys = nil, headers = {})
38
+ def get_report_usage(keys = nil, headers = {}, options: {})
39
39
  raise 'ReportUsage keys should be array' unless keys.is_a?(Array)
40
40
  raise 'ReportUseag keys should be a KeyReportRequest class' unless key_report_request?(keys)
41
41
  payload = keys.map(&:to_genba_json_payload)
42
42
  Genba::Util.log_debug "get_report_usage payload: #{payload.inspect}"
43
- @client.rest_post_with_token('/keyReport', payload, headers)
43
+ @client.rest_post_with_token('/keyReport', payload, headers, options)
44
44
  end
45
45
 
46
- def black_list(keys = nil, headers = {})
46
+ def black_list(keys = nil, headers = {}, options: {})
47
47
  raise 'Blacklist keys should be array' unless keys.is_a?(Array)
48
48
  raise 'Blacklist keys should be a KeyBlackListRequest class' unless key_black_list_request?(keys)
49
49
 
@@ -52,7 +52,7 @@ module Genba
52
52
  end
53
53
 
54
54
  payload = keys.map(&:to_genba_json_payload)
55
- @client.rest_post_with_token('/blackListKeys', payload, headers)
55
+ @client.rest_post_with_token('/blackListKeys', payload, headers, options)
56
56
  end
57
57
 
58
58
  private
data/lib/genba/client.rb CHANGED
@@ -3,7 +3,8 @@
3
3
  module Genba
4
4
  # Genba API Client
5
5
  class Client
6
- attr_accessor :customer_account_id
6
+ attr_accessor :customer_account_id,
7
+ :open_timeout, :read_timeout, :max_retry, :retry_delay
7
8
 
8
9
  API_URL = 'https://api.genbagames.com/api'.freeze
9
10
 
@@ -17,11 +18,15 @@ module Genba
17
18
  # * +config+ - Genba API credential attribute
18
19
  #
19
20
  # ==== Options
20
- def initialize(app_id:, username:, api_key:, customer_account_id:)
21
+ def initialize(app_id:, username:, api_key:, customer_account_id:, options: {})
21
22
  @app_id = app_id.strip
22
23
  @username = username.strip
23
24
  @api_key = api_key.strip
24
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
25
30
  end
26
31
 
27
32
  def generate_token
@@ -39,33 +44,71 @@ module Genba
39
44
  raw_token
40
45
  end
41
46
 
42
- def rest_get_with_token(path, query_params = {}, headers = {})
47
+ def rest_get_with_token(path, query_params = {}, headers = {}, options = {})
43
48
  genba_headers = token.merge(headers)
44
- Genba::Util.log_debug "API Headers: #{genba_headers.inspect}"
45
49
  api_url = "#{API_URL}#{path}"
46
50
  api_url += "?#{query_params.to_query}" unless query_params.empty?
47
- Genba::Util.log_info "api_url: #{api_url}"
48
- response = RestClient.get(api_url, genba_headers)
51
+ response = execute_request(method: :get, url: api_url,
52
+ headers: genba_headers, options: options)
49
53
  from_rest_client_response(response)
50
54
  end
51
55
 
52
- def rest_put_with_token(path, body = {}, headers = {})
56
+ def rest_put_with_token(path, body = {}, headers = {}, options = {})
53
57
  genba_headers = token.merge(headers)
54
- Genba::Util.log_debug "API Headers: #{genba_headers.inspect}"
55
- Genba::Util.log_info "api_url: #{API_URL}#{path}"
56
- 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)
57
60
  from_rest_client_response(response)
58
61
  end
59
62
 
60
- def rest_post_with_token(path, body = {}, headers = {})
63
+ def rest_post_with_token(path, body = {}, headers = {}, options = {})
61
64
  genba_headers = token.merge(headers)
62
- Genba::Util.log_debug "API Headers: #{genba_headers.inspect}"
63
- Genba::Util.log_info "api_url: #{API_URL}#{path}"
64
- Genba::Util.log_info "body: #{body}"
65
- 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)
66
67
  from_rest_client_response(response)
67
68
  end
68
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
+
69
112
  def products
70
113
  Products.new(self)
71
114
  end
data/lib/genba/util.rb CHANGED
@@ -3,24 +3,21 @@ require 'cgi'
3
3
  module Genba
4
4
  module Util
5
5
  def self.log_error(message, data = {})
6
- if !Genba.logger.nil? ||
7
- !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_ERROR
6
+ if !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_ERROR
8
7
  log_internal(message, data, color: :cyan,
9
8
  level: Genba::LEVEL_ERROR, logger: Genba.logger, out: $stderr)
10
9
  end
11
10
  end
12
11
 
13
12
  def self.log_info(message, data = {})
14
- if !Genba.logger.nil? ||
15
- !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_INFO
13
+ if !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_INFO
16
14
  log_internal(message, data, color: :cyan,
17
15
  level: Genba::LEVEL_INFO, logger: Genba.logger, out: $stdout)
18
16
  end
19
17
  end
20
18
 
21
19
  def self.log_debug(message, data = {})
22
- if !Genba.logger.nil? ||
23
- !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_DEBUG
20
+ if !Genba.log_level.nil? && Genba.log_level <= Genba::LEVEL_DEBUG
24
21
  log_internal(message, data, color: :blue,
25
22
  level: Genba::LEVEL_DEBUG, logger: Genba.logger, out: $stdout)
26
23
  end
@@ -35,7 +32,7 @@ module Genba
35
32
  magenta: 5, light_magenta: 65,
36
33
  cyan: 6, light_cyan: 66,
37
34
  white: 7, light_white: 67,
38
- default: 9,
35
+ default: 9
39
36
  }.freeze
40
37
  private_constant :COLOR_CODES
41
38
 
data/lib/genba/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Genba
2
- VERSION = '0.1.9'.freeze
2
+ VERSION = '0.1.10'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Lin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-17 00:00:00.000000000 Z
11
+ date: 2019-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj