mxit_api 0.1.0.pre → 0.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,17 +25,48 @@ Examples
25
25
  ========
26
26
 
27
27
  Prerequisites (usually found on the mxit dashboard http://code.mxit.com/MobiDashboard/Default.aspx):
28
- * MXIT_CLIENT_ID
29
- * MXIT_CLIENT_SECRET
28
+ * ruby 1.9.2
29
+ * mxit client id
30
+ * mxit secret id
30
31
 
31
32
 
32
- Authorization API
33
- ----------------------------------------
33
+ Messaging
34
+ ----------------
35
+ http://dev.mxit.com/docs/api/messaging/post-message-send
34
36
 
37
+ ```ruby
38
+ connection = MxitApi.new(client_id, client_secret, {grant_type: 'client_credentials', scope: 'message/send'})
39
+ connection.send_message(to: "m123,m345", from: "yourappname", body: "This is a mxit message")
40
+ ```
35
41
 
36
- Messaging API
42
+ User Profile
37
43
  ----------------
44
+ http://dev.mxit.com/docs/authentication#OAuth2_Flows
45
+ http://dev.mxit.com/docs/api/user/get-user-profile
38
46
 
47
+ ```ruby
48
+ connection = MxitApi.new(client_id, client_secret, {:grant_type => 'authorization_code',
49
+ :code => "code from authentication",
50
+ :redirect_uri => "http://you.host/redirect/url")
51
+ result = connection.profile
52
+ result[:display_name]
53
+ result[:gender]
54
+ # etc
55
+
56
+ ```
39
57
 
40
- User API
41
- ----------------
58
+ MxitMoney
59
+ ----------------
60
+
61
+ ```ruby
62
+ connection = MxitMoneyApi.connect('mxit_money_client_id')
63
+ if connection
64
+ result = connection.user_info("m40000000000")
65
+ if result[:is_registered]
66
+ connection.issue_money(:phone_number => result[:msisdn],
67
+ :merchant_reference => "#{result[:login_name]}#{rand(8999) + 1000}",
68
+ :amount_in_cents => "150")
69
+ end
70
+ end
71
+ # etc
72
+ ```
@@ -1,7 +1,7 @@
1
1
  class MxitApi
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 1
4
+ MINOR = 2
5
5
  PATCH = 0
6
6
  BUILD = 'pre'
7
7
 
data/lib/mxit_api.rb CHANGED
@@ -5,4 +5,9 @@ require 'mxit_api/initialize'
5
5
  require 'mxit_api/profile'
6
6
  require 'mxit_api/send_invite'
7
7
  require 'mxit_api/send_message'
8
- require 'mxit_api/version'
8
+ require 'mxit_api/version'
9
+ require 'mxit_money_api/initialize'
10
+ require 'mxit_money_api/user'
11
+ require 'mxit_money_api/issue_money'
12
+
13
+
@@ -0,0 +1,30 @@
1
+ class MxitMoneyApi
2
+
3
+ attr_reader :api_key, :balance
4
+
5
+ def initialize(api_key)
6
+ @api_key = api_key
7
+ balance
8
+ end
9
+
10
+ def self.connect(*args)
11
+ connection = new(*args)
12
+ connection.balance ? connection : nil
13
+ end
14
+
15
+ def balance
16
+ return @balance unless @balance.nil?
17
+ url = URI.parse('https://m2api.fireid.com/paymentsplatform/rest/v3/push/')
18
+ req = Net::HTTP::Get.new(url.path, 'Accept'=>'application/json')
19
+ req.basic_auth(api_key,"mxit_money_api")
20
+ http = Net::HTTP.new(url.host, url.port)
21
+ http.use_ssl = true
22
+ response = http.request(req)
23
+ if response.code == '200'
24
+ data = ActiveSupport::JSON.decode(response.body)
25
+ @balance = data['balance']
26
+ end
27
+ @balance
28
+ end
29
+
30
+ end
@@ -0,0 +1,20 @@
1
+ class MxitMoneyApi
2
+
3
+ def issue_money(params)
4
+ params = Hash[params.map {|k, v| [k.to_s.camelize(:lower), v] }]
5
+ url = URI.parse("https://m2api.fireid.com/paymentsplatform/rest/v3/push/issue/#{params.delete('phoneNumber')}?#{params.to_query}")
6
+ req = Net::HTTP::Post.new(url.to_s,
7
+ 'Accept'=>'application/json',
8
+ 'Content-Type' =>'application/json')
9
+ req.basic_auth(api_key,"mxit_money_api".to_s)
10
+ http = Net::HTTP.new(url.host, url.port)
11
+ http.use_ssl = true
12
+ response = http.request(req)
13
+ if response.code == '200' || response.code == '401' || response.code == '500' || response.code == '400'
14
+ @balance = nil
15
+ data = ActiveSupport::JSON.decode(response.body)
16
+ HashWithIndifferentAccess.new(Hash[data.map {|k, v| [k.underscore, v] }])
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,17 @@
1
+ class MxitMoneyApi
2
+
3
+ def user_info(params)
4
+ params.symbolize_keys!
5
+ url = URI.parse("https://m2api.fireid.com/paymentsplatform/rest/v3/user/#{params[:id]}?idType=#{(params[:id_type] || 'mxit_id').to_s.camelize(:lower)}")
6
+ req = Net::HTTP::Get.new(url.to_s, 'Accept'=>'application/json')
7
+ req.basic_auth(api_key,"mxit_money_api".to_s)
8
+ http = Net::HTTP.new(url.host, url.port)
9
+ http.use_ssl = true
10
+ response = http.request(req)
11
+ if response.code == '200' || response.code == '401' || response.code == '500' || response.code == '400'
12
+ data = ActiveSupport::JSON.decode(response.body)
13
+ HashWithIndifferentAccess.new(Hash[data.map {|k, v| [k.underscore, v] }])
14
+ end
15
+ end
16
+
17
+ end
data/mxit_api.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mxit_api"
8
- s.version = "0.1.0.pre"
8
+ s.version = "0.2.0.pre"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Grant Speelman"]
12
- s.date = "2012-11-08"
12
+ s.date = "2013-01-09"
13
13
  s.description = "gem to use the Mxit APIs at http://dev.mxit.com/docs/ "
14
14
  s.email = "grant.speelman@unboxedconsulting.com"
15
15
  s.extra_rdoc_files = [
@@ -31,8 +31,12 @@ Gem::Specification.new do |s|
31
31
  "lib/mxit_api/send_invite.rb",
32
32
  "lib/mxit_api/send_message.rb",
33
33
  "lib/mxit_api/version.rb",
34
+ "lib/mxit_money_api/initialize.rb",
35
+ "lib/mxit_money_api/issue_money.rb",
36
+ "lib/mxit_money_api/user.rb",
34
37
  "mxit_api.gemspec",
35
38
  "spec/mxit_api_spec.rb",
39
+ "spec/mxit_money_api_spec.rb",
36
40
  "spec/spec_helper.rb"
37
41
  ]
38
42
  s.homepage = "http://github.com/unboxed/mxit_api"
@@ -0,0 +1,122 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe MxitMoneyApi do
4
+
5
+ before :each do
6
+ token_body = %&{
7
+ "balance": 5000
8
+ }&
9
+ stub_request(:get, "https://3:mxit_money_api@m2api.fireid.com/paymentsplatform/rest/v3/push/").to_return(:status => 200, :body => token_body, :headers => {})
10
+ end
11
+
12
+ context "new" do
13
+
14
+ it "must post to mxit api requesting token" do
15
+ MxitMoneyApi.new("3")
16
+ assert_requested(:get, "https://3:mxit_money_api@m2api.fireid.com/paymentsplatform/rest/v3/push/",
17
+ :headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby'})
18
+ end
19
+
20
+ it "must set the balance" do
21
+ connection = MxitMoneyApi.new("3")
22
+ connection.balance.should == 5000
23
+ end
24
+
25
+ end
26
+
27
+ context "connect" do
28
+
29
+ it "must return connection if has access_token" do
30
+ connection = mock('MxitMoneyApi', balance: "123")
31
+ MxitMoneyApi.should_receive(:new).with("3").and_return(connection)
32
+ MxitMoneyApi.connect("3").should == connection
33
+ end
34
+
35
+ it "wont return connection if no balance" do
36
+ connection = mock('MxitApi', balance: nil)
37
+ MxitMoneyApi.should_receive(:new).with("3").and_return(connection)
38
+ MxitMoneyApi.connect("3").should be_nil
39
+ end
40
+
41
+ end
42
+
43
+ context "user_info" do
44
+
45
+ before :each do
46
+ @body = %&{ "isRegistered": true,
47
+ "loginName": "gbox21",
48
+ "mxitId": "m40000000000",
49
+ "msisdn": "27712345678"
50
+ }&
51
+ @connection = MxitMoneyApi.new("3")
52
+ end
53
+
54
+ it "must return a hash for search by mxitId" do
55
+ stub_request(:get, "https://3:mxit_money_api@m2api.fireid.com/paymentsplatform/rest/v3/user/m40000000000?idType=mxitId").
56
+ with(:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby'}).
57
+ to_return(:status => 200, :body => @body, :headers => {})
58
+ profile = @connection.user_info(:id => 'm40000000000')
59
+ profile.should be_kind_of(Hash)
60
+ profile[:is_registered].should == true
61
+ profile[:login_name].should == "gbox21"
62
+ profile[:mxit_id].should == "m40000000000"
63
+ profile[:msisdn].should == "27712345678"
64
+ end
65
+
66
+ it "must return a hash for search by phoneNumber" do
67
+ stub_request(:get, "https://3:mxit_money_api@m2api.fireid.com/paymentsplatform/rest/v3/user/0712345678?idType=phoneNumber").
68
+ with(:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby'}).
69
+ to_return(:status => 200, :body => @body, :headers => {})
70
+ profile = @connection.user_info(:id => '0712345678', :id_type => :phone_number)
71
+ profile.should be_kind_of(Hash)
72
+ profile[:is_registered].should == true
73
+ profile[:login_name].should == "gbox21"
74
+ profile[:mxit_id].should == "m40000000000"
75
+ profile[:msisdn].should == "27712345678"
76
+ end
77
+
78
+ it "must handle a failed response" do
79
+ body = %&{
80
+ "errorType": "INVALID_MSISDN",
81
+ "message": "The phone number is incorrect."
82
+ }&
83
+ stub_request(:get, "https://3:mxit_money_api@m2api.fireid.com/paymentsplatform/rest/v3/user/m40000000001?idType=mxitId").
84
+ with(:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby'}).
85
+ to_return(:status => 500, :body => body, :headers => {})
86
+ profile = @connection.user_info(:id => 'm40000000001')
87
+ profile.should_not be_empty
88
+ end
89
+
90
+ end
91
+
92
+ context "issue_money" do
93
+
94
+ before :each do
95
+ @body = %&{
96
+ "m2_reference": "886e797d-ff9b-4743-a6a6-908bf588b2f8"
97
+ }&
98
+ stub_request(:post, "https://3:mxit_money_api@m2api.fireid.com/paymentsplatform/rest/v3/push/issue/0761234567?amountInCents=123&merchantReference=000000001").
99
+ to_return(:status => 200, :body => @body, :headers => {})
100
+ @connection = MxitMoneyApi.new("3")
101
+ end
102
+
103
+ it "must make the correct api request" do
104
+ @connection.issue_money(:phone_number => "0761234567",
105
+ :merchant_reference => "000000001",
106
+ :amount_in_cents => "123")
107
+ assert_requested(:post, "https://3:mxit_money_api@m2api.fireid.com/paymentsplatform/rest/v3/push/issue/0761234567?amountInCents=123&merchantReference=000000001",
108
+ :headers => {'Accept'=>'application/json',
109
+ 'User-Agent'=>'Ruby',
110
+ 'Content-Type' =>'application/json'})
111
+ end
112
+
113
+ it "must handle a response" do
114
+ profile = @connection.issue_money(:phone_number => "0761234567",
115
+ :merchant_reference => "000000001",
116
+ :amount_in_cents => "123")
117
+ profile.should_not be_empty
118
+ end
119
+
120
+ end
121
+
122
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mxit_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre
4
+ version: 0.2.0.pre
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-08 00:00:00.000000000 Z
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -209,8 +209,12 @@ files:
209
209
  - lib/mxit_api/send_invite.rb
210
210
  - lib/mxit_api/send_message.rb
211
211
  - lib/mxit_api/version.rb
212
+ - lib/mxit_money_api/initialize.rb
213
+ - lib/mxit_money_api/issue_money.rb
214
+ - lib/mxit_money_api/user.rb
212
215
  - mxit_api.gemspec
213
216
  - spec/mxit_api_spec.rb
217
+ - spec/mxit_money_api_spec.rb
214
218
  - spec/spec_helper.rb
215
219
  homepage: http://github.com/unboxed/mxit_api
216
220
  licenses:
@@ -227,7 +231,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
227
231
  version: '0'
228
232
  segments:
229
233
  - 0
230
- hash: 3437017726523863957
234
+ hash: 3691744955216269910
231
235
  required_rubygems_version: !ruby/object:Gem::Requirement
232
236
  none: false
233
237
  requirements: