Sha256: 512f2a84e780fc247dd66ae568bef0cc761c7457ef9ed7337ea6a1fe4c0c87f4

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require 'faraday'
require 'faraday_middleware'
require 'deep_merge/rails_compat'

module BMLConnect
  class Client
    BML_API_VERSION = '2.0'
    BML_APP_VERSION = 'bml-connect-ruby'
    BML_SIGN_METHOD = 'sha1'
    BML_SANDBOX_ENDPOINT = "https://api.uat.merchants.bankofmaldives.com.mv/public/"
    BML_PRODUCTION_ENDPOINT = "https://api.merchants.bankofmaldives.com.mv/public/"

    attr_reader(:api_key, :http_client, :transactions)

    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

    def base_url
      @mode == 'production' ? BML_PRODUCTION_ENDPOINT : BML_SANDBOX_ENDPOINT
    end

    def set_http_client(client)
      @http_client = client
    end

    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

    def get(action, params = {})
      @http_client.get(action, params.slice(:page))
    end

    private
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bml_connect-0.2.0 lib/bml_connect/client.rb
bml_connect-0.1.0 lib/bml_connect/client.rb