class Jet::Client

Jet API Client

Constants

API_URL

Public Class Methods

new(config = {}, raw_token = {}) click to toggle source
# File lib/jet/client.rb, line 9
def initialize(config = {}, raw_token = {})
  @api_user = config[:api_user]
  @secret = config[:secret]
  @merchant_id = config[:merchant_id]
  @id_token = raw_token[:id_token]
  @token_type = raw_token[:token_type]
  @expires_on = Time.parse(raw_token[:expires_on].to_s) if raw_token[:expires_on]
end

Public Instance Methods

decode_json(json) click to toggle source
# File lib/jet/client.rb, line 22
def decode_json(json)
  Oj.load(json)
end
encode_json(data) click to toggle source
# File lib/jet/client.rb, line 18
def encode_json(data)
  Oj.dump(data, mode: :compat)
end
files() click to toggle source
# File lib/jet/client.rb, line 100
def files
  Files.new(self)
end
generate_token() click to toggle source
# File lib/jet/client.rb, line 26
def generate_token
  unless token_valid?
    body = { user: @api_user, pass: @secret }
    response = RestClient.post("#{API_URL}/token", encode_json(body))
    parsed_response = decode_json(response.body)
    @id_token = parsed_response['id_token']
    @token_type = parsed_response['token_type']
    @expires_on = Time.parse(parsed_response['expires_on'])
  end
  raw_token
end
orders() click to toggle source
# File lib/jet/client.rb, line 84
def orders
  Orders.new(self)
end
products() click to toggle source
# File lib/jet/client.rb, line 92
def products
  Products.new(self)
end
raw_token() click to toggle source
# File lib/jet/client.rb, line 43
def raw_token
  if token_valid?
    {
      id_token: @id_token,
      token_type: @token_type,
      expires_on: @expires_on.iso8601
    }
  else
    {}
  end
end
refunds() click to toggle source
# File lib/jet/client.rb, line 104
def refunds
  Refunds.new(self)
end
rest_get_with_token(path, query_params = {}) click to toggle source
# File lib/jet/client.rb, line 59
def rest_get_with_token(path, query_params = {})
  headers = token
  headers[:params] = query_params unless query_params.empty?
  response = RestClient.get("#{API_URL}#{path}", headers)
  decode_json(response.body) if response.code == 200
end
rest_patch_with_token(path, body = {}) click to toggle source
# File lib/jet/client.rb, line 78
def rest_patch_with_token(path, body = {})
  headers = token
  response = RestClient.patch("#{API_URL}#{path}", encode_json(body), headers)
  decode_json(response.body) if response.code == 200
end
rest_post_with_token(path, body = {}) click to toggle source
# File lib/jet/client.rb, line 72
def rest_post_with_token(path, body = {})
  headers = token
  response = RestClient.post("#{API_URL}#{path}", encode_json(body), headers)
  decode_json(response.body) if response.code == 201
end
rest_put_with_token(path, body = {}) click to toggle source
# File lib/jet/client.rb, line 66
def rest_put_with_token(path, body = {})
  headers = token
  response = RestClient.put("#{API_URL}#{path}", encode_json(body), headers)
  decode_json(response.body) if response.code == 200
end
returns() click to toggle source
# File lib/jet/client.rb, line 88
def returns
  Returns.new(self)
end
taxonomy() click to toggle source
# File lib/jet/client.rb, line 96
def taxonomy
  Taxonomy.new(self)
end
token() click to toggle source
# File lib/jet/client.rb, line 38
def token
  generate_token unless token_valid?
  { Authorization: "#{@token_type} #{@id_token}" }
end
token_valid?() click to toggle source
# File lib/jet/client.rb, line 55
def token_valid?
  @id_token && @token_type && @expires_on > Time.now
end