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/products.rb,
lib/genba/client/restrictions.rb

Overview

Genba API Client

Defined Under Namespace

Classes: Keys, Prices, Products, Restrictions

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Desribe the behaviour of the method

Attributes

  • config - Genba API credential attribute

Options



18
19
20
21
22
# File 'lib/genba/client.rb', line 18

def initialize(config = {})
  @app_id = config[:app_id]
  @username = config[:username]
  @api_key = config[:api_key]
end

Instance Method Details

#generate_tokenObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/genba/client.rb', line 24

def generate_token
  unless token_valid?
    body = { appId: @app_id, signature: genba_signature }
    response = RestClient.post(
      "#{API_URL}/token",
      body,
      headers: { accept: '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



70
71
72
# File 'lib/genba/client.rb', line 70

def keys
  Keys.new(self)
end

#pricesObject



62
63
64
# File 'lib/genba/client.rb', line 62

def prices
  Prices.new(self)
end

#productsObject



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

def products
  Products.new(self)
end

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



39
40
41
42
43
44
# File 'lib/genba/client.rb', line 39

def rest_get_with_token(path, query_params = {}, headers = {})
  genba_headers = token.merge(headers)
  headers[:params] = query_params unless query_params.empty?
  response = RestClient.get("#{API_URL}#{path}", genba_headers)
  from_rest_client_response(response)
end

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



52
53
54
55
56
# File 'lib/genba/client.rb', line 52

def rest_post_with_token(path, body = {}, headers = {})
  genba_headers = token.merge(headers)
  response = RestClient.post("#{API_URL}#{path}", encode_json(body), genba_headers)
  from_rest_client_response(response)
end

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



46
47
48
49
50
# File 'lib/genba/client.rb', line 46

def rest_put_with_token(path, body = {}, headers = {})
  genba_headers = token.merge(headers)
  response = RestClient.put("#{API_URL}#{path}", encode_json(body), genba_headers)
  from_rest_client_response(response)
end

#restrictionsObject



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

def restrictions
  Restrictions.new(self)
end