Class: Momoapi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/momoapi-ruby/client.rb

Direct Known Subclasses

Collection, Disbursement, Remittance

Instance Method Summary collapse

Instance Method Details

#get_auth_token(path, subscription_key) ⇒ Object

Create an access token which can then be used to authorize and authenticate towards the other end-points of the API



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/momoapi-ruby/client.rb', line 47

def get_auth_token(path, subscription_key)
  headers = {
    "Ocp-Apim-Subscription-Key": subscription_key
  }
  username = Momoapi.config.collection_user_id
  password = Momoapi.config.collection_api_secret
  url = Momoapi.config.base_url
  conn = Faraday.new(url: url)
  conn.headers = headers
  conn.basic_auth(username, password)
  response = conn.post(path)
  begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    response.body.to_s
  end
end

#get_balance(path, subscription_key) ⇒ Object

get the balance on an account



75
76
77
# File 'lib/momoapi-ruby/client.rb', line 75

def get_balance(path, subscription_key)
  prepare_get_request(path, subscription_key)
end

#get_transaction_status(path, subscription_key) ⇒ Object

retrieve transaction information, for transfer and payments



80
81
82
# File 'lib/momoapi-ruby/client.rb', line 80

def get_transaction_status(path, subscription_key)
  prepare_get_request(path, subscription_key)
end

#handle_error(response_body, response_code) ⇒ Object

Raises:



41
42
43
# File 'lib/momoapi-ruby/client.rb', line 41

def handle_error(response_body, response_code)
  raise Error::APIError.new(response_body, response_code)
end

#interpret_response(resp) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/momoapi-ruby/client.rb', line 29

def interpret_response(resp)
  body = resp.body.empty? ? '' : JSON.parse(resp.body)
  response = {
    body: body,
    code: resp.status
  }
  unless resp.status >= 200 && resp.status < 300
    handle_error(response[:body], response[:code])
  end
  response
end

#is_user_active(path, subscription_key) ⇒ Object

check if an account holder is registered and active in the system



85
86
87
# File 'lib/momoapi-ruby/client.rb', line 85

def is_user_active(path, subscription_key)
  prepare_get_request(path, subscription_key)
end

#prepare_get_request(path, subscription_key) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/momoapi-ruby/client.rb', line 65

def prepare_get_request(path, subscription_key)
  headers = {
    "X-Target-Environment": Momoapi.config.environment || 'sandbox',
    "Content-Type": 'application/json',
    "Ocp-Apim-Subscription-Key": subscription_key
  }
  send_request('get', path, headers)
end

#send_request(method, path, headers, *_body) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/momoapi-ruby/client.rb', line 14

def send_request(method, path, headers, *_body)
  auth_token = get_auth_token['access_token']
  conn = Faraday.new(url: Momoapi.config.base_url)
  conn.headers = headers
  conn.authorization(:Bearer, auth_token)

  case method
  when 'get'
    response = conn.get(path)
  when 'post'
    response = conn.post(path)
  end
  interpret_response(response)
end