nexmo 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +17 -13
  2. data/lib/nexmo.rb +20 -6
  3. data/nexmo.gemspec +2 -2
  4. data/spec/nexmo_spec.rb +41 -6
  5. metadata +10 -9
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
- A Ruby wrapper for the [Nexmo](http://nexmo.com/) API
2
- =====================================================
1
+ nexmo
2
+ =====
3
+
4
+
5
+ A Ruby wrapper for the [Nexmo API](https://www.nexmo.com/documentation/api/index.html).
3
6
 
4
7
 
5
8
  Installation
@@ -84,22 +87,23 @@ OAuth::Consumer.new(consumer_key, consumer_secret, {
84
87
  Using the `:body` or `:query_string` authorization mechanisms is not supported.
85
88
 
86
89
 
87
- Switching JSON implementations
88
- ------------------------------
90
+ Custom response behaviour
91
+ -------------------------
89
92
 
90
- By default the "json" library is used to encode request bodies and decode response
91
- bodies. This is available in the Ruby 1.9 standard library, and as a gem for Ruby 1.8.
92
- You can specify an alternate implementation that you wish to use explicitly when
93
- constructing a client object. For example, to use [multi_json](https://rubygems.org/gems/multi_json):
93
+ You can customise the response handling by passing a block when constructing
94
+ a Nexmo::Client object. For example, if you want to use an alternative JSON
95
+ implementation to decode response bodies, you could do this:
94
96
 
95
97
  ```ruby
96
- require 'nexmo'
97
- require 'multi_json'
98
-
99
- nexmo = Nexmo::Client.new('...API KEY...', '...API SECRET...', :json => MultiJson)
98
+ nexmo = Nexmo::Client.new do |response|
99
+ response.object = MultiJson.load(response.body) if response.ok? && response.json?
100
+ response
101
+ end
100
102
  ```
101
103
 
102
- Ditto for anything that is compatible with the default implementation.
104
+ This might also be useful if you prefer a different style of error handling
105
+ (e.g. raising exceptions on error and returning the response object directly
106
+ for 200 OK responses), if you need to log responses etc.
103
107
 
104
108
 
105
109
  Troubleshooting
data/lib/nexmo.rb CHANGED
@@ -5,10 +5,16 @@ require 'cgi'
5
5
 
6
6
  module Nexmo
7
7
  class Client
8
- def initialize(key = ENV['NEXMO_API_KEY'], secret = ENV['NEXMO_API_SECRET'], options = {})
9
- @key, @secret = key, secret
8
+ def initialize(key = ENV['NEXMO_API_KEY'], secret = ENV['NEXMO_API_SECRET'], options = {}, &block)
9
+ @key, @secret, @block = key, secret, block
10
10
 
11
- @json = options.fetch(:json) { JSON }
11
+ if options.has_key?(:json)
12
+ Kernel.warn '[nexmo] :json option is deprecated'
13
+
14
+ @json = options[:json]
15
+ else
16
+ @json = JSON
17
+ end
12
18
 
13
19
  @http = Net::HTTP.new('rest.nexmo.com', Net::HTTP.https_default_port)
14
20
 
@@ -80,7 +86,7 @@ module Nexmo
80
86
  @http.get(request_uri(path, params.merge(:api_key => @key, :api_secret => @secret)))
81
87
  end
82
88
 
83
- Response.new(http_response, :json => @json)
89
+ decode(http_response)
84
90
  end
85
91
 
86
92
  def post(path, params)
@@ -90,7 +96,13 @@ module Nexmo
90
96
  @http.post(path, @json.dump(params.merge(:api_key => @key, :api_secret => @secret)), {'Content-Type' => 'application/json'})
91
97
  end
92
98
 
93
- Response.new(http_response, :json => @json)
99
+ decode(http_response)
100
+ end
101
+
102
+ def decode(http_response)
103
+ response = Response.new(http_response, :json => @json)
104
+
105
+ @block ? @block.call(response) : response
94
106
  end
95
107
 
96
108
  def request_uri(path, hash = {})
@@ -111,6 +123,8 @@ module Nexmo
111
123
  end
112
124
 
113
125
  class Response
126
+ attr_writer :object
127
+
114
128
  def initialize(http_response, options = {})
115
129
  @http_response = http_response
116
130
 
@@ -134,7 +148,7 @@ module Nexmo
134
148
  end
135
149
 
136
150
  def object
137
- @object ||= @json.load(body)
151
+ @object ||= @json.parse(body)
138
152
  end
139
153
  end
140
154
 
data/nexmo.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'nexmo'
3
- s.version = '1.1.0'
3
+ s.version = '1.2.0'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ['Tim Craft']
6
6
  s.email = ['mail@timcraft.com']
@@ -10,8 +10,8 @@ Gem::Specification.new do |s|
10
10
  s.files = Dir.glob('{lib,spec}/**/*') + %w(README.md nexmo.gemspec)
11
11
  s.add_development_dependency('rake', '>= 0.9.3')
12
12
  s.add_development_dependency('mocha', '~> 0.13.2')
13
- s.add_development_dependency('multi_json', '~> 1.3.6')
14
13
  s.add_development_dependency('oauth', '~> 0.4.7')
14
+ s.add_development_dependency('faux', '~> 1.1.0')
15
15
  s.require_path = 'lib'
16
16
 
17
17
  if RUBY_VERSION == '1.8.7'
data/spec/nexmo_spec.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'minitest/autorun'
2
2
  require 'mocha/setup'
3
- require 'multi_json'
3
+ require 'faux'
4
4
  require 'nexmo'
5
5
  require 'oauth'
6
+ require 'json'
6
7
 
7
8
  describe 'Nexmo::Client' do
8
9
  before do
@@ -162,13 +163,21 @@ describe 'Nexmo::Client' do
162
163
  end
163
164
 
164
165
  describe 'when initialized with a different json implementation' do
165
- before do
166
- @client = Nexmo::Client.new('key', 'secret', :json => MultiJson)
166
+ it 'emits a deprecation warning' do
167
+ Kernel.expects(:warn).with(regexp_matches(/:json option is deprecated/))
168
+
169
+ @client = Nexmo::Client.new('key', 'secret', :json => stub)
167
170
  end
168
171
 
169
172
  describe 'send_message method' do
170
173
  it 'encodes the request body using the alternative json implementation' do
171
- MultiJson.expects(:dump).with(instance_of(Hash))
174
+ Kernel.stubs(:warn)
175
+
176
+ @json = faux(JSON)
177
+
178
+ @json.expects(:dump).with(instance_of(Hash))
179
+
180
+ @client = Nexmo::Client.new('key', 'secret', :json => @json)
172
181
 
173
182
  @client.http.stubs(:post)
174
183
 
@@ -176,6 +185,20 @@ describe 'Nexmo::Client' do
176
185
  end
177
186
  end
178
187
  end
188
+
189
+ describe 'when initialized with a block' do
190
+ it 'calls the block for each response and returns the return value of the block' do
191
+ @client = Nexmo::Client.new('key', 'secret') do |response|
192
+ response.must_be_instance_of(Nexmo::Response)
193
+
194
+ :return_value
195
+ end
196
+
197
+ @client.http.stubs(:get).returns(stub)
198
+
199
+ @client.get_balance.must_equal(:return_value)
200
+ end
201
+ end
179
202
  end
180
203
 
181
204
  describe 'Nexmo::Response' do
@@ -228,14 +251,26 @@ describe 'Nexmo::Response' do
228
251
  end
229
252
  end
230
253
 
254
+ describe 'object equals method' do
255
+ it 'sets the object to be returned by the object method' do
256
+ @object = stub
257
+
258
+ @response.object = @object
259
+
260
+ @response.object.must_equal(@object)
261
+ end
262
+ end
263
+
231
264
  describe 'when initialized with a different json implementation' do
232
265
  before do
233
- @response = Nexmo::Response.new(@http_response, :json => MultiJson)
266
+ @json = faux(JSON)
267
+
268
+ @response = Nexmo::Response.new(@http_response, :json => @json)
234
269
  end
235
270
 
236
271
  describe 'object method' do
237
272
  it 'decodes the response body using the alternative json implementation' do
238
- MultiJson.expects(:load).with('{"value":0.0}')
273
+ @json.expects(:parse).with('{"value":0.0}')
239
274
 
240
275
  @http_response.stubs(:body).returns('{"value":0.0}')
241
276
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexmo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
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: 2013-02-10 00:00:00.000000000 Z
12
+ date: 2013-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -44,13 +44,13 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: 0.13.2
46
46
  - !ruby/object:Gem::Dependency
47
- name: multi_json
47
+ name: oauth
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 1.3.6
53
+ version: 0.4.7
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,15 +58,15 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 1.3.6
61
+ version: 0.4.7
62
62
  - !ruby/object:Gem::Dependency
63
- name: oauth
63
+ name: faux
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
- version: 0.4.7
69
+ version: 1.1.0
70
70
  type: :development
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
- version: 0.4.7
77
+ version: 1.1.0
78
78
  description: A Ruby wrapper for the Nexmo API
79
79
  email:
80
80
  - mail@timcraft.com
@@ -106,8 +106,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  requirements: []
108
108
  rubyforge_project:
109
- rubygems_version: 1.8.24
109
+ rubygems_version: 1.8.23
110
110
  signing_key:
111
111
  specification_version: 3
112
112
  summary: See description
113
113
  test_files: []
114
+ has_rdoc: