nexmos 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,4 +4,4 @@ module Nexmos
4
4
  ::Nexmos.logger = Rails.logger
5
5
  end
6
6
  end
7
- end
7
+ end
@@ -1,5 +1,5 @@
1
1
  module Nexmos
2
2
  class Search < Base
3
- self.define_api_calls(:search)
3
+ define_api_calls(:search)
4
4
  end
5
- end
5
+ end
@@ -1,5 +1,5 @@
1
1
  module Nexmos
2
2
  class TextToSpeech < Base
3
- self.define_api_calls(:text_to_speech)
3
+ define_api_calls(:text_to_speech)
4
4
  end
5
- end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Nexmos
2
- VERSION = "0.2"
2
+ VERSION = '0.3'
3
3
  end
@@ -15,10 +15,10 @@ Gem::Specification.new do |gem|
15
15
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
- gem.add_dependency('rash')
18
+ gem.add_dependency('hashie', '>= 2.1.0')
19
19
  gem.add_dependency('faraday')
20
20
  gem.add_dependency('faraday_middleware')
21
- gem.add_dependency('activesupport', '>= 3.0.0')
22
- gem.add_development_dependency('rspec')
21
+ gem.add_dependency('activesupport', '>= 3.0', '<= 5.0')
22
+ gem.add_development_dependency('rspec', '>= 3.0')
23
23
  gem.add_development_dependency('webmock')
24
24
  end
@@ -45,7 +45,7 @@ describe ::Nexmos::Account do
45
45
  "ranges" => ["35846", "35850"],
46
46
  "mtPrice" => "0.05000000"}],
47
47
  "name" => "Finland"
48
- }
48
+ }.to_json
49
49
  end
50
50
 
51
51
  let(:prefix_prices) do
@@ -80,7 +80,7 @@ describe ::Nexmos::Account do
80
80
  "ranges" => nil,
81
81
  "mtPrice" => "0.05000000"}],
82
82
  "name" => "Finland"}],
83
- }
83
+ }.to_json
84
84
  end
85
85
 
86
86
  before(:each) do
@@ -97,11 +97,11 @@ describe ::Nexmos::Account do
97
97
 
98
98
  it 'should return value' do
99
99
  request = stub_request(:get, "https://rest.nexmo.com/account/get-balance?api_key=default_key&api_secret=default_secret").
100
- with(webmock_default_headers).to_return(:status => 200, :body => {"value" => 4.107}, :headers => {})
100
+ with(webmock_default_headers).to_return(:status => 200, :body => {"value" => 4.107}.to_json, headers: {'Content-Type' => 'application/json'})
101
101
  res = subject.get_balance
102
- res.should be_kind_of(::Hash)
103
- res.value.should == 4.107
104
- request.should have_been_made.once
102
+ expect(res).to be_kind_of(::Hash)
103
+ expect(res.value).to eq(4.107)
104
+ expect(request).to have_been_made.once
105
105
  end
106
106
  end
107
107
 
@@ -112,15 +112,15 @@ describe ::Nexmos::Account do
112
112
 
113
113
  it 'should be success' do
114
114
  request = stub_request(:get, "https://rest.nexmo.com/account/get-pricing/outbound?api_key=default_key&api_secret=default_secret&country=FI").
115
- with(webmock_default_headers).to_return(:status => 200, :body => finland_prices, :headers => {})
115
+ with(webmock_default_headers).to_return(:status => 200, :body => finland_prices, :headers => {'Content-Type' => 'application/json'})
116
116
  res = subject.get_pricing(:country => 'FI')
