Class: Mpayer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mpayer/base.rb

Direct Known Subclasses

Account, Client, Payable, Sms, Transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_no: nil, mpayer_token: nil, base_url: 'https://app.mpayer.co.ke/api') ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
# File 'lib/mpayer/base.rb', line 5

def initialize(user_no: nil, mpayer_token: nil, base_url: 'https://app.mpayer.co.ke/api')
  @base_url ||= base_url 
  @user_no ||= user_no
  @mpayer_token ||= mpayer_token
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



3
4
5
# File 'lib/mpayer/base.rb', line 3

def base_url
  @base_url
end

#mpayer_tokenObject

Returns the value of attribute mpayer_token.



3
4
5
# File 'lib/mpayer/base.rb', line 3

def mpayer_token
  @mpayer_token
end

#user_noObject

Returns the value of attribute user_no.



3
4
5
# File 'lib/mpayer/base.rb', line 3

def user_no
  @user_no
end

Instance Method Details

#authObject



15
16
17
# File 'lib/mpayer/base.rb', line 15

def auth
  WSSE.header(user_no, mpayer_token).to_s
end

#delete(url) ⇒ Object



24
25
26
27
# File 'lib/mpayer/base.rb', line 24

def delete(url)
  response = Typhoeus.delete("#{base_url}#{url}", headers: headers)
  parse_api_response(response)
end

#get(url, per_page = 10) ⇒ Object



19
20
21
22
# File 'lib/mpayer/base.rb', line 19

def get(url, per_page=10)
  response = Typhoeus.get("#{base_url}#{url}?per_page=#{per_page}", headers: headers)
  parse_api_response(response)
end

#parse_api_response(response) ⇒ Object



11
12
13
# File 'lib/mpayer/base.rb', line 11

def parse_api_response(response)
  JSON.parse(response.body)
end

#post(url, body, batch = false) ⇒ Object



29
30
31
32
33
# File 'lib/mpayer/base.rb', line 29

def post(url, body, batch=false)
  headers = batch_headers if batch == true
  response = Typhoeus.post("#{base_url}#{url}", body: body.to_json, headers: headers)
  parse_api_response(response)
end

#put(url, body) ⇒ Object



35
36
37
38
# File 'lib/mpayer/base.rb', line 35

def put(url, body)
  response = Typhoeus.put("#{base_url}#{url}", body: body.to_json, headers: headers)
  parse_api_response(response)
end