em-smsified 0.2.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.
- data/LICENSE.txt +20 -0
- data/README.md +66 -0
- data/examples/local_test.rb +40 -0
- data/examples/sending_and_subscribing.rb +68 -0
- data/lib/em-smsified/base.rb +100 -0
- data/lib/em-smsified/helpers.rb +61 -0
- data/lib/em-smsified/incoming_message.rb +35 -0
- data/lib/em-smsified/oneapi.rb +49 -0
- data/lib/em-smsified/reporting.rb +59 -0
- data/lib/em-smsified/response.rb +41 -0
- data/lib/em-smsified/subscriptions.rb +137 -0
- data/lib/em-smsified.rb +15 -0
- data/spec/em-smsified_spec.rb +559 -0
- data/spec/spec_helper.rb +16 -0
- metadata +183 -0
data/lib/em-smsified.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
%w(
|
3
|
+
cgi
|
4
|
+
time
|
5
|
+
eventmachine
|
6
|
+
em-http-request
|
7
|
+
yajl
|
8
|
+
em-smsified/helpers
|
9
|
+
em-smsified/response
|
10
|
+
em-smsified/base
|
11
|
+
em-smsified/subscriptions
|
12
|
+
em-smsified/reporting
|
13
|
+
em-smsified/oneapi
|
14
|
+
em-smsified/incoming_message
|
15
|
+
).each { |lib| require lib }
|
@@ -0,0 +1,559 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
# These tests are all local unit tests
|
4
|
+
WebMock.disable_net_connect!
|
5
|
+
|
6
|
+
def wrap_and_run(obj, message, *args)
|
7
|
+
EventMachine.run_block do
|
8
|
+
obj.send(message, *args) do |response|
|
9
|
+
return response
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
describe "Smsified" do
|
16
|
+
def failed
|
17
|
+
EventMachine.stop
|
18
|
+
fail
|
19
|
+
end
|
20
|
+
|
21
|
+
before(:all) do
|
22
|
+
@username = 'user'
|
23
|
+
@password = 'pass'
|
24
|
+
@address = '14155551212'
|
25
|
+
@sender_address = '13035551212'
|
26
|
+
@request_uri = "https://api.smsified.com/v1/smsmessaging/outbound/#{@sender_address}/requests"
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "Helpers" do
|
30
|
+
before(:all) do
|
31
|
+
class Foo
|
32
|
+
include EventMachine::Smsified::Helpers
|
33
|
+
|
34
|
+
attr_reader :keys, :query_string
|
35
|
+
|
36
|
+
def initialize(hash)
|
37
|
+
@keys = camelcase_keys(hash)
|
38
|
+
@query_string = build_query_string(hash)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
@foo = Foo.new({ :destination_address => 'foo',
|
43
|
+
:notify_url => 'bar',
|
44
|
+
:client_correlator => 'baz',
|
45
|
+
:callback_data => 'donkey' })
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'Should camelcase the appropriate keys' do
|
49
|
+
camelcased_keys = @foo.keys
|
50
|
+
|
51
|
+
camelcased_keys[:destinationAddress].should eql 'foo'
|
52
|
+
camelcased_keys[:notifyURL].should eql 'bar'
|
53
|
+
camelcased_keys[:clientCorrelator].should eql 'baz'
|
54
|
+
camelcased_keys[:callbackData].should eql 'donkey'
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'Should build a proper query string' do
|
58
|
+
@foo.query_string.should eql "destinationAddress=foo¬ifyURL=bar&clientCorrelator=baz&callbackData=donkey"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "OneAPI" do
|
63
|
+
before(:all) do
|
64
|
+
@one_api = EventMachine::Smsified::OneAPI.new :username => @username, :password => @password, :debug => true
|
65
|
+
|
66
|
+
@message_sent = { "resourceReference" => { "resourceURL" => "https://api.smsified.com/v1/smsmessaging/outbound/tel%3A%2B#{@sender_address}/requests/795bd02c8e343b2dfd673b67dd0ee55a" } }
|
67
|
+
end
|
68
|
+
|
69
|
+
before(:each) do
|
70
|
+
#Because WebMock configures a global RSpect WebMock.reset!, we need to set up the stub before each spec instead of on :all
|
71
|
+
stub_request(:post, @request_uri).
|
72
|
+
to_return(:status => 200, :body => @message_sent.to_json, :headers => {})
|
73
|
+
end
|
74
|
+
|
75
|
+
it "Should get errors if instantiating without all of the right parameters" do
|
76
|
+
begin
|
77
|
+
EventMachine::Smsified::OneAPI.new 'foobar'
|
78
|
+
rescue => e
|
79
|
+
e.to_s.should eql 'an options Hash is required'
|
80
|
+
end
|
81
|
+
|
82
|
+
begin
|
83
|
+
EventMachine::Smsified::OneAPI.new(:password => nil)
|
84
|
+
rescue => e
|
85
|
+
e.to_s.should eql ':username required'
|
86
|
+
end
|
87
|
+
|
88
|
+
begin
|
89
|
+
EventMachine::Smsified::OneAPI.new(:username => @username)
|
90
|
+
rescue => e
|
91
|
+
e.to_s.should eql ':password required'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it "Should raise an error if no :sender_address specified" do
|
96
|
+
begin
|
97
|
+
@one_api.send_sms('foobar')
|
98
|
+
rescue => e
|
99
|
+
e.to_s.should eql 'an options Hash is required'
|
100
|
+
end
|
101
|
+
|
102
|
+
begin
|
103
|
+
@one_api.send_sms({})
|
104
|
+
rescue => e
|
105
|
+
e.to_s.should eql ':sender_address is required'
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
it "Should not raise an error if a :sender_address was specified at instantiation" do
|
110
|
+
one_api = EventMachine::Smsified::OneAPI.new :username => @username, :password => @password, :debug => true, :sender_address => @sender_address
|
111
|
+
|
112
|
+
response = wrap_and_run(one_api, :send_sms, :address => @address, :message => 'Hola from RSpec!')
|
113
|
+
|
114
|
+
response.data.should eql @message_sent
|
115
|
+
end
|
116
|
+
|
117
|
+
it "Should raise an error if all required params are not passed when sending an SMS" do
|
118
|
+
begin
|
119
|
+
@one_api.send_sms(:message => 'Hola from RSpec!', :sender_address => @sender_address)
|
120
|
+
rescue => e
|
121
|
+
e.to_s.should eql ':address is required'
|
122
|
+
end
|
123
|
+
|
124
|
+
begin
|
125
|
+
@one_api.send_sms(:address => @address, :sender_address => @sender_address)
|
126
|
+
rescue => e
|
127
|
+
e.to_s.should eql ':message is required'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
it "Should instantiate a OneAPI object" do
|
132
|
+
oneapi = EventMachine::Smsified::OneAPI.new :username => @username, :password => @password, :debug => true
|
133
|
+
oneapi.instance_of?(EventMachine::Smsified::OneAPI).should eql true
|
134
|
+
end
|
135
|
+
|
136
|
+
it "Should send an SMS" do
|
137
|
+
response = wrap_and_run(@one_api, :send_sms, :address => @address, :message => 'Hola from RSpec!', :sender_address => @sender_address)
|
138
|
+
|
139
|
+
response.data.should eql @message_sent
|
140
|
+
|
141
|
+
a_request(:post, @request_uri).
|
142
|
+
with{ |req| req.body == "address=14155551212&message=Hola+from+RSpec%21"}.should have_been_made
|
143
|
+
end
|
144
|
+
|
145
|
+
it "Should send an SMS to multiple destinations" do
|
146
|
+
response = wrap_and_run(@one_api, :send_sms, :address => ['14155551212', '13035551212'],
|
147
|
+
:message => 'Hola from RSpec!',
|
148
|
+
:sender_address => @sender_address)
|
149
|
+
response.data.should eql @message_sent
|
150
|
+
|
151
|
+
a_request(:post, @request_uri).
|
152
|
+
with{ |req| req.body == "address=14155551212&address=13035551212&message=Hola+from+RSpec%21"}.should have_been_made
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe 'Subscriptions' do
|
157
|
+
before(:all) do
|
158
|
+
@subscriptions = EventMachine::Smsified::Subscriptions.new :username => @username, :password => @password, :debug => true
|
159
|
+
end
|
160
|
+
|
161
|
+
it "Should instantiate a Subscriptions object" do
|
162
|
+
smsified = EventMachine::Smsified::Subscriptions.new(:username => @username, :password => @password)
|
163
|
+
smsified.instance_of?(EventMachine::Smsified::Subscriptions).should eql true
|
164
|
+
end
|
165
|
+
|
166
|
+
it "Should get errors if instantiating without all of the right parameters" do
|
167
|
+
begin
|
168
|
+
EventMachine::Smsified::Subscriptions.new 'foobar'
|
169
|
+
rescue => e
|
170
|
+
e.to_s.should eql 'an options Hash is required'
|
171
|
+
end
|
172
|
+
|
173
|
+
begin
|
174
|
+
EventMachine::Smsified::Subscriptions.new({})
|
175
|
+
rescue => e
|
176
|
+
e.to_s.should eql ':username required'
|
177
|
+
end
|
178
|
+
|
179
|
+
begin
|
180
|
+
EventMachine::Smsified::Subscriptions.new(:username => @username)
|
181
|
+
rescue => e
|
182
|
+
e.to_s.should eql ':password required'
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe 'Listing subscriptions' do
|
187
|
+
before(:all) do
|
188
|
+
@no_subscription = { "inboundSubscriptionList" => { "numberOfSubscriptions" => "0" } }
|
189
|
+
|
190
|
+
@inbound_subscriptions = {
|
191
|
+
"inboundSubscriptionList" => {
|
192
|
+
"inboundSubscription" => [
|
193
|
+
{
|
194
|
+
"resourceURL" => "https://api.smsified.com/v1/smsmessaging/inbound/subscriptions/3cf88f9cfd0dae96cbfdf16f18c07411",
|
195
|
+
"subscriptionId" => "3cf88f9cfd0dae96cbfdf16f18c07411",
|
196
|
+
"notificationFormat" => "JSON",
|
197
|
+
"destinationAddress" => "tel:+17177455076",
|
198
|
+
"notifyURL" => "http://98.207.5.162:8080"
|
199
|
+
},
|
200
|
+
{
|
201
|
+
"resourceURL" => "https://api.smsified.com/v1/smsmessaging/inbound/subscriptions/75bb5bef239aed425c2966cbb95f33c9",
|
202
|
+
"subscriptionId" => "75bb5bef239aed425c2966cbb95f33c9",
|
203
|
+
"notificationFormat" => "JSON",
|
204
|
+
"destinationAddress" => "tel:+17177455076",
|
205
|
+
"notifyURL" => "http://98.207.5.162:8080"
|
206
|
+
}
|
207
|
+
],
|
208
|
+
"numberOfSubscriptions" => "2",
|
209
|
+
"resourceURL" => "https://api.smsified.com/v1/smsmessaging/inbound/subscriptions"
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
@outbound_subscriptions = {
|
214
|
+
"outboundSubscriptionList" => {
|
215
|
+
"numberOfSubscriptions" => "2",
|
216
|
+
"resourceURL" => "https://api.smsified.com/v1/smsmessaging/outbound/subscriptions",
|
217
|
+
"outboundSubscription" => [
|
218
|
+
{
|
219
|
+
"senderAddress" => "tel:+17177455076",
|
220
|
+
"resourceURL" => "https://api.smsified.com/v1/smsmessaging/outbound/subscriptions/68faa512b1c81ee0d33a6b97004d1212",
|
221
|
+
"subscriptionId" => "68faa512b1c81ee0d33a6b97004d1212",
|
222
|
+
"notificationFormat" => "JSON",
|
223
|
+
"notifyURL" => "http://98.207.5.162:8080"
|
224
|
+
},
|
225
|
+
{
|
226
|
+
"senderAddress" => "tel:+17177455076",
|
227
|
+
"resourceURL" => "https://api.smsified.com/v1/smsmessaging/outbound/subscriptions/6e64fb72cd2a27b8a9460caccbd4dc53",
|
228
|
+
"subscriptionId" => "6e64fb72cd2a27b8a9460caccbd4dc53",
|
229
|
+
"notificationFormat" => "JSON",
|
230
|
+
"notifyURL" => "http://98.207.5.162:8080"
|
231
|
+
}
|
232
|
+
]
|
233
|
+
}
|
234
|
+
}
|
235
|
+
end
|
236
|
+
before(:each) do
|
237
|
+
stub_request(:get,
|
238
|
+
"https://api.smsified.com/v1/smsmessaging/inbound/subscriptions?destinationAddress=#{@address}").
|
239
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
240
|
+
to_return(:status => ["200", "OK"],
|
241
|
+
:body => @no_subscription.to_json)
|
242
|
+
|
243
|
+
stub_request(:get,
|
244
|
+
"https://api.smsified.com/v1/smsmessaging/inbound/subscriptions?destinationAddress=#{@sender_address}").
|
245
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
246
|
+
to_return(:status => ["200", "OK"],
|
247
|
+
:body => @inbound_subscriptions.to_json)
|
248
|
+
|
249
|
+
stub_request(:get,
|
250
|
+
"https://api.smsified.com/v1/smsmessaging/outbound/subscriptions?senderAddress=#{@sender_address}").
|
251
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
252
|
+
to_return(
|
253
|
+
:status => ["200", "OK"],
|
254
|
+
:body => @outbound_subscriptions.to_json)
|
255
|
+
end
|
256
|
+
|
257
|
+
it "Should let me instantiate a OneAPI object and call subscription methods" do
|
258
|
+
one_api = EventMachine::Smsified::OneAPI.new :username => @username, :password => @password, :debug => true
|
259
|
+
inbound_subscriptions = wrap_and_run(one_api, :inbound_subscriptions, @address)
|
260
|
+
inbound_subscriptions.data.should eql @no_subscription
|
261
|
+
end
|
262
|
+
|
263
|
+
it "Should find no subscriptions" do
|
264
|
+
inbound_subscriptions = wrap_and_run(@subscriptions, :inbound_subscriptions, @address)
|
265
|
+
inbound_subscriptions.data.should eql @no_subscription
|
266
|
+
end
|
267
|
+
|
268
|
+
it "Should get a list of inbound subscriptions" do
|
269
|
+
inbound_subscriptions = wrap_and_run(@subscriptions, :inbound_subscriptions, @sender_address)
|
270
|
+
inbound_subscriptions.http.response_header.status.should eql 200
|
271
|
+
inbound_subscriptions.data.should eql @inbound_subscriptions
|
272
|
+
end
|
273
|
+
|
274
|
+
it "Should get a list of outbound subscriptions" do
|
275
|
+
outbound_subscriptions = wrap_and_run(@subscriptions, :outbound_subscriptions, @sender_address)
|
276
|
+
outbound_subscriptions.http.response_header.status.should eql 200
|
277
|
+
outbound_subscriptions.data.should eql @outbound_subscriptions
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
describe 'Create subscriptions' do
|
282
|
+
before(:all) do
|
283
|
+
@inbound_subscription = { "resourceReference" => { "resourceURL" => "https://api.smsified.com/v1/smsmessaging/inbound/subscriptions/e636368b7fddac0e93e34ae03bad33dd" } }
|
284
|
+
@outbound_subscription = { "resourceReference" => { "resourceURL" => "https://api.smsified.com/v1/smsmessaging/outbound/subscriptions/4bc465cd394c9f5e78802af5ad6bb442" } }
|
285
|
+
@inbound_uri = %r|https://api.smsified.com/v1/smsmessaging/inbound/subscriptions?|
|
286
|
+
@outbound_uri = %r|https://api.smsified.com/v1/smsmessaging/outbound/17177455076/subscriptions?|
|
287
|
+
end
|
288
|
+
|
289
|
+
it 'Should create an inbound subscription' do
|
290
|
+
stub_request(:post, @inbound_uri).
|
291
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
292
|
+
to_return(:status => ["200", "OK"],
|
293
|
+
:body => @inbound_subscription.to_json)
|
294
|
+
|
295
|
+
result = wrap_and_run(@subscriptions, :create_inbound_subscription, '17177455076', :notify_url => 'http://foobar.com')
|
296
|
+
result.http.response_header.status.should eql 200
|
297
|
+
result.data.should eql @inbound_subscription
|
298
|
+
a_request(:post, @inbound_uri).
|
299
|
+
with{ |req| req.body == "destinationAddress=17177455076¬ifyURL=http%3A%2F%2Ffoobar.com"}.should have_been_made
|
300
|
+
end
|
301
|
+
|
302
|
+
it 'Should create an outbound subscription' do
|
303
|
+
stub_request(:post, @outbound_uri).
|
304
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
305
|
+
to_return(:status => ["200", "OK"],
|
306
|
+
:body => @outbound_subscription.to_json)
|
307
|
+
|
308
|
+
result = wrap_and_run(@subscriptions, :create_outbound_subscription, '17177455076', :notify_url => 'http://foobar.com')
|
309
|
+
result.http.response_header.status.should eql 200
|
310
|
+
result.data.should eql @outbound_subscription
|
311
|
+
a_request(:post, @outbound_uri).
|
312
|
+
with{ |req| req.body == "notifyURL=http%3A%2F%2Ffoobar.com"}.should have_been_made
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
describe 'Updated subscriptions' do
|
317
|
+
before(:all) do
|
318
|
+
@inbound_subscription = { "resourceReference" => { "resourceURL" => "https://api.smsified.com/v1/smsmessaging/inbound/subscriptions/e636368b7fddac0e93e34ae03bad33dd" } }
|
319
|
+
@outbound_subscription = { "resourceReference" => { "resourceURL" => "https://api.smsified.com/v1/smsmessaging/outbound/subscriptions/4bc465cd394c9f5e78802af5ad6bb442" } }
|
320
|
+
|
321
|
+
@inbound_uri = %r|https://api.smsified.com/v1/smsmessaging/inbound/subscriptions?|
|
322
|
+
|
323
|
+
@outbound_uri = "https://api.smsified.com/v1/smsmessaging/outbound/#{@sender_address}/subscriptions"
|
324
|
+
end
|
325
|
+
|
326
|
+
it 'Should update an inbound subscription' do
|
327
|
+
stub_request(:post, @inbound_uri).
|
328
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
329
|
+
to_return(:status => ["200", "OK"],
|
330
|
+
:body => @inbound_subscription.to_json)
|
331
|
+
|
332
|
+
result = wrap_and_run(@subscriptions, :update_inbound_subscription, 'c880c96f161f6220d4977b29b4bfc111', :notify_url => 'http://foobar1.com')
|
333
|
+
|
334
|
+
result.http.response_header.status.should eql 200
|
335
|
+
result.data.should eql @inbound_subscription
|
336
|
+
a_request(:post, @inbound_uri).
|
337
|
+
with{ |req| req.body == "notifyURL=http%3A%2F%2Ffoobar1.com"}.should have_been_made
|
338
|
+
end
|
339
|
+
|
340
|
+
it 'Should update an outbound subscription' do
|
341
|
+
stub_request(:post, @outbound_uri).
|
342
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
343
|
+
to_return(:status => ["200", "OK"],
|
344
|
+
:body => @outbound_subscription.to_json)
|
345
|
+
|
346
|
+
result = wrap_and_run(@subscriptions, :update_outbound_subscription, @sender_address, :notify_url => 'http://foobar.com')
|
347
|
+
result.http.response_header.status.should eql 200
|
348
|
+
result.data.should eql @outbound_subscription
|
349
|
+
a_request(:post, @outbound_uri).
|
350
|
+
with{ |req| req.body == "notifyURL=http%3A%2F%2Ffoobar.com" }.should have_been_made
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
describe 'Deleting subscriptions' do
|
355
|
+
before(:each) do
|
356
|
+
stub_request(:delete, %r|https://api.smsified.com/v1/smsmessaging/?|).
|
357
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
358
|
+
to_return(:status => ["204", "OK"])
|
359
|
+
end
|
360
|
+
|
361
|
+
it "Should delete an inbound subscription" do
|
362
|
+
result = wrap_and_run(@subscriptions, :delete_inbound_subscription, '3cf88f9cfd0dae96cbfdf16f18c07411')
|
363
|
+
result.http.response_header.status.should eql 204
|
364
|
+
end
|
365
|
+
|
366
|
+
it "Should delete an outbound subscription" do
|
367
|
+
|
368
|
+
result = wrap_and_run(@subscriptions, :delete_outbound_subscription, '342b61efc3ba9fd2fd992e58903ef050')
|
369
|
+
result.http.response_header.status.should eql 204
|
370
|
+
end
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
describe "Reporting" do
|
375
|
+
before(:all) do
|
376
|
+
@reporting = EventMachine::Smsified::Reporting.new :username => @username, :password => @password, :debug => true
|
377
|
+
|
378
|
+
@delivery_status = {
|
379
|
+
"deliveryInfoList" => {
|
380
|
+
"deliveryInfo" => [
|
381
|
+
{
|
382
|
+
"address" => "tel:+14153675082",
|
383
|
+
"parts" => "1",
|
384
|
+
"senderAddress" => "tel:+17177455076",
|
385
|
+
"messageId" => "74ae6147f915eabf87b35b9ea30c5916",
|
386
|
+
"code" => "0",
|
387
|
+
"createdDateTime" => 'Fri May 13 16:14:50 UTC 2011',
|
388
|
+
"sentDateTime" => 'Fri May 13 16:14:53 UTC 2011',
|
389
|
+
"deliveryStatus" => "DeliveredToNetwork",
|
390
|
+
"message" => "Hola from RSpec!",
|
391
|
+
"direction" => "outbound"
|
392
|
+
}
|
393
|
+
],
|
394
|
+
"resourceURL" => "https://api.smsified.com/v1/smsmessaging/outbound/tel%3A%2B17177455076/requests/795bd02c8e343b2dfd673b67dd0ee55a/deliveryInfos"
|
395
|
+
}
|
396
|
+
}
|
397
|
+
|
398
|
+
@message = { "parts" => 1,
|
399
|
+
"sent" => "2011-05-13T16:14:50.480+0000",
|
400
|
+
"code" => "0",
|
401
|
+
"body" => "Hola from RSpec!",
|
402
|
+
"messageId" => "74ae6147f915eabf87b35b9ea30c5916",
|
403
|
+
"from" => "17177455076",
|
404
|
+
"to" => "14153675082",
|
405
|
+
"direction" => "out",
|
406
|
+
"status" => "success",
|
407
|
+
"created" => "2011-05-13T16:14:50.480+0000"}
|
408
|
+
|
409
|
+
@message_range = [
|
410
|
+
{
|
411
|
+
"parts" => 1,
|
412
|
+
"sent" => "2011-05-13T20:27:56.690+0000",
|
413
|
+
"code" => "-1",
|
414
|
+
"body" => "foobar9446",
|
415
|
+
"messageId" => "d194e91c32de943ae942ad4043b7905b",
|
416
|
+
"from" => "17177455076",
|
417
|
+
"to" => "14155551",
|
418
|
+
"direction" => "out",
|
419
|
+
"status" => "fail",
|
420
|
+
"created" => "2011-05-13T20:27:56.690+0000"
|
421
|
+
},
|
422
|
+
{
|
423
|
+
"parts" => 1,
|
424
|
+
"sent" => "2011-05-13T20:27:53.660+0000",
|
425
|
+
"code" => "-1",
|
426
|
+
"body" => "foobar4374",
|
427
|
+
"messageId" => "4d9237b323618ab164fb6d646882da99",
|
428
|
+
"from" => "17177455076",
|
429
|
+
"to" => "14155551212",
|
430
|
+
"direction" => "out",
|
431
|
+
"status" => "fail",
|
432
|
+
"created" => "2011-05-13T20:27:53.660+0000"
|
433
|
+
}
|
434
|
+
]
|
435
|
+
end
|
436
|
+
before(:each) do
|
437
|
+
stub_request(:get, "https://api.smsified.com/v1/messages/74ae6147f915eabf87b35b9ea30c5916").
|
438
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
439
|
+
to_return(:status => ["200", "OK"],
|
440
|
+
:body => @message.to_json)
|
441
|
+
|
442
|
+
stub_request(:get, "https://api.smsified.com/v1/messages?start=2011-05-12&end=2011-05-12").
|
443
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
444
|
+
to_return(:status => ["200", "OK"],
|
445
|
+
:headers => {"Content-Type" => "application/json"},
|
446
|
+
:body => @message_range.to_json)
|
447
|
+
|
448
|
+
stub_request(:get, "https://api.smsified.com/v1/smsmessaging/outbound/#{@sender_address}/requests/795bd02c8e343b2dfd673b67dd0ee55a/deliveryInfos").
|
449
|
+
with(:headers => {'Authorization' => [@username, @password]}).
|
450
|
+
to_return(:status => ["200", "OK"],
|
451
|
+
:body => @delivery_status.to_json)
|
452
|
+
end
|
453
|
+
|
454
|
+
it "Should instantiate a Reporting object" do
|
455
|
+
reporting = EventMachine::Smsified::Reporting.new :username => 'smsified_tester_smsified', :password => 'bug.fungus52', :debug => true
|
456
|
+
reporting.instance_of?(EventMachine::Smsified::Reporting).should eql true
|
457
|
+
end
|
458
|
+
|
459
|
+
it "Should get errors if instantiating without all of the right parameters" do
|
460
|
+
begin
|
461
|
+
EventMachine::Smsified::Reporting.new 'foobar'
|
462
|
+
rescue => e
|
463
|
+
e.to_s.should eql 'an options Hash is required'
|
464
|
+
end
|
465
|
+
|
466
|
+
begin
|
467
|
+
EventMachine::Smsified::Reporting.new({})
|
468
|
+
rescue => e
|
469
|
+
e.to_s.should eql ':username required'
|
470
|
+
end
|
471
|
+
|
472
|
+
begin
|
473
|
+
EventMachine::Smsified::Reporting.new(:username => @username)
|
474
|
+
rescue => e
|
475
|
+
e.to_s.should eql ':password required'
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
it "should get json by default" do
|
480
|
+
response = wrap_and_run(@reporting, :search_sms, 'start=2011-05-12&end=2011-05-12')
|
481
|
+
response.http.response_header["CONTENT_TYPE"].should eql "application/json"
|
482
|
+
end
|
483
|
+
|
484
|
+
it "Should raise an error if no :sender_address specified" do
|
485
|
+
begin
|
486
|
+
wrap_and_run(@reporting, :delivery_status, 'foobar')
|
487
|
+
rescue => e
|
488
|
+
e.to_s.should eql 'an options Hash is required'
|
489
|
+
end
|
490
|
+
|
491
|
+
begin
|
492
|
+
@reporting.delivery_status({})
|
493
|
+
rescue => e
|
494
|
+
e.to_s.should eql ':sender_address is required'
|
495
|
+
end
|
496
|
+
end
|
497
|
+
|
498
|
+
it "Should not raise an error if a :sender_address was specified at instantiation" do
|
499
|
+
reporting = EventMachine::Smsified::Reporting.new :username => @username, :password => @password, :debug => true, :sender_address => @sender_address
|
500
|
+
delivery_response = wrap_and_run(reporting, :delivery_status, :request_id => '795bd02c8e343b2dfd673b67dd0ee55a')
|
501
|
+
delivery_response.data.should == @delivery_status
|
502
|
+
end
|
503
|
+
|
504
|
+
it "Should retrieve an SMS message" do
|
505
|
+
response = wrap_and_run(@reporting, :retrieve_sms, '74ae6147f915eabf87b35b9ea30c5916')
|
506
|
+
response.data.should eql @message
|
507
|
+
end
|
508
|
+
|
509
|
+
it "Should retrieve SMS messages based on a query string" do
|
510
|
+
response = wrap_and_run(@reporting, :search_sms, 'start=2011-05-12&end=2011-05-12')
|
511
|
+
response.data.should eql @message_range
|
512
|
+
end
|
513
|
+
|
514
|
+
it "Should send an SMS and get the Delivery status" do
|
515
|
+
delivery_response = wrap_and_run(@reporting, :delivery_status, :request_id => '795bd02c8e343b2dfd673b67dd0ee55a', :sender_address => @sender_address)
|
516
|
+
delivery_response.data.should == @delivery_status
|
517
|
+
end
|
518
|
+
|
519
|
+
it "Should let me instantiate a OneAPI object and call reporting methods" do
|
520
|
+
one_api = EventMachine::Smsified::OneAPI.new :username => @username, :password => @password, :debug => true
|
521
|
+
delivery_response = wrap_and_run(one_api, :delivery_status, :request_id => '795bd02c8e343b2dfd673b67dd0ee55a', :sender_address => @sender_address)
|
522
|
+
delivery_response.data.should eql @delivery_status
|
523
|
+
|
524
|
+
sms_message = wrap_and_run(one_api, :retrieve_sms, '74ae6147f915eabf87b35b9ea30c5916')
|
525
|
+
sms_message.data.should eql @message
|
526
|
+
end
|
527
|
+
end
|
528
|
+
|
529
|
+
describe 'IncomingMessage' do
|
530
|
+
it 'Should parse an incoming message from SMSified' do
|
531
|
+
json = '{
|
532
|
+
"inboundSMSMessageNotification": {
|
533
|
+
"inboundSMSMessage": {
|
534
|
+
"dateTime": "2011-05-11T18:05:54.546Z",
|
535
|
+
"destinationAddress": "16575550100",
|
536
|
+
"message": "Inbound test",
|
537
|
+
"messageId": "ef795d3dac56a62fef3ff1852b0c123a",
|
538
|
+
"senderAddress": "14075550100"
|
539
|
+
}
|
540
|
+
}
|
541
|
+
}'
|
542
|
+
|
543
|
+
incoming_message = EventMachine::Smsified::IncomingMessage.new json
|
544
|
+
incoming_message.date_time.should eql Time.parse '2011-05-11T18:05:54.546Z'
|
545
|
+
incoming_message.destination_address.should eql '16575550100'
|
546
|
+
incoming_message.message.should eql 'Inbound test'
|
547
|
+
incoming_message.message_id.should eql 'ef795d3dac56a62fef3ff1852b0c123a'
|
548
|
+
incoming_message.sender_address.should eql '14075550100'
|
549
|
+
end
|
550
|
+
|
551
|
+
it "Should raise an error if JSON not passed" do
|
552
|
+
lambda { EventMachine::Smsified::IncomingMessage.new 'foobar' }.should raise_error(EventMachine::Smsified::IncomingMessage::MessageError)
|
553
|
+
end
|
554
|
+
|
555
|
+
it "Should raise an error if a different type than an IncomingMessage is passed" do
|
556
|
+
lambda { EventMachine::Smsified::IncomingMessage.new "{ 'foo': 'bar'}" }.should raise_error(EventMachine::Smsified::IncomingMessage::MessageError)
|
557
|
+
end
|
558
|
+
end
|
559
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'em-smsified'
|
5
|
+
require 'awesome_print'
|
6
|
+
require 'json'
|
7
|
+
require 'webmock/rspec'
|
8
|
+
|
9
|
+
|
10
|
+
# Requires supporting files with custom matchers and macros, etc,
|
11
|
+
# in ./support/ and its subdirectories.
|
12
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
|
16
|
+
end
|