117
- res.should be_kind_of(::Hash)
118
- res.success?.should be_true
119
- res.country.should == 'FI'
120
- res.keys.sort.should == %w(country name prefix mt networks success?).sort
121
- res.networks.should be_kind_of(::Array)
122
- res.networks[0].keys.sort.should == %w(code network mt_price ranges).sort
123
- request.should have_been_made.once
117
+ expect(res).to be_kind_of(::Hash)
118
+ expect(res.success?).to be_truthy
119
+ expect(res.country).to eq('FI')
120
+ expect(res.keys.sort).to eq(%w(country name prefix mt networks success?).sort)
121
+ expect(res.networks).to be_kind_of(::Array)
122
+ expect(res.networks[0].keys.sort).to eq(%w(code network mt_price ranges).sort)
123
+ expect(request).to have_been_made.once
124
124
  end
125
125
  end
126
126
 
@@ -131,41 +131,47 @@ describe ::Nexmos::Account do
131
131
 
132
132
  it 'should be success' do
133
133
  request = stub_request(:get, "https://rest.nexmo.com/account/get-prefix-pricing/outbound?api_key=default_key&api_secret=default_secret&prefix=358").
134
- with(webmock_default_headers).to_return(:status => 200, :body => prefix_prices, :headers => {})
134
+ with(webmock_default_headers).to_return(:status => 200, :body => prefix_prices, :headers => {'Content-Type' => 'application/json'})
135
135
  res = subject.get_prefix_pricing(:prefix => '358')
136
- res.should be_kind_of(::Hash)
137
- res.success?.should be_true
138
- res.keys.sort.should == %w(count prices success?).sort
139
- res.prices.should be_kind_of(::Array)
140
- res.prices[0].keys.sort.should == %w(country name prefix mt networks).sort
141
- res.prices[0].networks.should be_kind_of(::Array)
142
- res.prices[0].networks[0].keys.sort.should == %w(code network mt_price ranges).sort
143
- request.should have_been_made.once
136
+ expect(res).to be_kind_of(::Hash)
137
+ expect(res.success?).to be_truthy
138
+ expect(res.keys.sort).to eq(%w(count prices success?).sort)
139
+ expect(res.prices).to be_kind_of(::Array)
140
+ expect(res.prices[0].keys.sort).to eq(%w(country name prefix mt networks).sort)
141
+ expect(res.prices[0].networks).to be_kind_of(::Array)
142
+ expect(res.prices[0].networks[0].keys.sort).to eq(%w(code network mt_price ranges).sort)
143
+ expect(request).to have_been_made.once
144
144
  end
145
145
  end
146
146
 
147
147
  context '#get_numbers' do
148
148
  it 'should return only count on empty numbers' do
149
149
  request = stub_request(:get, "https://rest.nexmo.com/account/numbers?api_key=default_key&api_secret=default_secret").
150
- with(webmock_default_headers).to_return(:status => 200, :body => {:count => 0}, :headers => {})
150
+ with(webmock_default_headers).to_return(:status => 200, :body => {:count => 0}.to_json, :headers => {'Content-Type' => 'application/json'})
151
151
  res = subject.get_numbers
152
- res.should be_kind_of(::Hash)
153
- res.success?.should be_true
154
- res['count'].should == 0
155
- request.should have_been_made.once
152
+ expect(res).to be_kind_of(::Hash)
153
+ expect(res.success?).to be_truthy
154
+ expect(res['count']).to eq(0)
155
+ expect(request).to have_been_made.once
156
156
  end
157
157
 
158
158
  it 'should return numbers array' do
159
159
  request = stub_request(:get, "https://rest.nexmo.com/account/numbers?api_key=default_key&api_secret=default_secret").
160
- with(webmock_default_headers).to_return(:status => 200, :body => { "count" => 1,"numbers" => [{"country" => "ES","msisdn" => "34911067000","type" => "landline"}]}, :headers => {})
160
+ with(webmock_default_headers).to_return(:status => 200,
161
+ :body => {
162
+ "count" => 1,
163
+ "numbers" => [
164
+ {"country" => "ES","msisdn" => "34911067000","type" => "landline"}
165
+ ]
166
+ }.to_json, :headers => {'Content-Type' => 'application/json'})
161
167
  res = subject.get_numbers
