Class: Genba::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/genba/client.rb,
lib/genba/client/keys.rb,
lib/genba/client/prices.rb,
lib/genba/client/reports.rb,
lib/genba/client/products.rb,
lib/genba/client/restrictions.rb,
lib/genba/client/direct_entitlements.rb

Overview

Genba API Client

Defined Under Namespace

Classes: DirectEntitlements, Keys, Prices, Products, Reports, Restrictions

Constant Summary collapse

API_URL =
'https://api.genbagames.com/api'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id:, username:, api_key:, customer_account_id:, options: {}) ⇒ Client

Desribe the behaviour of the method

Attributes

  • config - Genba API credential attribute

Options



21
22
23
24
25
26
27
28
29
30
# File 'lib/genba/client.rb', line 21

def initialize(app_id:, username:, api_key:, customer_account_id:, options: {})
  @app_id = app_id.strip
  @username = username.strip
  @api_key = api_key.strip
  @customer_account_id = .strip
  @open_timeout = options[:open_timeout] || 15
  @read_timeout = options[:read_timeout] || 60
  @max_retry = options[:max_retry] || 0
  @retry_delay = options[:retry_delay] || 2
end

Instance Attribute Details

#customer_account_idObject

Returns the value of attribute customer_account_id.



6
7
8
# File 'lib/genba/client.rb', line 6

def 
  @customer_account_id
end

#max_retryObject

Returns the value of attribute max_retry.



6
7
8
# File 'lib/genba/client.rb', line 6

def max_retry
  @max_retry
end

#open_timeoutObject

Returns the value of attribute open_timeout.



6
7
8
# File 'lib/genba/client.rb', line 6

def open_timeout
  @open_timeout
end

#read_timeoutObject

Returns the value of attribute read_timeout.



6
7
8
# File 'lib/genba/client.rb', line 6

def read_timeout
  @read_timeout
end

#retry_delayObject

Returns the value of attribute retry_delay.



6
7
8
# File 'lib/genba/client.rb', line 6

def retry_delay
  @retry_delay
end

Instance Method Details

#direct_entitlementsObject



128
129
130
# File 'lib/genba/client.rb', line 128

def direct_entitlements
  DirectEntitlements.new(self)
end

#execute_request(method:, url:, payload: {}, headers: {}, options: {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/genba/client.rb', line 70

def execute_request(method:, url:, payload: {}, headers: {}, options: {})
  request_opts = {
    headers: headers,
    method: method,
    payload: payload,
    url: url
  }
  other_opts = {
    open_timeout: options[:open_timeout] || @open_timeout,
    read_timeout: options[:read_timeout] || @read_timeout,
    max_retry: options[:max_retry] || @max_retry
  }

  Genba::Util.log_debug "API Headers: #{headers.inspect}"
  Genba::Util.log_debug "Options: #{other_opts}"
  Genba::Util.log_info "#{method.upcase}: #{url}"
  Genba::Util.log_info "payload: #{payload}" if payload.present?

  request_opts.merge! other_opts
  execute_request_with_rescues(request_opts, other_opts[:max_retry])
end

#execute_request_with_rescues(request_opts, max_retry) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/genba/client.rb', line 92

def execute_request_with_rescues(request_opts, max_retry)
  num_try = 0
  begin
    response = RestClient::Request.execute(request_opts)
  rescue StandardError => e
    Genba::Util.log_error "#{e.class} => #{e.message}"

    num_try += 1
    sleep @retry_delay

    if num_try <= max_retry
      Genba::Util.log_error "retry ====> #{num_try}"
      retry
    end

    raise e
  end
  response
end

#generate_tokenObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/genba/client.rb', line 32

def generate_token
  unless token_valid?
    body = { appId: @app_id, signature: genba_signature }
    response = RestClient.post(
      "#{API_URL}/token",
      body,
      headers: { accept: 'application/json', 'Content-Type': 'application/json' }
    )
    parsed_response = decode_json(response.body)
    @id_token = parsed_response['token']
    @expires_on = Time.parse(parsed_response['expiration'])
  end
  raw_token
end

#keysObject



124
125
126
# File 'lib/genba/client.rb', line 124

def keys
  Keys.new(self)
end

#pricesObject



116
117
118
# File 'lib/genba/client.rb', line 116

def prices
  Prices.new(self)
end

#productsObject



112
113
114
# File 'lib/genba/client.rb', line 112

def products
  Products.new(self)
end

#reportsObject



132
133
134
# File 'lib/genba/client.rb', line 132

def reports
  Reports.new(self)
end

#rest_get_with_token(path, query_params = {}, headers = {}, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/genba/client.rb', line 47

def rest_get_with_token(path, query_params = {}, headers = {}, options = {})
  genba_headers = token.merge(headers)
  api_url = "#{API_URL}#{path}"
  api_url += "?#{query_params.to_query}" unless query_params.empty?
  response = execute_request(method: :get, url: api_url,
                             headers: genba_headers, options: options)
  from_rest_client_response(response)
end

#rest_post_with_token(path, body = {}, headers = {}, options = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/genba/client.rb', line 63

def rest_post_with_token(path, body = {}, headers = {}, options = {})
  genba_headers = token.merge(headers)
  response = execute_request(method: :post, url: "#{API_URL}#{path}",
                             payload: encode_json(body), headers: genba_headers, options: options)
  from_rest_client_response(response)
end

#rest_put_with_token(path, body = {}, headers = {}, options = {}) ⇒ Object



56
57
58
59
60
61
# File 'lib/genba/client.rb', line 56

def rest_put_with_token(path, body = {}, headers = {}, options = {})
  genba_headers = token.merge(headers)
  response = execute_request(method: :put, url: "#{API_URL}#{path}",
                             payload: encode_json(body), headers: genba_headers, options: options)
  from_rest_client_response(response)
end

#restrictionsObject



120
121
122
# File 'lib/genba/client.rb', line 120

def restrictions
  Restrictions.new(self)
end