bvr 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module Bvr
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE ChangePassword>
3
+ <ChangePassword>
4
+ <Customer>foo*provider</Customer>
5
+ <Result>Success</Result>
6
+ <Reason>User/Password combination unknown</Reason>
7
+ </ChangePassword>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE ChangeUserInfo>
3
+ <ChangeUserInfo>
4
+ <Customer>foo*provider</Customer>
5
+ <Result>Success</Result>
6
+ <Reason>GeoCallCLI Changed</Reason>
7
+ <Result>Success</Result>
8
+ <Reason>Request completely succeeded</Reason>
9
+ </ChangeUserInfo>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE CreateAccount>
3
+ <CreateAccount>
4
+ <Customer>foo</Customer>
5
+ <Result>Failed</Result>
6
+ <Reason>Request completely or partially failed.</Reason>
7
+ </CreateAccount>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE ChangeUserInfo>
3
+ <ChangeUserInfo>
4
+ <Customer>foo*provider</Customer>
5
+ <Result>Success</Result>
6
+ <Reason>CustomerBlocked Changed</Reason>
7
+ <Result>Success</Result>
8
+ <Reason>Request completely succeeded</Reason>
9
+ </ChangeUserInfo>
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE GetUserInfo>
3
+ <GetUserInfo>
4
+ <Customer>foo*provider</Customer>
5
+ <Balance>1.86</Balance>
6
+ <SpecificBalance>1.86828</SpecificBalance>
7
+ <Blocked>False</Blocked>
8
+ <EmailAddress>JohnDoe@gmail.com</EmailAddress>
9
+ <GeocallCLI>+4412345678</GeocallCLI>
10
+ <GeocallCLI>+4412345679</GeocallCLI>
11
+ </GetUserInfo>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE Transaction>
3
+ <Transaction>
4
+ <Customer>foo</Customer>
5
+ <Amount>1</Amount>
6
+ <DateTime>2013-12-29 01:22:28 (UTC)</DateTime>
7
+ <Result>Success</Result>
8
+ </Transaction>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE Transaction>
3
+ <Transaction>
4
+ <Result>Failed</Result>
5
+ <Reason>Invalid format: Amount </Reason>
6
+ </Transaction>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0"?>
2
+ <!DOCTYPE ValidateUser>
3
+ <ValidateUser>
4
+ <Customer>foo*provider</Customer>
5
+ <Result>Success</Result>
6
+ </ValidateUser>
@@ -0,0 +1,10 @@
1
+ class Module
2
+ include Minitest::Spec::DSL
3
+ end
4
+ module FaradayStub
5
+ let(:faraday_adapter) { Faraday::Adapter::Test::Stubs.new }
6
+
7
+ before do
8
+ Bvr.connection = Bvr::Connection.new.tap{ |con| con.faraday_connection = Faraday.new{|b| b.adapter :test, faraday_adapter} }
9
+ end
10
+ end
@@ -0,0 +1,120 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative "../../helpers/faraday_stub"
3
+
4
+ describe Bvr::CallCollection do
5
+
6
+ describe '.find(customer_id, options={})' do
7
+ let(:customer_id) { 'john' }
8
+ let(:params) { { command: Bvr::CallCollection::API_COMMANDS[:find_by_customer_id], customer: customer_id } }
9
+ let(:connection) { Minitest::Mock.new }
10
+
11
+ before { Bvr.connection = connection }
12
+
13
+ subject{ Bvr::CallCollection.find_by_customer_id(customer_id) }
14
+
15
+ it 'sets the right command and the customer_id' do
16
+ connection.expect :get, {}, [params] #.merge({username: Bvr.config.username, password: Bvr.config.password})]
17
+ subject
18
+ connection.verify
19
+ end
20
+
21
+ describe 'when options are provided' do
22
+ let(:options) { { recordcount: 'bar', callid: 'foo' }.merge(params) }
23
+
24
+ subject{ Bvr::CallCollection.find_by_customer_id(customer_id, options) }
25
+
26
+ it 'merges the CallOverview command and the customer_id with options' do
27
+ connection.expect :get, {}, [options]
28
+ subject
29
+ connection.verify
30
+ end
31
+ end
32
+
33
+ describe 'when wrong options are provided' do
34
+ let(:options) { { foo: 'bar', john: 'Doe' }.merge(params) }
35
+
36
+ subject{ Bvr::CallCollection.find_by_customer_id(customer_id, options) }
37
+
38
+ it 'raise an ArgumentError' do
39
+ proc {subject}.must_raise ArgumentError
40
+ end
41
+ end
42
+
43
+ describe 'when there is a response' do
44
+ include FaradayStub
45
+
46
+ let(:response) do
47
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/calloverview.xml'))
48
+ end
49
+
50
+ subject{ Bvr::CallCollection.find_by_customer_id(customer_id) }
51
+
52
+ before do
53
+ faraday_adapter.get(Bvr::Connection.uri_from_h(params)) { [200, {}, response] }
54
+ end
55
+
56
+ it 'created a new Bvr::CallCollection from the response' do
57
+ subject.must_be_instance_of Bvr::CallCollection
58
+ end
59
+
60
+ it 'sets the total calls' do
61
+ subject.count.must_equal 10
62
+ end
63
+
64
+ it 'can tell if there is more data' do
65
+ subject.next?.must_equal false
66
+ end
67
+
68
+ it 'sets a collection of Call' do
69
+ subject.collection.must_be_instance_of Array
70
+ subject.collection.size.must_equal 5
71
+ end
72
+ end
73
+
74
+ describe 'when there is not response' do
75
+ let(:connection) { Minitest::Mock.new }
76
+ let(:customer_id) { 'foo' }
77
+ let(:params) { { command: Bvr::CallCollection::API_COMMANDS[:find_by_customer_id], customer: customer_id } }
78
+
79
+ subject { Bvr::CallCollection.find_by_customer_id(customer_id) }
80
+
81
+ before do
82
+ Bvr.connection = connection
83
+ end
84
+
85
+ it 'returns empty array' do
86
+ connection.expect(:get, {}, [params])
87
+ subject.must_equal []
88
+ connection.verify
89
+ end
90
+ end
91
+ end
92
+
93
+ describe '#count' do
94
+ let(:raw_count) { '20' }
95
+ let(:call_collection) {Bvr::CallCollection.new}
96
+
97
+ subject { call_collection.count }
98
+
99
+ it 'returns the number of counts' do
100
+ call_collection.stub(:raw_count, raw_count) do
101
+ subject.must_equal Integer(raw_count)
102
+ end
103
+ end
104
+ end
105
+
106
+ describe '#next?' do
107
+ let(:raw_more_data) { 'True' }
108
+ let(:call_collection) {Bvr::CallCollection.new}
109
+
110
+ subject { call_collection.next? }
111
+
112
+ it 'says if there is more data' do
113
+ call_collection.stub(:raw_more_data, raw_more_data) do
114
+ subject.must_equal true
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+
@@ -0,0 +1,60 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Bvr::Call do
4
+ let(:h) do
5
+ customer_h = ::XmlSimple.xml_in File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/calloverview.xml'))
6
+ customer_h['Calls'][0]['Call'][0]
7
+ end
8
+
9
+ describe '.new_from_response(h)' do
10
+ subject { Bvr::Call.new_from_response(h) }
11
+
12
+ it 'Creates a new Call with the right params' do
13
+ start_time = "2013-12-24 18:05:26 (UTC)"
14
+ subject.must_be_instance_of Bvr::Call
15
+ subject.id.must_equal "1234567890"
16
+ subject.calltype.must_equal "PSTNOutSip"
17
+ subject.raw_start_time.must_equal start_time
18
+ subject.raw_dest.must_equal "+32123456788"
19
+ subject.raw_duration.must_equal "00:02:21"
20
+ subject.charge.must_equal "0.0705"
21
+ end
22
+ end
23
+
24
+ describe '#duration' do
25
+ let(:raw_duration) { '00:02:21' }
26
+ let(:raw_start_time) { "2013-12-24 18:05:26 (UTC)" }
27
+ let(:call) { Bvr::Call.new.tap{ |call| call.raw_duration = raw_duration; call.raw_start_time = raw_start_time } }
28
+
29
+ subject { call.duration }
30
+
31
+ it 'returns the relative time duration' do
32
+ subject.must_be_instance_of Time
33
+ subject.must_equal Time.parse(raw_duration, call.start_time)
34
+ end
35
+ end
36
+
37
+ describe '#start_time' do
38
+ let(:raw_start_time) { "2013-12-24 18:05:26 (UTC)" }
39
+ let(:call) { Bvr::Call.new.tap{ |call| call.raw_start_time = raw_start_time } }
40
+
41
+ subject { call.start_time }
42
+
43
+ it 'return the time' do
44
+ subject.must_be_instance_of Time
45
+ subject.must_equal Time.parse(raw_start_time)
46
+ end
47
+ end
48
+
49
+ describe '#dest' do
50
+ let(:raw_dest) { "+32123456788" }
51
+ let(:call) { Bvr::Call.new.tap{ |call| call.raw_dest = raw_dest } }
52
+
53
+ subject { call.dest }
54
+
55
+ it 'returns a phone' do
56
+ subject.must_be_instance_of Bvr::Phone
57
+ subject.number.must_equal raw_dest
58
+ end
59
+ end
60
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative '../../spec_helper'
2
+ require_relative "../../helpers/faraday_stub"
2
3
 
