bobes-textmagic 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -1
- data/README.rdoc +4 -0
- data/Rakefile +0 -2
- data/VERSION.yml +1 -1
- data/lib/api.rb +53 -0
- data/lib/response.rb +8 -0
- data/test/test_api.rb +58 -0
- data/test/test_response.rb +66 -2
- data/textmagic.gemspec +2 -2
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
==
|
1
|
+
== 0.3.3 2009-06-13
|
2
|
+
|
3
|
+
* Added support for send_time parameter for send command.
|
4
|
+
* Added support for check_number command.
|
5
|
+
|
6
|
+
== 0.3.2 2009-06-07
|
2
7
|
|
3
8
|
* Fixed message length validation to count escaped characters twice for non-unicode messages.
|
4
9
|
* Aliased message_status as status and delete_reply as delete.
|
data/README.rdoc
CHANGED
@@ -68,6 +68,10 @@ of parts, specify an optional +max_length+ parameter:
|
|
68
68
|
|
69
69
|
api.send 'Very very long message...', '999314159265', :max_length => 2
|
70
70
|
|
71
|
+
If you want to postpone message delivery, specify a +send_time+ parameter:
|
72
|
+
|
73
|
+
api.send 'Two hours later', '999314159265', :send_time => Time.now.to_i + 7200
|
74
|
+
|
71
75
|
See TextMagic::API.send for more information on +send+ method.
|
72
76
|
|
73
77
|
=== Checking sent message status
|
data/Rakefile
CHANGED
@@ -63,8 +63,6 @@ Rake::RDocTask.new do |rdoc|
|
|
63
63
|
rdoc.rdoc_dir = 'rdoc'
|
64
64
|
rdoc.title = "textmagic #{version}"
|
65
65
|
rdoc.rdoc_files.include('README*')
|
66
|
-
rdoc.rdoc_files.include('LICENSE')
|
67
|
-
rdoc.rdoc_files.include('History*')
|
68
66
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
69
67
|
rdoc.options << '--charset' << 'utf8'
|
70
68
|
rdoc.options << '--fmt' << 'shtml'
|
data/VERSION.yml
CHANGED
data/lib/api.rb
CHANGED
@@ -51,6 +51,10 @@ module TextMagic
|
|
51
51
|
# the text.
|
52
52
|
# * +max_length+: accepted values are +nil+, +1+, +2+ and +3+, defaults to nil.
|
53
53
|
# If not specified, the SMS gateway will apply its own default value.
|
54
|
+
# * +send_time+: allows you to postpone sending of the message. Note that the message
|
55
|
+
# will be sent to the SMS gateway immediately and will wait there until the specified
|
56
|
+
# time. You can either supply a numeric value denoting number of seconds since
|
57
|
+
# 1.1.1970, or a Time object.
|
54
58
|
#
|
55
59
|
# Example usage:
|
56
60
|
#
|
@@ -70,6 +74,10 @@ module TextMagic
|
|
70
74
|
# want to get a hash response, put the phone number in an array:
|
71
75
|
#
|
72
76
|
# api.send('Hi Barney', ['999271828182'])
|
77
|
+
#
|
78
|
+
# Postponed sending:
|
79
|
+
#
|
80
|
+
# api.send('Two hours later', '999314159265', :send_time => Time.now.to_i + 7200)
|
73
81
|
def send(text, *args)
|
74
82
|
raise Error.new(1, 'Message text is empty') if text.nil? || text.blank?
|
75
83
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
@@ -85,6 +93,7 @@ module TextMagic
|
|
85
93
|
single = args.size == 1 && args.first.is_a?(String)
|
86
94
|
phones = args.flatten
|
87
95
|
raise Error.new(9, 'Invalid phone number format') unless API.validate_phones(phones)
|
96
|
+
options[:send_time] = options[:send_time].to_i if options[:send_time]
|
88
97
|
hash = Executor.execute('send', @username, @password, options.merge(:text => text, :phone => phones.join(',')))
|
89
98
|
TextMagic::API::Response.send(hash, single)
|
90
99
|
end
|
@@ -190,5 +199,49 @@ module TextMagic
|
|
190
199
|
true
|
191
200
|
end
|
192
201
|
alias :delete :delete_reply
|
202
|
+
|
203
|
+
# Executes a check_number command by sending a request to the TextMagic's
|
204
|
+
# SMS gateway.
|
205
|
+
#
|
206
|
+
# If called with a single phone number, this method returns an OpenStruct instance
|
207
|
+
# with credit price and country code for the given phone number. If called with
|
208
|
+
# multiple phone numbers, the method returns a hash of such instances with phone
|
209
|
+
# numbers as keys.
|
210
|
+
# In case the request to the SMS gateway is not successful or the server returns
|
211
|
+
# an error response, an Error is raised.
|
212
|
+
#
|
213
|
+
# Example usage:
|
214
|
+
#
|
215
|
+
# check = api.check_number('447624800500')
|
216
|
+
# check.price
|
217
|
+
# # => 0.8
|
218
|
+
# check.country
|
219
|
+
# # => 'GB'
|
220
|
+
#
|
221
|
+
# Example with multiple phone numbers:
|
222
|
+
#
|
223
|
+
# check = api.check_number('447624800500', '61428102137')
|
224
|
+
# check['447624800500'].price
|
225
|
+
# # => 0.8
|
226
|
+
# check['61428102137'].country
|
227
|
+
# # => 'AU'
|
228
|
+
#
|
229
|
+
# Multiple phone number can be supplied as an array or as a list of arguments:
|
230
|
+
#
|
231
|
+
# api.check_number(['447624800500', '61428102137'])
|
232
|
+
# api.check_number('447624800500', '61428102137')
|
233
|
+
#
|
234
|
+
# If you want to check a single phone number but still want to get
|
235
|
+
# a hash response, put the number in an array:
|
236
|
+
#
|
237
|
+
# api.check_number(['447624800500'])
|
238
|
+
def check_number(*phones)
|
239
|
+
single = phones.size == 1 && phones.first.is_a?(String)
|
240
|
+
phones.flatten!
|
241
|
+
raise TextMagic::API::Error.new(4, 'Insufficient parameters') if phones.empty?
|
242
|
+
hash = Executor.execute('check_number', @username, @password, :phone => phones.join(','))
|
243
|
+
TextMagic::API::Response.check_number(hash, single)
|
244
|
+
end
|
245
|
+
alias :check :check_number
|
193
246
|
end
|
194
247
|
end
|
data/lib/response.rb
CHANGED
@@ -61,6 +61,14 @@ module TextMagic
|
|
61
61
|
response.unread = hash['unread']
|
62
62
|
response
|
63
63
|
end
|
64
|
+
|
65
|
+
def self.check_number(hash, single)
|
66
|
+
response = {}
|
67
|
+
hash.each do |phone, check_hash|
|
68
|
+
response[phone] = OpenStruct.new(check_hash)
|
69
|
+
end
|
70
|
+
single ? response.values.first : response
|
71
|
+
end
|
64
72
|
end
|
65
73
|
end
|
66
74
|
end
|
data/test/test_api.rb
CHANGED
@@ -102,6 +102,18 @@ class APITest < Test::Unit::TestCase
|
|
102
102
|
lambda { @api.send(@text, @phone) }.should raise_error(TextMagic::API::Error)
|
103
103
|
end
|
104
104
|
|
105
|
+
should 'support send_time option' do
|
106
|
+
time = Time.now + rand
|
107
|
+
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone, :unicode => 0, :send_time => time.to_i).returns(@response)
|
108
|
+
@api.send(@text, @phone, :send_time => time.to_i)
|
109
|
+
end
|
110
|
+
|
111
|
+
should 'convert send_time to Fixnum' do
|
112
|
+
time = Time.now + rand
|
113
|
+
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone, :unicode => 0, :send_time => time.to_i).returns(@response)
|
114
|
+
@api.send(@text, @phone, :send_time => time)
|
115
|
+
end
|
116
|
+
|
105
117
|
should 'call Response.send method to process the response hash (single phone)' do
|
106
118
|
processed_response = rand
|
107
119
|
TextMagic::API::Response.expects(:send).with(@response, true).returns(processed_response)
|
@@ -228,4 +240,50 @@ class APITest < Test::Unit::TestCase
|
|
228
240
|
@api.delete_reply(random_string).should == true
|
229
241
|
end
|
230
242
|
end
|
243
|
+
|
244
|
+
context 'Check number command' do
|
245
|
+
|
246
|
+
setup do
|
247
|
+
@username, @password = random_string, random_string
|
248
|
+
@api = TextMagic::API.new(@username, @password)
|
249
|
+
@response = random_string
|
250
|
+
@processed_response = random_string
|
251
|
+
TextMagic::API::Executor.stubs(:execute).returns(@response)
|
252
|
+
TextMagic::API::Response.stubs(:check_number).returns(@processed_response)
|
253
|
+
end
|
254
|
+
|
255
|
+
should 'call Executor execute with correct arguments' do
|
256
|
+
phone = random_phone
|
257
|
+
TextMagic::API::Executor.expects(:execute).with('check_number', @username, @password, :phone => phone)
|
258
|
+
@api.check_number(phone)
|
259
|
+
end
|
260
|
+
|
261
|
+
should 'join phones supplied as array' do
|
262
|
+
phones = Array.new(3) { random_phone }
|
263
|
+
TextMagic::API::Executor.expects(:execute).with('check_number', @username, @password, :phone => phones.join(','))
|
264
|
+
@api.check_number(phones)
|
265
|
+
end
|
266
|
+
|
267
|
+
should 'join phones supplied as arguments' do
|
268
|
+
phones = Array.new(3) { random_phone }
|
269
|
+
TextMagic::API::Executor.expects(:execute).with('check_number', @username, @password, :phone => phones.join(','))
|
270
|
+
@api.check_number(*phones)
|
271
|
+
end
|
272
|
+
|
273
|
+
should 'not call execute and should raise an exception if no phones are specified' do
|
274
|
+
TextMagic::API::Executor.expects(:execute).never
|
275
|
+
lambda { @api.check_number }.should raise_error(TextMagic::API::Error)
|
276
|
+
end
|
277
|
+
|
278
|
+
should 'call Response.check_number method to process the response hash (single phone)' do
|
279
|
+
TextMagic::API::Response.expects(:check_number).with(@response, true).returns(@processed_response)
|
280
|
+
@api.check_number(random_string).should == @processed_response
|
281
|
+
end
|
282
|
+
|
283
|
+
should 'call Response.check_number method to process the response hash (mulitple phones)' do
|
284
|
+
TextMagic::API::Response.expects(:check_number).with(@response, false).returns(@processed_response).twice
|
285
|
+
@api.check_number([random_string]).should == @processed_response
|
286
|
+
@api.check_number(random_string, random_string).should == @processed_response
|
287
|
+
end
|
288
|
+
end
|
231
289
|
end
|
data/test/test_response.rb
CHANGED
@@ -121,7 +121,7 @@ class ResponseTest < Test::Unit::TestCase
|
|
121
121
|
@response.credits_cost.should be_close(@credits_cost, 1e-10)
|
122
122
|
end
|
123
123
|
end
|
124
|
-
|
124
|
+
|
125
125
|
context 'Response to message_status command with multiple ids' do
|
126
126
|
|
127
127
|
setup do
|
@@ -209,7 +209,7 @@ class ResponseTest < Test::Unit::TestCase
|
|
209
209
|
@response.first.timestamp.should == Time.at(@timestamp)
|
210
210
|
end
|
211
211
|
|
212
|
-
should 'have from for
|
212
|
+
should 'have from for all messages' do
|
213
213
|
@response.first.from.should == @phone
|
214
214
|
end
|
215
215
|
|
@@ -221,4 +221,68 @@ class ResponseTest < Test::Unit::TestCase
|
|
221
221
|
@response.first.message_id.should == @message_id
|
222
222
|
end
|
223
223
|
end
|
224
|
+
|
225
|
+
context 'Response to check_number command with single phone' do
|
226
|
+
|
227
|
+
setup do
|
228
|
+
@phone = random_phone
|
229
|
+
@price = rand
|
230
|
+
@country = random_string
|
231
|
+
@hash = {
|
232
|
+
@phone => {
|
233
|
+
'price' => @price,
|
234
|
+
'country' => @country
|
235
|
+
}
|
236
|
+
}
|
237
|
+
@response = TextMagic::API::Response.check_number(@hash, true)
|
238
|
+
end
|
239
|
+
|
240
|
+
should 'be an OpenStruct instance' do
|
241
|
+
@response.class.should == OpenStruct
|
242
|
+
end
|
243
|
+
|
244
|
+
should 'have price' do
|
245
|
+
@response.price.should be_close(@price, 1e-10)
|
246
|
+
end
|
247
|
+
|
248
|
+
should 'have country' do
|
249
|
+
@response.country.should == @country
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
context 'Response to check_number command with multiple phones' do
|
254
|
+
|
255
|
+
setup do
|
256
|
+
@phone = random_phone
|
257
|
+
@price = rand
|
258
|
+
@country = random_string
|
259
|
+
@hash = {
|
260
|
+
@phone => {
|
261
|
+
'price' => @price,
|
262
|
+
'country' => @country
|
263
|
+
}
|
264
|
+
}
|
265
|
+
@response = TextMagic::API::Response.check_number(@hash, false)
|
266
|
+
end
|
267
|
+
|
268
|
+
should 'be a hash' do
|
269
|
+
@response.class.should == Hash
|
270
|
+
end
|
271
|
+
|
272
|
+
should 'have phones as keys' do
|
273
|
+
@response.keys.should == [@phone]
|
274
|
+
end
|
275
|
+
|
276
|
+
should 'contain OpenStruct instances' do
|
277
|
+
@response.values.first.class.should == OpenStruct
|
278
|
+
end
|
279
|
+
|
280
|
+
should 'have price for all phones' do
|
281
|
+
@response.values.first.price.should be_close(@price, 1e-10)
|
282
|
+
end
|
283
|
+
|
284
|
+
should 'have country for all phones' do
|
285
|
+
@response.values.first.country.should == @country
|
286
|
+
end
|
287
|
+
end
|
224
288
|
end
|
data/textmagic.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{textmagic}
|
5
|
-
s.version = "0.3.
|
5
|
+
s.version = "0.3.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Vladim\303\255r Bobe\305\241 Tu\305\276insk\303\275"]
|
9
|
-
s.date = %q{2009-06-
|
9
|
+
s.date = %q{2009-06-13}
|
10
10
|
s.default_executable = %q{tm}
|
11
11
|
s.description = %q{
|
12
12
|
textmagic is a Ruby interface to the TextMagic's Bulk SMS Gateway.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bobes-textmagic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Vladim\xC3\xADr Bobe\xC5\xA1 Tu\xC5\xBEinsk\xC3\xBD"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-13 00:00:00 -07:00
|
13
13
|
default_executable: tm
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|