162
- res.should be_kind_of(::Hash)
163
- res.success?.should be_true
164
- res['count'].should == 1
165
- res['numbers'].should be_kind_of(::Array)
166
- res['numbers'].first.should be_kind_of(::Hash)
167
- res['numbers'].first.should == {"country" => "ES","msisdn" => "34911067000","type" => "landline"}
168
- request.should have_been_made.once
168
+ expect(res).to be_kind_of(::Hash)
169
+ expect(res.success?).to be_truthy
170
+ expect(res['count']).to eq(1)
171
+ expect(res['numbers']).to be_kind_of(::Array)
172
+ expect(res['numbers'].first).to be_kind_of(::Hash)
173
+ expect(res['numbers'].first).to eq({"country" => "ES","msisdn" => "34911067000","type" => "landline"})
174
+ expect(request).to have_been_made.once
169
175
  end
170
176
  end
171
177
 
@@ -176,11 +182,11 @@ describe ::Nexmos::Account do
176
182
 
177
183
  it 'should success top up' do
178
184
  request = stub_request(:get, "https://rest.nexmo.com/account/top-up?api_key=default_key&api_secret=default_secret&trx=test_trx").
179
- with(webmock_default_headers).to_return(:status => 200, :body => '', :headers => {})
185
+ with(webmock_default_headers).to_return(:status => 200, :body => '', :headers => {'Content-Type' => 'application/json'})
180
186
  res = subject.top_up :trx => 'test_trx'
181
- res.should be_kind_of(::Hash)
182
- res.success?.should be_true
183
- request.should have_been_made.once
187
+ expect(res).to be_kind_of(::Hash)
188
+ expect(res.success?).to be_truthy
189
+ expect(request).to have_been_made.once
184
190
  end
185
191
 
186
192
  end
@@ -4,9 +4,9 @@ describe ::Nexmos::Base do
4
4
  let(:webmock_default_headers) do
5
5
  {
6
6
  :headers => {
7
- 'Accept'=>'application/json',
8
- 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
9
- 'User-Agent'=> ::Nexmos.user_agent
7
+ 'Accept' => 'application/json',
8
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
9
+ 'User-Agent' => ::Nexmos.user_agent
10
10
  }
11
11
  }
12
12
  end
@@ -14,20 +14,28 @@ describe ::Nexmos::Base do
14
14
  before(:each) do
15
15
  ::Nexmos.reset!
16
16
  end
17
+
17
18
  context 'class' do
18
- subject { ::Nexmos::Base }
19
+ subject { described_class }
19
20
  let(:default_faraday_options) do
20
21
  {
21
- :url => 'https://rest.nexmo.com',
22
+ :url => 'https://rest.nexmo.com',
22
23
  :headers => {
23
- :accept => 'application/json',
24
+ :accept => 'application/json',
24
25
  :user_agent => ::Nexmos.user_agent
25
26
  }
26
27
  }
27
28
  end
28
29
 
29
- its(:faraday_options) { should == default_faraday_options }
30
- its(:connection) { should be_kind_of(::Faraday::Connection) }
30
+ describe '#faraday_options' do
31
+ subject { super().faraday_options }
32
+ it { is_expected.to eq(default_faraday_options) }
33
+ end
34
+
35
+ describe '#connection' do
36
+ subject { super().connection }
37
+ it { is_expected.to be_kind_of(::Faraday::Connection) }
38
+ end
31
39
 
32
40
  context 'faraday_options' do
33
41
  it 'should have custom user agent' do
@@ -35,14 +43,14 @@ describe ::Nexmos::Base do
35
43
  c.user_agent = 'test user agent'
36
44
  end
37
45
  default_faraday_options[:headers][:user_agent] = 'test user agent'
38
- subject.faraday_options.should == default_faraday_options
46
+ expect(subject.faraday_options).to eq(default_faraday_options)
39
47
  end
40
48
  end
41
49
 
42
50
  context 'define_api_calls' do
