messagebird 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ require 'helper'
2
+
3
+ describe MessageBird::HTTP::Response do
4
+
5
+ let(:mock_response){ OpenStruct.new(:body => 'repondre') }
6
+ let(:klass){ MessageBird::HTTP::Response }
7
+
8
+ describe '#initialize' do
9
+
10
+ it 'decodes the given response' do
11
+ mock(MessageBird::HTTP::ResponseCode).decode('repondre')
12
+ klass.new(mock_response)
13
+ end
14
+
15
+ it 'stores the retrieved response code in a variable' do
16
+ stub(MessageBird::HTTP::ResponseCode).decode{'test'}
17
+ obj = klass.new(mock_response)
18
+ obj.response_code.must_equal 'test'
19
+ end
20
+
21
+ describe 'invalid input' do
22
+ let(:bad_response){ OpenStruct.new }
23
+
24
+ it 'raises an ArgumentError if only argument does not respond to :body method' do
25
+ assert_raises(ArgumentError){ klass.new(bad_response) }
26
+ end
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,97 @@
1
+ require 'helper'
2
+
3
+ describe MessageBird::HTTP::Sender do
4
+
5
+ let(:uri){ OpenStruct.new(:host => :foo, :port => :bar) }
6
+ let(:mock_connection){ OpenStruct.new(:use_ssl => nil) }
7
+ let(:sms){ OpenStruct.new(:uri => uri)}
8
+ let(:response_factory){ lambda{|foo|'FooBar'} }
9
+
10
+
11
+ subject{ MessageBird::HTTP::Sender }
12
+
13
+ before do
14
+ subject.response_factory = response_factory
15
+ stub(mock_connection).request{:bar}
16
+ end
17
+
18
+ describe '#deliver' do
19
+ it 'sends the given sms' do
20
+ stub(subject).ensure_valid_sms!
21
+ mock(subject).send_sms(:foo)
22
+ subject.deliver(:foo)
23
+ end
24
+
25
+ it 'takes an optional block' do
26
+ stub(subject).ensure_valid_sms!
27
+ stub(subject).create_connection.with_any_args
28
+ stub(subject).create_request.with_any_args
29
+ stub(subject).send_request.with_any_args{'FooBat'}
30
+ subject.send(:send_sms, sms) do |response|
31
+ response.must_equal 'FooBat'
32
+ end
33
+ end
34
+
35
+ it 'raises an ArgumentError when given object is not a valid SMS' do
36
+ assert_raises(ArgumentError){
37
+ subject.deliver('string')
38
+ }
39
+ end
40
+ end
41
+
42
+ describe '#create_connection' do
43
+ it 'creates a new connection' do
44
+ mock(Net::HTTP).new.with_any_args{mock_connection}
45
+ subject.send :create_connection, uri
46
+ end
47
+
48
+ it 'sets the connection to SSL' do
49
+ mock(mock_connection).use_ssl=(true)
50
+ stub(Net::HTTP).new.with_any_args{mock_connection}
51
+ subject.send :create_connection, uri
52
+ end
53
+ end
54
+
55
+ describe '#create_request' do
56
+ it 'works' do
57
+ mock(Net::HTTP::Get).new(:bar).once
58
+ subject.send :create_request, :bar
59
+ end
60
+ end
61
+
62
+ describe '#response_factory' do
63
+ it 'works' do
64
+ assert subject.send(:response_factory).is_a?(Proc)
65
+ end
66
+ end
67
+
68
+ describe '#send_request' do
69
+ it 'calls the response_factory' do
70
+ mock(subject.send(:response_factory)).call(:bar).once
71
+ subject.send :send_request, mock_connection, 'bar'
72
+ end
73
+ end
74
+
75
+ describe '#send_sms' do
76
+ it 'creates a connection' do
77
+ mock(subject).create_connection.with_any_args
78
+ stub(subject).create_request.with_any_args{:foo}
79
+ stub(subject).send_request.with_any_args{:bar}
80
+ subject.send :send_sms, sms
81
+ end
82
+
83
+ it 'creates a request' do
84
+ stub(subject).create_connection.with_any_args
85
+ mock(subject).create_request.with_any_args{:foo}
86
+ stub(subject).send_request.with_any_args{:bar}
87
+ subject.send :send_sms, sms
88
+ end
89
+
90
+ it 'creates sends the request over the connection' do
91
+ stub(subject).create_connection.with_any_args{:foo}
92
+ stub(subject).create_request.with_any_args{:bar}
93
+ mock(subject).send_request(:foo, :bar){:bat}
94
+ subject.send :send_sms, sms
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,167 @@
1
+ require 'helper'
2
+
3
+ module MessageBirdHTTPSMSHelper
4
+ def format_recipients(input)
5
+ subject.send :format_recipients, input
6
+ end
7
+ end
8
+
9
+ describe MessageBird::HTTP::SMS do
10
+ include MessageBirdHTTPSMSHelper
11
+
12
+ subject{ MessageBird::HTTP::SMS.new('sender',3154447100,'message',{}) }
13
+
14
+ describe '#api_url' do
15
+ it 'retrieves the api_url from the Config object' do
16
+ mock(MessageBird::Config).api_url{'test'}
17
+ subject.api_url
18
+ end
19
+
20
+ it 'caches the result' do
21
+ mock(MessageBird::Config).api_url.once{'test'}
22
+ subject.api_url
23
+ subject.api_url
24
+ end
25
+ end
26
+
27
+ describe '#deliver' do
28
+ it 'commands the Sender to deliver itself' do
29
+ mock(MessageBird::HTTP::Sender).deliver(subject)
30
+ subject.deliver
31
+ end
32
+ end
33
+
34
+ describe '#format_recipients' do
35
+ it 'converts an integer to string' do
36
+ format_recipients(31541471696).must_equal "31541471696"
37
+ end
38
+
39
+ it 'converts an enumerable into a comma-separated string' do
40
+ numbers = [3154147100, "315472000"]
41
+ format_recipients(numbers).must_equal "3154147100,315472000"
42
+ end
43
+ end
44
+
45
+ describe '#password' do
46
+ it 'retrieves the password from the Config object' do
47
+ mock(MessageBird::Config).password{'test'}
48
+ subject.password
49
+ end
50
+
51
+ it 'escapes the result' do
52
+ stub(MessageBird::Config).password{'test'}
53
+ mock(subject).escape('test')
54
+ subject.password
55
+ end
56
+
57
+ it 'caches the result' do
58
+ mock(MessageBird::Config).password.once{'test'}
59
+ subject.password
60
+ subject.password
61
+ end
62
+ end
63
+
64
+ describe '#password=' do
65
+ it 'escapes the input and saves it' do
66
+ mock(subject).escape('test')
67
+ subject.send 'password='.to_sym, 'test'
68
+ end
69
+
70
+ it 'saves the result in a variable' do
71
+ stub(subject).escape('test'){'tost'}
72
+ subject.send 'password='.to_sym, 'test'
73
+ subject.password.must_equal 'tost'
74
+ end
75
+ end
76
+
77
+ describe '#request_uri' do
78
+ it 'delegates the call to uri' do
79
+ mock(subject.uri).request_uri.once
80
+ subject.request_uri
81
+ end
82
+ end
83
+
84
+ describe '#set_optional_variables' do
85
+ let(:options){ {:test=>'blaat', :tast=>'bloat'} }
86
+
87
+ it 'tries to set the appropriate instance variables' do
88
+ assert_raises(NoMethodError){ subject.send :set_optional_variables, options}
89
+ end
90
+ end
91
+
92
+ describe '#test_mode' do
93
+ it 'fetches the test_mode from the Config object' do
94
+ mock(MessageBird::Config).fetch(:test_mode, true)
95
+ subject.test_mode
96
+ end
97
+
98
+ it 'converts to integer boolean values for false' do
99
+ mock(MessageBird::Config).fetch(:test_mode, true){false}
100
+ subject.test_mode.must_equal 0
101
+ end
102
+
103
+ it 'converts to integer boolean values for true' do
104
+ mock(MessageBird::Config).fetch(:test_mode, true){true}
105
+ subject.test_mode.must_equal 1
106
+ end
107
+
108
+ it 'returns a true value if the config option is unset or nil' do
109
+ # No config options set
110
+ subject.test_mode.must_equal 1
111
+ end
112
+ end
113
+
114
+ describe '#test_mode=' do
115
+ it 'sets the corresponding variable' do
116
+ subject.send 'test_mode=', false
117
+ subject.test_mode.must_equal 0
118
+
119
+ subject.send 'test_mode=', true
120
+ subject.test_mode.must_equal 1
121
+ end
122
+
123
+ it 'raises an ArgumentError when parameter is not a boolean' do
124
+ assert_raises(ArgumentError){ subject.send('test_mode='.to_sym, 'test') }
125
+ end
126
+ end
127
+
128
+ describe '#uri' do
129
+ it 'parses its url into a uri' do
130
+ mock(URI).parse(subject.url)
131
+ subject.uri
132
+ end
133
+
134
+ it 'caches the result in a variable' do
135
+ stub(URI).parse{'testval'}
136
+ subject.uri
137
+ mock(URI).parse.never
138
+ subject.uri
139
+ end
140
+ end
141
+
142
+ describe '#url' do
143
+ it 'returns a string with http in it' do
144
+ assert(subject.url.include?('http'), "Not a valid URL: #{subject.url}")
145
+ end
146
+ end
147
+
148
+ describe '#username' do
149
+ it 'retrieves the username from the Config object' do
150
+ mock(MessageBird::Config).username{'test'}
151
+ subject.username
152
+ end
153
+
154
+ it 'escapes the result' do
155
+ stub(MessageBird::Config).username{'test'}
156
+ mock(subject).escape('test')
157
+ subject.username
158
+ end
159
+
160
+ it 'caches the result' do
161
+ mock(MessageBird::Config).username.once{'test'}
162
+ subject.username
163
+ subject.username
164
+ end
165
+ end
166
+
167
+ end
@@ -0,0 +1,47 @@
1
+ require 'helper'
2
+
3
+ describe MessageBird::SMS do
4
+ subject{ MessageBird::SMS }
5
+
6
+ describe '#deliver' do
7
+ let(:sms){ Object.new }
8
+
9
+ it 'creates a new SMS object' do
10
+ stub(sms).deliver.with_any_args
11
+ mock(subject).new.with_any_args{sms}
12
+ subject.deliver(1,2,'msg')
13
+ end
14
+
15
+ it 'commands the SMS object to deliver itself' do
16
+ mock(sms).deliver.with_any_args
17
+ stub(subject).new.with_any_args{sms}
18
+ sms.deliver(1,2,'msg')
19
+ end
20
+ end
21
+
22
+ describe '#new' do
23
+ it 'checks which module to use for sending' do
24
+ mock(subject).klass_for(:https).once{ stub(Object.new).new }
25
+ subject.new(:one,:two,'message',{:module => :https})
26
+ end
27
+
28
+ it 'returns a new SMS object of the appropriate module' do
29
+ subject.new(1,2,'message',{:module => :http}).class.must_equal MessageBird::HTTP::SMS
30
+ end
31
+ end
32
+
33
+ describe '#klass_for' do
34
+ it 'takes a string' do
35
+ subject.send(:klass_for, 'http')
36
+ end
37
+
38
+ it 'takes a symbol' do
39
+ subject.send(:klass_for, :http)
40
+ end
41
+
42
+ it 'returns the appropriate SMS class for the given module' do
43
+ subject.send(:klass_for, 'http').must_equal MessageBird::HTTP::SMS
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,29 @@
1
+ require 'helper'
2
+
3
+ describe MessageBird do
4
+
5
+ subject{ MessageBird }
6
+ let(:config){ MessageBird::Config }
7
+
8
+ describe '#configure' do
9
+ it 'it delegates configuration method calls to MessageBird::Config' do
10
+ mock(config).configure.with_any_args.once
11
+ MessageBird.configure do; end
12
+ end
13
+
14
+ it 'supports a block' do
15
+ config.configure do
16
+ api_url 'test'
17
+ end
18
+ config.api_url.must_equal 'test'
19
+ end
20
+ end
21
+
22
+ describe '#send_text_message' do
23
+ it 'command the SMS object to deliver a message' do
24
+ mock(MessageBird::SMS).deliver(1,2,'testmsg', {:test => :value})
25
+ subject.deliver(1,2,'testmsg', {:test => :value})
26
+ end
27
+ end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: messagebird
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Bram de Vries
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jeweler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Implementation of the MessageBird text (sms) service API
42
+ email: bram.devries@nedap.com
43
+ executables:
44
+ - messagebird_test_connection
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - README.md
48
+ files:
49
+ - .travis.yml
50
+ - Gemfile
51
+ - LICENCE
52
+ - README.md
53
+ - Rakefile
54
+ - VERSION
55
+ - bin/messagebird_test_connection
56
+ - lib/messagebird.rb
57
+ - lib/messagebird/config.rb
58
+ - lib/messagebird/deliverable.rb
59
+ - lib/messagebird/helpers.rb
60
+ - lib/messagebird/http/response.rb
61
+ - lib/messagebird/http/response_code.rb
62
+ - lib/messagebird/http/sender.rb
63
+ - lib/messagebird/http/sms.rb
64
+ - lib/messagebird/sms.rb
65
+ - messagebird.gemspec
66
+ - test/helper.rb
67
+ - test/messagebird/config_test.rb
68
+ - test/messagebird/deliverable_test.rb
69
+ - test/messagebird/helpers_test.rb
70
+ - test/messagebird/http/response_code_test.rb
71
+ - test/messagebird/http/response_test.rb
72
+ - test/messagebird/http/sender_test.rb
73
+ - test/messagebird/http/sms_test.rb
74
+ - test/messagebird/sms_test.rb
75
+ - test/messagebird_test.rb
76
+ homepage: http://github.com/nedap/messagebird-sms-api-ruby
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.1.11
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: MessageBird API for Ruby
100
+ test_files: []