nexmo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.txt +42 -0
  2. data/lib/nexmo.rb +68 -0
  3. data/nexmo.gemspec +13 -0
  4. metadata +61 -0
@@ -0,0 +1,42 @@
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.
@@ -0,0 +1,68 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ module Nexmo
6
+ class Client
7
+ def initialize(key, secret)
8
+ @key, @secret = key, secret
9
+
10
+ @http = Net::HTTP.new('rest.nexmo.com', 443)
11
+
12
+ @http.use_ssl = true
13
+ end
14
+
15
+ attr_accessor :key, :secret, :http
16
+
17
+ def send_message(data)
18
+ response = post('/sms/json', data.merge(username: @key, password: @secret))
19
+
20
+ object = JSON.parse(response.body)['messages'].first
21
+
22
+ status = object['status'].to_i
23
+
24
+ if status == 0
25
+ Success.new(object['message-id'])
26
+ else
27
+ Failure.new(Error.new(object['error-text']))
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def post(path, data)
34
+ @http.post(path, urlencode(data), {'Content-Type' => 'application/x-www-form-urlencoded'})
35
+ end
36
+
37
+ def urlencode(data)
38
+ data.map { |k, v| "#{urlescape(k)}=#{urlescape(v)}" }.join('&')
39
+ end
40
+
41
+ def urlescape(value)
42
+ CGI.escape(value.to_s)
43
+ end
44
+ end
45
+
46
+ class Success < Struct.new(:message_id)
47
+ def success?
48
+ true
49
+ end
50
+
51
+ def failure?
52
+ false
53
+ end
54
+ end
55
+
56
+ class Failure < Struct.new(:error)
57
+ def success?
58
+ false
59
+ end
60
+
61
+ def failure?
62
+ true
63
+ end
64
+ end
65
+
66
+ class Error < StandardError
67
+ end
68
+ end
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'nexmo'
3
+ s.version = '0.1.0'
4
+ s.platform = Gem::Platform::RUBY
5
+ s.authors = ['Tim Craft']
6
+ s.email = ['mail@timcraft.com']
7
+ s.homepage = 'http://github.com/timcraft/nexmo'
8
+ s.description = 'A simple wrapper for the Nexmo API'
9
+ s.summary = 'See description'
10
+ s.files = Dir.glob('{lib,test}/**/*') + %w(README.txt nexmo.gemspec)
11
+ s.add_dependency('json', ['~> 1.5.1'])
12
+ s.require_path = 'lib'
13
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nexmo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tim Craft
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-24 00:00:00.000000000 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ requirement: &2853850 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2853850
26
+ description: A simple wrapper for the Nexmo API
27
+ email:
28
+ - mail@timcraft.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/nexmo.rb
34
+ - README.txt
35
+ - nexmo.gemspec
36
+ has_rdoc: true
37
+ homepage: http://github.com/timcraft/nexmo
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.6.2
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: See description
61
+ test_files: []