class BMLConnect::Client

Constants

BML_API_VERSION
BML_APP_VERSION
BML_PRODUCTION_ENDPOINT
BML_SANDBOX_ENDPOINT
BML_SIGN_METHOD

Attributes

api_key[R]
http_client[R]
transactions[R]

Public Class Methods

new(api_key: nil, app_id: nil, mode: nil, options: {}) click to toggle source
# File lib/bml_connect/client.rb, line 17
def initialize(api_key: nil, app_id: nil, mode: nil, options: {})
  @api_key = api_key || (defined?(BML_API_KEY) ? BML_API_KEY : 'not-set')
  @app_id = app_id || (defined?(BML_APP_ID) ? BML_APP_ID : 'not-set')
  @mode = mode || (defined?(BML_MODE) ? BML_MODE : 'production')
  @http_client = initialize_http_client(options)
  @transactions = Transactions.new(self)
end

Public Instance Methods

base_url() click to toggle source
# File lib/bml_connect/client.rb, line 25
def base_url
  @mode == 'production' ? BML_PRODUCTION_ENDPOINT : BML_SANDBOX_ENDPOINT
end
get(action, params = {}) click to toggle source
# File lib/bml_connect/client.rb, line 42
def get(action, params = {})
  @http_client.get(action, params.slice(:page))
end
post(action, params = {}) click to toggle source
# File lib/bml_connect/client.rb, line 33
def post(action, params = {})
  data = params.merge({
    'apiVersion' => BML_API_VERSION,
    'appVersion' => BML_APP_VERSION,
    'signMethod' => BML_SIGN_METHOD,
  })
  @http_client.post(action, data.to_json)
end
set_http_client(client) click to toggle source
# File lib/bml_connect/client.rb, line 29
def set_http_client(client)
  @http_client = client
end

Private Instance Methods

initialize_http_client(options) click to toggle source
# File lib/bml_connect/client.rb, line 47
def initialize_http_client(options)
  defaults = {
    url: base_url,
    headers: {
      'Content-Type' => 'application/json',
      'Accept' => 'application/json',
      'Authorization' =>  api_key,
    }
  };
  Faraday.new(defaults.deeper_merge!(options)) do |f|
    f.response :logger, nil, { headers: true, bodies: true } if defined?(DEBUG_ON)
    f.response :json, :parser_options => { :symbolize_names => true }
  end
end