Class: MxitMoneyApi
- Inherits:
-
Object
- Object
- MxitMoneyApi
- Defined in:
- lib/mxit_money_api/user.rb,
lib/mxit_money_api/initialize.rb,
lib/mxit_money_api/issue_money.rb
Constant Summary collapse
- BASE_URL =
'https://api.mxitmoney.co.za'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#balance ⇒ Object
readonly
Returns the value of attribute balance.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(api_key) ⇒ MxitMoneyApi
constructor
A new instance of MxitMoneyApi.
- #issue_money(params) ⇒ Object
- #user_info(params) ⇒ Object
Constructor Details
#initialize(api_key) ⇒ MxitMoneyApi
Returns a new instance of MxitMoneyApi.
6 7 8 9 |
# File 'lib/mxit_money_api/initialize.rb', line 6 def initialize(api_key) @api_key = api_key balance end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
4 5 6 |
# File 'lib/mxit_money_api/initialize.rb', line 4 def api_key @api_key end |
#balance ⇒ Object (readonly)
Returns the value of attribute balance.
4 5 6 |
# File 'lib/mxit_money_api/initialize.rb', line 4 def balance @balance end |
Class Method Details
.connect(*args) ⇒ Object
11 12 13 14 |
# File 'lib/mxit_money_api/initialize.rb', line 11 def self.connect(*args) connection = new(*args) connection.balance ? connection : nil end |
Instance Method Details
#issue_money(params) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mxit_money_api/issue_money.rb', line 3 def issue_money(params) params = Hash[params.map {|k, v| [k.to_s.camelize(:lower), v] }] url = URI.parse("#{BASE_URL}/paymentsplatform/rest/v3/push/issue/#{params.delete('phoneNumber')}?#{params.to_query}") req = Net::HTTP::Post.new(url.to_s, 'Accept'=>'application/json', 'Content-Type' =>'application/json') req.basic_auth(api_key, 'mxit_money_api'.to_s) response = Net::HTTP.start(url.host, url.port, :use_ssl => url.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https| https.request(req) end if response.code == '200' || response.code == '401' || response.code == '500' || response.code == '400' @balance = nil data = ActiveSupport::JSON.decode(response.body) HashWithIndifferentAccess.new(Hash[data.map {|k, v| [k.underscore, v] }]) end end |
#user_info(params) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/mxit_money_api/user.rb', line 3 def user_info(params) params.symbolize_keys! url = URI.parse("#{BASE_URL}/paymentsplatform/rest/v3/user/#{params[:id]}?idType=#{(params[:id_type] || 'mxit_id').to_s.camelize(:lower)}") req = Net::HTTP::Get.new(url.to_s, 'Accept'=>'application/json') req.basic_auth(api_key, 'mxit_money_api'.to_s) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE response = http.request(req) if response.code == '200' || response.code == '401' || response.code == '500' || response.code == '400' data = ActiveSupport::JSON.decode(response.body) HashWithIndifferentAccess.new(Hash[data.map {|k, v| [k.underscore, v] }]) end end |