Class: Airtel::Pesa::BalanceEnquiry

Inherits:
Object
  • Object
show all
Defined in:
lib/airtel/pesa/balance_enquiry.rb

Constant Summary collapse

STAGING_URL =
"https://openapiuat.airtel.africa".freeze
PRODUCTION_URL =
"https://openapi.airtel.africa".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction_country_code, transaction_currency_code) ⇒ BalanceEnquiry

Returns a new instance of BalanceEnquiry.



21
22
23
24
# File 'lib/airtel/pesa/balance_enquiry.rb', line 21

def initialize(transaction_country_code, transaction_currency_code)
  @transaction_country_code = transaction_country_code
  @transaction_currency_code = transaction_currency_code
end

Instance Attribute Details

#transaction_country_codeObject (readonly)

Returns the value of attribute transaction_country_code.



15
16
17
# File 'lib/airtel/pesa/balance_enquiry.rb', line 15

def transaction_country_code
  @transaction_country_code
end

#transaction_currency_codeObject (readonly)

Returns the value of attribute transaction_currency_code.



15
16
17
# File 'lib/airtel/pesa/balance_enquiry.rb', line 15

def transaction_currency_code
  @transaction_currency_code
end

Class Method Details

.call(transaction_country_code:, transaction_currency_code:) ⇒ Object



17
18
19
# File 'lib/airtel/pesa/balance_enquiry.rb', line 17

def self.call(transaction_country_code:, transaction_currency_code:)
  new(transaction_country_code, transaction_currency_code).call
end

Instance Method Details

#callObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/airtel/pesa/balance_enquiry.rb', line 26

def call
  url = URI("#{env_url}/standard/v1/users/balance")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{token}"
  request["X-Country"] = transaction_country_code
  request["X-Currency"] = transaction_currency_code

  response = http.request(request)
  parsed_response = JSON.parse(response.read_body)
  result = Airtel::Pesa.to_recursive_ostruct(parsed_response)
  OpenStruct.new(result: result, error: nil)
rescue JSON::ParserError => error
  OpenStruct.new(result: nil, error: error)
end