43
51
  it 'should call define_method' do
44
52
  ::Nexmos.apis[:account].keys.each do |k|
45
- subject.should_receive(:define_method).with(k)
53
+ expect(subject).to receive(:define_method).with(k)
46
54
  end
47
55
  subject.define_api_calls(:account)
48
56
  end
@@ -50,7 +58,7 @@ describe ::Nexmos::Base do
50
58
  it 'should define dynamic method and call make_api_call inside' do
51
59
  subject.define_api_calls(:account)
52
60
  instance = subject.new('test_key', 'test_secret')
53
- instance.should_receive(:make_api_call).with(::Nexmos.apis[:account][:get_balance], {})
61
+ expect(instance).to receive(:make_api_call).with(::Nexmos.apis[:account][:get_balance], {})
54
62
  instance.get_balance
55
63
  end
56
64
  end
@@ -61,140 +69,143 @@ describe ::Nexmos::Base do
61
69
  subject { ::Nexmos::Base.new('test_api', 'test_secret') }
62
70
  context 'new' do
63
71
  it 'should raise on empty api_key' do
64
- expect {::Nexmos::Base.new('', 'test_secret')}.to raise_error('api_key should be set')
72
+ expect { ::Nexmos::Base.new('', 'test_secret') }.to raise_error('api_key should be set')
65
73
  end
66
74
  it 'should raise on empty api_secret' do
67
- expect {::Nexmos::Base.new('test_key', '')}.to raise_error('api_secret should be set')
75
+ expect { ::Nexmos::Base.new('test_key', '') }.to raise_error('api_secret should be set')
68
76
  end
69
77
  it 'should set default_params with values from ::Nexmos module' do
70
78
  ::Nexmos.setup do |c|
71
- c.api_key = 'default_key'
79
+ c.api_key = 'default_key'
72
80
  c.api_secret = 'default_secret'
73
81
  end
74
82
  instance = ::Nexmos::Base.new
75
- instance.instance_variable_get('@default_params').should == {'api_key' => 'default_key', 'api_secret' => 'default_secret'}
83
+ expect(instance.instance_variable_get('@default_params')).to eq({ 'api_key' => 'default_key', 'api_secret' => 'default_secret' })
76
84
  end
77
85
  it 'should set default_params with custom api key and secret' do
78
86
  instance = ::Nexmos::Base.new('test_key', 'test_secret')
79
- instance.instance_variable_get('@default_params').should == {'api_key' => 'test_key', 'api_secret' => 'test_secret'}
87
+ expect(instance.instance_variable_get('@default_params')).to eq({ 'api_key' => 'test_key', 'api_secret' => 'test_secret' })
80
88
  end
81
89
  end
82
90
 
83
- its(:connection) { should be_kind_of(::Faraday::Connection) }
91
+ describe '#connection' do
92
+ subject { super().connection }
93
+ it { is_expected.to be_kind_of(::Faraday::Connection) }
94
+ end
84
95
 
85
96
  context 'make_api_call' do
86
97
 
87
98
  before(:each) do
88
99
  stub_request(:get, "https://rest.nexmo.com/test/url?api_key=test_api&api_secret=test_secret").
