nexmo 0.2.2 → 0.3.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.
@@ -0,0 +1,56 @@
1
+ A simple wrapper for the [Nexmo](http://nexmo.com/) API
2
+ =======================================================
3
+
4
+
5
+ Installation
6
+ ------------
7
+
8
+ Run `gem install nexmo` and `require 'nexmo'`,
9
+ or do the gemfile/bundle thing if you're using Rails.
10
+
11
+
12
+ Usage
13
+ -----
14
+
15
+ Construct a client object with your Nexmo API credentials:
16
+
17
+ ```ruby
18
+ nexmo = Nexmo::Client.new('...KEY...', '...SECRET...')
19
+ ```
20
+
21
+ The underlying HTTP object is easily accessible. For example, you may want
22
+ to adjust the SSL verification when testing locally:
23
+
24
+ ```ruby
25
+ nexmo.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
26
+ ```
27
+
28
+ Use the `send_message` method to send an SMS, passing the API
29
+ parameters as a hash:
30
+
31
+ ```ruby
32
+ response = nexmo.send_message({
33
+ from: 'RUBY',
34
+ to: '...NUMBER...',
35
+ text: 'Hello world'
36
+ })
37
+ ```
38
+
39
+ Phone numbers should be specified in international format. If the response
40
+ is successful you can access the message id, and if it's a failure you can
41
+ retrieve the error message and/or the underlying HTTP response returned from
42
+ the server:
43
+
44
+ ```ruby
45
+ if response.success?
46
+ # store response.message_id
47
+ elsif response.failure?
48
+ # check response.error.message and/or response.http
49
+ # raise response.error
50
+ end
51
+ ```
52
+
53
+ The Nexmo documentation contains a [list of error codes](http://nexmo.com/documentation/index.html#dlr_error)
54
+ which may be useful if you have problems sending a message.
55
+
56
+ That's all folks. Chunky bacon.
@@ -20,14 +20,18 @@ module Nexmo
20
20
  def send_message(data)
21
21
  response = @http.post('/sms/json', encode(data), headers)
22
22
 
23
- object = JSON.parse(response.body)['messages'].first
23
+ if response.code.to_i == 200 && response['Content-Type'] == 'application/json'
24
+ object = JSON.parse(response.body)['messages'].first
24
25
 
25
- status = object['status'].to_i
26
+ status = object['status'].to_i
26
27
 
27
- if status == 0
28
- Success.new(object['message-id'])
28
+ if status == 0
29
+ Success.new(object['message-id'])
30
+ else
31
+ Failure.new(Error.new("#{object['error-text']} (status=#{status})"), response)
32
+ end
29
33
  else
30
- Failure.new(Error.new("#{object['error-text']} (status=#{status})"))
34
+ Failure.new(Error.new("Unexpected HTTP response (code=#{response.code})"), response)
31
35
  end
32
36
  end
33
37
 
@@ -48,7 +52,7 @@ module Nexmo
48
52
  end
49
53
  end
50
54
 
51
- class Failure < Struct.new(:error)
55
+ class Failure < Struct.new(:error, :http)
52
56
  def success?
53
57
  false
54
58
  end
@@ -1,13 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'nexmo'
3
- s.version = '0.2.2'
3
+ s.version = '0.3.0'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ['Tim Craft']
6
6
  s.email = ['mail@timcraft.com']
7
7
  s.homepage = 'http://github.com/timcraft/nexmo'
8
8
  s.description = 'A simple wrapper for the Nexmo API'
9
9
  s.summary = 'See description'
10
- s.files = Dir.glob('{lib,spec}/**/*') + %w(README.txt nexmo.gemspec)
10
+ s.files = Dir.glob('{lib,spec}/**/*') + %w(README.md nexmo.gemspec)
11
11
  s.add_dependency('json', ['~> 1.5'])
12
12
  s.add_development_dependency('mocha')
13
13
  s.require_path = 'lib'
@@ -26,8 +26,9 @@ describe Nexmo::Client do
26
26
  @headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
27
27
  end
28
28
 
29
- it 'should make the correct http call return a successful response if the first message status equals 0' do
30
- http_response = stub(:body => '{"messages":[{"status":0,"message-id":"id"}]}')
29
+ it 'should make the correct http call and return a success object if the first message status equals 0' do
30
+ http_response = stub(:code => '200', :body => '{"messages":[{"status":0,"message-id":"id"}]}')
31
+ http_response.expects(:[]).with('Content-Type').returns('application/json')
31
32
 
32
33
  data = 'from=ruby&to=number&text=Hey%21&username=key&password=secret'
33
34
 
@@ -40,8 +41,9 @@ describe Nexmo::Client do
40
41
  response.message_id.must_equal('id')
41
42
  end
42
43
 
43
- it 'should return a failure response if the first message status does not equal 0' do
44
- http_response = stub(:body => '{"messages":[{"status":2,"error-text":"Missing from param"}]}')
44
+ it 'should return a failure object if the first message status does not equal 0' do
45
+ http_response = stub(:code => '200', :body => '{"messages":[{"status":2,"error-text":"Missing from param"}]}')
46
+ http_response.expects(:[]).with('Content-Type').returns('application/json')
45
47
 
46
48
  data = 'to=number&text=Hey%21&username=key&password=secret'
47
49
 
@@ -52,6 +54,22 @@ describe Nexmo::Client do
52
54
  response.success?.must_equal(false)
53
55
  response.failure?.must_equal(true)
54
56
  response.error.to_s.must_equal('Missing from param (status=2)')
57
+ response.http.wont_be_nil
58
+ end
59
+
60
+ it 'should return a failure object if the server returns an unexpected http response' do
61
+ http_response = stub(:code => '503')
62
+
63
+ data = 'from=ruby&to=number&text=Hey%21&username=key&password=secret'
64
+
65
+ @client.http.expects(:post).with('/sms/json', data, @headers).returns(http_response)
66
+
67
+ response = @client.send_message({from: 'ruby', to: 'number', text: 'Hey!'})
68
+
69
+ response.success?.must_equal(false)
70
+ response.failure?.must_equal(true)
71
+ response.error.to_s.must_equal('Unexpected HTTP response (code=503)')
72
+ response.http.wont_be_nil
55
73
  end
56
74
  end
57
75
  end
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: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-21 00:00:00.000000000 Z
12
+ date: 2012-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &3707430 !ruby/object:Gem::Requirement
16
+ requirement: &3132970 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.5'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *3707430
24
+ version_requirements: *3132970
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &3707100 !ruby/object:Gem::Requirement
27
+ requirement: &3132770 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *3707100
35
+ version_requirements: *3132770
36
36
  description: A simple wrapper for the Nexmo API
37
37
  email:
38
38
  - mail@timcraft.com
@@ -42,7 +42,7 @@ extra_rdoc_files: []
42
42
  files:
43
43
  - lib/nexmo.rb
44
44
  - spec/nexmo_spec.rb
45
- - README.txt
45
+ - README.md
46
46
  - nexmo.gemspec
47
47
  homepage: http://github.com/timcraft/nexmo
48
48
  licenses: []
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  version: '0'
65
65
  requirements: []
66
66
  rubyforge_project:
67
- rubygems_version: 1.8.10
67
+ rubygems_version: 1.8.16
68
68
  signing_key:
69
69
  specification_version: 3
70
70
  summary: See description
data/README.txt DELETED
@@ -1,42 +0,0 @@
1
- A simple wrapper for the Nexmo API (http://nexmo.com/).
2
-
3
- Install via rubygems:
4
-
5
- gem install nexmo
6
-
7
- Either require it, or add it to your Rails Gemfile:
8
-
9
- require 'nexmo'
10
-
11
- gem 'nexmo'
12
-
13
- Construct a client with your Nexmo API credentials:
14
-
15
- nexmo = Nexmo::Client.new('...KEY...', '...SECRET...')
16
-
17
- The underlying HTTP object is easily accessible. For example, you may want
18
- to adjust the SSL verification when testing locally:
19
-
20
- nexmo.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
21
-
22
- Use the send_message method to send an SMS, passing the API parameters
23
- as a hash:
24
-
25
- response = nexmo.send_message({
26
- from: 'RUBY',
27
- to: '...NUMBER...',
28
- text: 'Hello world'
29
- })
30
-
31
- If the response is successful you can access the message id, and if it's
32
- a failure you can either check the error message or choose to raise the
33
- error as an exception:
34
-
35
- if response.success?
36
- # store response.message_id
37
- elsif response.failure?
38
- # check response.error.message
39
- # raise response.error
40
- end
41
-
42
- Chunky bacon.