3
4
  describe Bvr::Connection do
4
5
  describe '.new(faraday_adapter)' do
@@ -32,36 +33,22 @@ describe Bvr::Connection do
32
33
  end
33
34
  end
34
35
 
35
- before do
36
- Bvr.configure do |config|
37
- config.username = username
38
- config.password = password
39
- end
40
- end
41
-
42
36
  subject { connection.get(params) }
43
37
 
44
38
  it 'calls connection.get with the right uri' do
45
39
  response = Minitest::Mock.new
46
- faraday_connection.expect :get, response, [connection.uri(params)]
47
- response.expect :body, nil
40
+ faraday_connection.expect :get, response, [Bvr::Connection.uri_from_h(params)]
41
+ response.expect :body, ::XmlSimple.xml_out({})
48
42
  subject
49
43
  faraday_connection.verify
50
44
  response.verify
51
45
  end
52
-
53
46
  end
54
47
 
55
- describe 'uri(queryH)' do
48
+ describe 'self.uri_from_h(queryH)' do
56
49
  let(:queryH) { { foo: 'bar', bar: 'foo'} }
57
50
  let(:username) { 'username' }
58
- let(:faraday_connection) { Minitest::Mock.new }
59
51
  let(:password) { 'password' }
60
- let(:connection) do
61
- Bvr::Connection.new.tap do |connection|
62
- connection.faraday_connection = faraday_connection
63
- end
64
- end
65
52
 