89
- with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=> ::Nexmos.user_agent}).
90
- to_return(:status => 200, :body => {'key' => 'value'}, :headers => {})
100
+ with(:headers => { 'Accept' => 'application/json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => ::Nexmos.user_agent }).
101
+ to_return(:status => 200, :body => { 'key' => 'value' }.to_json, :headers => { 'Content-Type' => 'application/json' })
91
102
  end
92
103
 
93
104
  let(:api_params_without_required) do
94
105
  {
95
106
  :method => :get,
96
- :url => '/test/url'
107
+ :url => '/test/url'
97
108
  }
98
109
  end
99
110
 
100
111
  let(:api_params_with_required) do
101
112
  {
102
- :method => :get,
103
- :url => '/test/url',
113
+ :method => :get,
114
+ :url => '/test/url',
104
115
  :required => %w(key1 key2)
105
116
  }
106
117
  end
107
118
 
108
119
  it 'should call check_required_params' do
109
- subject.should_receive(:check_required_params)
120
+ expect(subject).to receive(:check_required_params)
110
121
  subject.make_api_call(api_params_with_required)
111
122
  end
112
123
 
113
124
  it 'should raise if all required params missing' do
114
- expect{subject.make_api_call(api_params_with_required)}.to raise_error('key1,key2 params required')
125
+ expect { subject.make_api_call(api_params_with_required) }.to raise_error('key1,key2 params required')
115
126
  end
116
127
 
117
128
  it 'should raise if some required params missing' do
118
- expect{subject.make_api_call(api_params_with_required,{:key1 => 'val'})}.to raise_error('key2 params required')
129
+ expect { subject.make_api_call(api_params_with_required, { :key1 => 'val' }) }.to raise_error('key2 params required')
119
130
  end
120
131
 
121
132
  it 'should call normalize_params' do
122
- subject.should_receive(:normalize_params).and_call_original
133
+ expect(subject).to receive(:normalize_params).and_call_original
123
134
  subject.make_api_call(api_params_without_required)
124
135
  end
125
136
 
126
137
  it 'should not call camelize_params' do
127
138
  stub_request(:get, "https://rest.nexmo.com/test/url?api_key=test_api&api_secret=test_secret&test_call=value").
128
- with(webmock_default_headers).
129
- to_return(:status => 200, :body => {}, :headers => {})
130
- subject.should_not_receive(:camelize_params)
131
- subject.make_api_call(api_params_without_required, {'test_call' => 'value'})
139
+ with(webmock_default_headers).
140
+ to_return(:status => 200, :body => {}.to_json, :headers => { 'Content-Type' => 'application/json' })
141
+ expect(subject).not_to receive(:camelize_params)
142
+ subject.make_api_call(api_params_without_required, { 'test_call' => 'value' })
132
143
  end
133
144
 
134
145
  it 'should call camelize_params' do
135
146
  stub_request(:get, "https://rest.nexmo.com/test/url?api_key=test_api&api_secret=test_secret&testCall=value").
136
- with(webmock_default_headers).
137
- to_return(:status => 200, :body => {}, :headers => {})
138
- subject.should_receive(:camelize_params).and_call_original
139
- subject.make_api_call(api_params_without_required.merge(:camelize => true), {'test_call' => 'value'})
147
+ with(webmock_default_headers).
148
+ to_return(:status => 200, :body => {}.to_json, :headers => { 'Content-Type' => 'application/json' })
149
+ expect(subject).to receive(:camelize_params).and_call_original
150
+ subject.make_api_call(api_params_without_required.merge(:camelize => true), { 'test_call' => 'value' })
140
151
  end
141
152
 
142
153
  it 'should call get_response' do
143
154
  stub_request(:get, "https://rest.nexmo.com/test/url?api_key=test_api&api_secret=test_secret").
144
- with(webmock_default_headers).
145
- to_return(:status => 200, :body => {}, :headers => {})
146
- subject.should_receive(:get_response).and_call_original
155
+ with(webmock_default_headers).
156
+ to_return(:status => 200, :body => {}.to_json, :headers => { 'Content-Type' => 'application/json' })
157
+ expect(subject).to receive(:get_response).and_call_original
147
158
  subject.make_api_call(api_params_without_required)
148
159
  end
149
160
 
150
161
  it 'should return hash' do
151
- subject.make_api_call(api_params_without_required).should be_a_kind_of(::Hash)
162
+ expect(subject.make_api_call(api_params_without_required)).to be_a_kind_of(::Hash)
152
163
  end
153
164
 
154
165
  it 'should return Hashie::Mash' do
155
- subject.make_api_call(api_params_without_required).should be_a_kind_of(::Hashie::Mash)
166
+ expect(subject.make_api_call(api_params_without_required)).to be_a_kind_of(::Hashie::Mash)
156
167
  end
157
168
 
158
169
  it 'should respond to success? method in result' do
159
- subject.make_api_call(api_params_without_required).should respond_to(:success?)
170
+ expect(subject.make_api_call(api_params_without_required)).to respond_to(:success?)
160
171
  end
161
172
 
162
173
  it 'should have success? key in hash' do
163
- subject.make_api_call(api_params_without_required)[:success?].should be
174
+ expect(subject.make_api_call(api_params_without_required)[:success?]).to be
164
175
  end
165
176
 
166
177
  it 'should have success? == true' do
167
- subject.make_api_call(api_params_without_required)[:success?].should be_true
178
+ expect(subject.make_api_call(api_params_without_required)[:success?]).to be_truthy
168
179
  end
169
180
 
170
181
  it 'should have success? == false on response with status != 200' do
171
182
  stub_request(:get, "https://rest.nexmo.com/test/url?api_key=test_api&api_secret=test_secret&testCall=value").
172
- with(webmock_default_headers).
173
- to_return(:status => 410, :body => {}, :headers => {})
174
- res = subject.make_api_call(api_params_without_required.merge(:camelize => true), {'test_call' => 'value'})
175
- res[:success?].should be_false
176
- res[:failed?].should be_false
177
- res[:not_authorized?].should be_false
183
+ with(webmock_default_headers).
184
+ to_return(:status => 410, :body => {}.to_json, :headers => { 'Content-Type' => 'application/json' })
185
+ res = subject.make_api_call(api_params_without_required.merge(:camelize => true), { 'test_call' => 'value' })
186
+ expect(res[:success?]).to be_falsey
187
+ expect(res[:failed?]).to be_falsey
188
+ expect(res[:not_authorized?]).to be_falsey
178
189
  end
179
190
 
180
191
  it 'should have not_authorized? == true' do
181
192
  stub_request(:get, "https://rest.nexmo.com/test/url?api_key=test_api&api_secret=test_secret&testCall=value").
182
- with(webmock_default_headers).
183
- to_return(:status => 401, :body => {}, :headers => {})
184
- res = subject.make_api_call(api_params_without_required.merge(:camelize => true), {'test_call' => 'value'})
185
- res[:success?].should be_false
186
- res[:not_authorized?].should be_true
187
- res[:failed?].should be_false
193
+ with(webmock_default_headers).
194
+ to_return(:status => 401, :body => {}.to_json, :headers => { 'Content-Type' => 'application/json' })
195
+ res = subject.make_api_call(api_params_without_required.merge(:camelize => true), { 'test_call' => 'value' })
196
+ expect(res[:success?]).to be_falsey
197
+ expect(res[:not_authorized?]).to be_truthy
198
+ expect(res[:failed?]).to be_falsey
188
199
  end
189
200
 
190
201
  it 'should have failed? == true' do
191
202
  stub_request(:get, "https://rest.nexmo.com/test/url?api_key=test_api&api_secret=test_secret&testCall=value").
192
- with(webmock_default_headers).
193
- to_return(:status => 420, :body => {}, :headers => {})
194
- res = subject.make_api_call(api_params_without_required.merge(:camelize => true), {'test_call' => 'value'})
195
- res[:success?].should be_false
196
- res[:not_authorized?].should be_false
197
- res[:failed?].should be_true
203
+ with(webmock_default_headers).
204
+ to_return(:status => 420, :body => {}.to_json, :headers => { 'Content-Type' => 'application/json' })
205
+ res = subject.make_api_call(api_params_without_required.merge(:camelize => true), { 'test_call' => 'value' })
206
+ expect(res[:success?]).to be_falsey
207
+ expect(res[:not_authorized?]).to be_falsey
208
+ expect(res[:failed?]).to be_truthy
198
209
  end
199
210
 
200
211
  end