nexmo 0.1.1 → 0.2.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/lib/nexmo.rb +7 -13
  2. data/nexmo.gemspec +3 -2
  3. data/spec/nexmo_spec.rb +34 -0
  4. metadata +17 -7
@@ -1,22 +1,24 @@
1
1
  require 'net/http'
2
2
  require 'net/https'
3
3
  require 'json'
4
- require 'cgi'
4
+ require 'uri'
5
5
 
6
6
  module Nexmo
7
7
  class Client
8
8
  def initialize(key, secret)
9
9
  @key, @secret = key, secret
10
10
 
11
+ @headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
12
+
11
13
  @http = Net::HTTP.new('rest.nexmo.com', 443)
12
14
 
13
15
  @http.use_ssl = true
14
16
  end
15
17
 
16
- attr_accessor :key, :secret, :http
18
+ attr_accessor :key, :secret, :http, :headers
17
19
 
18
20
  def send_message(data)
19
- response = post('/sms/json', data.merge(:username => @key, :password => @secret))
21
+ response = @http.post('/sms/json', encode(data), headers)
20
22
 
21
23
  object = JSON.parse(response.body)['messages'].first
22
24
 
@@ -31,16 +33,8 @@ module Nexmo
31
33
 
32
34
  private
33
35
 
34
- def post(path, data)
35
- @http.post(path, urlencode(data), {'Content-Type' => 'application/x-www-form-urlencoded'})
36
- end
37
-
38
- def urlencode(data)
39
- data.map { |k, v| "#{urlescape(k)}=#{urlescape(v)}" }.join('&')
40
- end
41
-
42
- def urlescape(value)
43
- CGI.escape(value.to_s)
36
+ def encode(data)
37
+ URI.encode_www_form data.merge(:username => @key, :password => @secret)
44
38
  end
45
39
  end
46
40
 
@@ -1,13 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'nexmo'
3
- s.version = '0.1.1'
3
+ s.version = '0.2.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,test}/**/*') + %w(README.txt nexmo.gemspec)
10
+ s.files = Dir.glob('{lib,spec}/**/*') + %w(README.txt nexmo.gemspec)
11
11
  s.add_dependency('json', ['~> 1.5.1'])
12
+ s.add_development_dependency('mocha')
12
13
  s.require_path = 'lib'
13
14
  end
@@ -0,0 +1,34 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha'
3
+
4
+ require_relative '../lib/nexmo'
5
+
6
+ describe Nexmo::Client do
7
+ before do
8
+ @client = Nexmo::Client.new('key', 'secret')
9
+ end
10
+
11
+ it 'uses ssl by default' do
12
+ assert @client.http.use_ssl?
13
+ end
14
+
15
+ it 'allows access to headers' do
16
+ @client.headers.must_be_kind_of(Hash)
17
+ end
18
+
19
+ describe '#send_message' do
20
+ it 'makes the correct http call' do
21
+ http_response = stub(:body => '{"messages":[{"status":0,"message-id":"id"}]}')
22
+
23
+ data = 'from=ruby&to=number&text=Hey%21&username=key&password=secret'
24
+
25
+ headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
26
+
27
+ @client.http.expects(:post).with('/sms/json', data, headers).returns(http_response)
28
+
29
+ response = @client.send_message({from: 'ruby', to: 'number', text: 'Hey!'})
30
+
31
+ assert response.success?
32
+ end
33
+ end
34
+ 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.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-30 00:00:00.000000000 +01:00
13
- default_executable:
12
+ date: 2011-11-16 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: json
17
- requirement: &1011080 !ruby/object:Gem::Requirement
16
+ requirement: &2946070 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ~>
@@ -22,7 +21,18 @@ dependencies:
22
21
  version: 1.5.1
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *1011080
24
+ version_requirements: *2946070
25
+ - !ruby/object:Gem::Dependency
26
+ name: mocha
27
+ requirement: &2945010 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2945010
26
36
  description: A simple wrapper for the Nexmo API
27
37
  email:
28
38
  - mail@timcraft.com
@@ -31,9 +41,9 @@ extensions: []
31
41
  extra_rdoc_files: []
32
42
  files:
33
43
  - lib/nexmo.rb
44
+ - spec/nexmo_spec.rb
34
45
  - README.txt
35
46
  - nexmo.gemspec
36
- has_rdoc: true
37
47
  homepage: http://github.com/timcraft/nexmo
38
48
  licenses: []
39
49
  post_install_message:
@@ -54,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
64
  version: '0'
55
65
  requirements: []
56
66
  rubyforge_project:
57
- rubygems_version: 1.6.2
67
+ rubygems_version: 1.8.10
58
68
  signing_key:
59
69
  specification_version: 3
60
70
  summary: See description