66
53
  before do
67
54
  Bvr.configure do |config|
@@ -70,7 +57,7 @@ describe Bvr::Connection do
70
57
  end
71
58
  end
72
59
 
73
- subject { connection.uri(queryH) }
60
+ subject { Bvr::Connection.uri_from_h(queryH) }
74
61
 
75
62
  it 'match the api path' do
76
63
  subject.must_match Regexp.new(Bvr::Connection::API_PATH)
@@ -0,0 +1,166 @@
1
+ require_relative '../../spec_helper'
2
+ require_relative "../../helpers/faraday_stub"
3
+
4
+ describe Bvr::Credit do
5
+ describe '.add(customer_id, amount)' do
6
+ include FaradayStub
7
+ let(:customer_id) { 'foo' }
8
+ let(:amount) { 10 }
9
+ let(:options) do
10
+ {
11
+ command: Bvr::Credit::API_COMMANDS[:add],
12
+ customer: customer_id,
13
+ amount: amount
14
+ }
15
+ end
16
+ let(:response) do
17
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
18
+ end
19
+
20
+ subject { Bvr::Credit.add(customer_id, amount) }
21
+
22
+ before do
23
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
24
+ end
25
+
26
+ it 'returns a result' do
27
+ subject['Result'].must_be_instance_of String
28
+ end
29
+ end
30
+
31
+
32
+ describe '#add(amount)' do
33
+ include FaradayStub
34
+ let(:raw_specific_balance) { 10.12345 }
35
+ let(:raw_balance) { 10.12 }
36
+ let(:amount) { 1 }
37
+ let(:customer) { Bvr::Customer.new(customer_id) }
38
+ let(:credit) { Bvr::Credit.new(raw_specific_balance, raw_balance, customer) }
39
+ let(:customer_id) { 'foo' }
40
+ let(:options) do
41
+ {
42
+ command: Bvr::Credit::API_COMMANDS[:add],
43
+ customer: customer_id,
44
+ amount: amount
45
+ }
46
+ end
47
+ let(:response) do
48
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
49
+ end
50
+
51
+ subject { credit.add(amount) }
52
+
53
+ before do
54
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
55
+ end
56
+
57
+ it 'increase the amout' do
58
+ customer.stub(:id, customer_id) do
59
+ credit.specific_balance.must_equal raw_specific_balance
60
+ credit.balance.must_equal raw_balance
61
+
62
+ subject.must_equal true
63
+
64
+ credit.specific_balance.must_equal raw_specific_balance + amount
65
+ credit.balance.must_equal raw_balance + amount
66
+ end
67
+ end
68
+
69
+ describe 'when the amount if more than .5f' do
70
+ let(:amount) { 1.000001 }
71
+
72
+ it 'round up' do
73
+ customer.id = customer_id
74
+
75
+ credit.specific_balance.must_equal raw_specific_balance
76
+ credit.balance.must_equal raw_balance
77
+
78
+ subject.must_equal true
79
+
80
+ credit.specific_balance.must_equal raw_specific_balance + Float("%.5f" % amount)
81
+ credit.balance.must_equal raw_balance + Float("%.2f" % amount)
82
+ end
83
+ end
84
+
85
+ describe 'when there is an error' do
86
+ let(:response) do
87
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction_failed.xml'))
88
+ end
89
+
90
+ it 'does not change the amout and return false' do
91
+ customer.stub(:id, customer_id) do
92
+ credit.raw_specific_balance.must_equal raw_specific_balance
93
+ credit.raw_balance.must_equal raw_balance
94
+
95
+ subject.must_equal false
96
+
97
+ credit.raw_specific_balance.must_equal raw_specific_balance
98
+ credit.raw_balance.must_equal raw_balance
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ describe '#rm(amount)' do
105
+ include FaradayStub
106
+ let(:raw_specific_balance) { 10.12345 }
107
+ let(:raw_balance) { 10.12 }
108
+ let(:amount) { 1 }
109
+ let(:customer) { Bvr::Customer.new(customer_id) }
110
+ let(:credit) { Bvr::Credit.new(raw_specific_balance, raw_balance, customer) }
111
+ let(:customer_id) { 'foo' }
112
+ let(:options) do
113
+ {
114
+ command: Bvr::Credit::API_COMMANDS[:add],
115
+ customer: customer_id,
116
+ amount: - amount
117
+ }
118
+ end
119
+ let(:response) do
120
+ File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
121
+ end
122
+
123
+ subject { credit.rm(amount) }
124
+
125
+ before do
126
+ faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
127
+ end
128
+
129
+ it 'decrease the amout' do
130
+ customer.stub(:id, customer_id) do
131
+ credit.specific_balance.must_equal raw_specific_balance
132
+ credit.balance.must_equal raw_balance
133
+
134
+ subject.must_equal true
135
+
136
+ credit.specific_balance.must_equal raw_specific_balance - amount
137
+ credit.balance.must_equal raw_balance - amount
138
+ end
139
+ end
140
+
141
+ end
142
+
143
+ describe '#balance' do
144
+ let(:raw_balance) { "10.12" }
145
+ let(:credit) { Bvr::Credit.new(nil, raw_balance) }
146
+
147
+ subject { credit.balance }
148
+
149
+ it 'return a float out of the balance' do
150
+ subject.must_be_instance_of Float
151
+ subject.must_equal Float(raw_balance)
152
+ end
153
+ end
154
+
155
+ describe '#specific_balance' do
156
+ let(:raw_specific_balance) { "10.12345" }
157
+ let(:credit) { Bvr::Credit.new(raw_specific_balance) }
158
+
159
+ subject { credit.specific_balance }
160
+
161
+ it 'return a float out of the balance' do
162
+ subject.must_be_instance_of Float
163
+ subject.must_equal Float(raw_specific_balance)
164
+ end
165
+ end
166
+ end