textmagic 0.3.3 → 0.4.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/.gitignore +1 -0
- data/README.rdoc +2 -2
- data/Rakefile +22 -21
- data/VERSION.yml +3 -2
- data/lib/api.rb +52 -52
- data/lib/charset.rb +3 -1
- data/lib/error.rb +2 -2
- data/lib/executor.rb +6 -6
- data/lib/response.rb +20 -20
- data/lib/textmagic.rb +3 -3
- data/test/test_api.rb +69 -67
- data/test/test_charset.rb +16 -15
- data/test/test_error.rb +5 -5
- data/test/test_executor.rb +17 -18
- data/test/test_helper.rb +8 -12
- data/test/test_response.rb +75 -75
- data/test/test_validation.rb +26 -27
- data/textmagic.gemspec +24 -17
- metadata +77 -25
data/test/test_helper.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
1
|
+
require "rubygems"
|
2
|
+
require "test/unit"
|
3
|
+
require "shoulda"
|
4
|
+
require "mocha"
|
5
|
+
require "matchy"
|
6
|
+
require "fakeweb"
|
7
7
|
|
8
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),
|
8
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
9
9
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
10
|
|
11
|
-
require
|
11
|
+
require "textmagic"
|
12
12
|
|
13
13
|
class Test::Unit::TestCase
|
14
14
|
end
|
@@ -26,7 +26,3 @@ def random_hash
|
|
26
26
|
3.times { hash[random_string] = random_string }
|
27
27
|
hash
|
28
28
|
end
|
29
|
-
|
30
|
-
def load_response(filename)
|
31
|
-
File.read(File.join(File.dirname(__FILE__), 'fixtures', filename) + '.json')
|
32
|
-
end
|
data/test/test_response.rb
CHANGED
@@ -1,81 +1,81 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class ResponseTest < Test::Unit::TestCase
|
4
4
|
|
5
|
-
context
|
5
|
+
context "Response to account command" do
|
6
6
|
|
7
7
|
setup do
|
8
8
|
@balance = 0.1 * rand(1e4)
|
9
|
-
@hash = {
|
9
|
+
@hash = { "balance" => @balance.to_s }
|
10
10
|
@response = TextMagic::API::Response.account(@hash)
|
11
11
|
end
|
12
12
|
|
13
|
-
should
|
13
|
+
should "be an OpenStruct instance" do
|
14
14
|
@response.class.should == OpenStruct
|
15
15
|
end
|
16
16
|
|
17
|
-
should
|
17
|
+
should "have balance" do
|
18
18
|
@response.balance.should be_close(@balance, 1e-10)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
context
|
22
|
+
context "Response to send command with single phone number" do
|
23
23
|
|
24
24
|
setup do
|
25
25
|
@message_id, @phone = random_string, random_phone
|
26
26
|
@text = random_string
|
27
27
|
@parts_count = 1 + rand(3)
|
28
|
-
@hash = {
|
28
|
+
@hash = { "message_id" => { @message_id => @phone }, "sent_text" => @text, "parts_count" => @parts_count }
|
29
29
|
@response = TextMagic::API::Response.send(@hash, true)
|
30
30
|
end
|
31
31
|
|
32
|
-
should
|
32
|
+
should "equal to the message_id" do
|
33
33
|
@response.should == @message_id
|
34
34
|
end
|
35
35
|
|
36
|
-
should
|
36
|
+
should "have sent_text" do
|
37
37
|
@response.sent_text.should == @text
|
38
38
|
end
|
39
39
|
|
40
|
-
should
|
40
|
+
should "have parts_count" do
|
41
41
|
@response.parts_count.should == @parts_count
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
context
|
45
|
+
context "Response to send command with multiple phone numbers" do
|
46
46
|
|
47
47
|
setup do
|
48
48
|
@message_id1, @phone1 = random_string, random_phone
|
49
49
|
@message_id2, @phone2 = random_string, random_phone
|
50
50
|
@text = random_string
|
51
51
|
@parts_count = 1 + rand(3)
|
52
|
-
@hash = {
|
52
|
+
@hash = { "message_id" => { @message_id1 => @phone1, @message_id2 => @phone2 }, "sent_text" => @text, "parts_count" => @parts_count }
|
53
53
|
@response = TextMagic::API::Response.send(@hash, false)
|
54
54
|
end
|
55
55
|
|
56
|
-
should
|
56
|
+
should "be a hash" do
|
57
57
|
@response.class.should == Hash
|
58
58
|
end
|
59
59
|
|
60
|
-
should
|
60
|
+
should "have phone numbers as keys" do
|
61
61
|
@response.keys.sort.should == [@phone1, @phone2].sort
|
62
62
|
end
|
63
63
|
|
64
|
-
should
|
64
|
+
should "have message ids as values" do
|
65
65
|
@response[@phone1].should == @message_id1
|
66
66
|
@response[@phone2].should == @message_id2
|
67
67
|
end
|
68
68
|
|
69
|
-
should
|
69
|
+
should "have sent_text" do
|
70
70
|
@response.sent_text.should == @text
|
71
71
|
end
|
72
72
|
|
73
|
-
should
|
73
|
+
should "have parts_count" do
|
74
74
|
@response.parts_count.should == @parts_count
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
context
|
78
|
+
context "Response to message_status command with single id" do
|
79
79
|
|
80
80
|
setup do
|
81
81
|
@text = random_string
|
@@ -85,44 +85,44 @@ class ResponseTest < Test::Unit::TestCase
|
|
85
85
|
@completed_time = (Time.now - 20).to_i
|
86
86
|
@credits_cost = 0.01 * rand(300)
|
87
87
|
@hash = {
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
88
|
+
"141421" => {
|
89
|
+
"text" => @text,
|
90
|
+
"status" => @status,
|
91
|
+
"created_time" => @created_time.to_s,
|
92
|
+
"reply_number" => @reply_number,
|
93
|
+
"completed_time" => @completed_time.to_s,
|
94
|
+
"credits_cost" => @credits_cost
|
95
95
|
}
|
96
96
|
}
|
97
97
|
@response = TextMagic::API::Response.message_status(@hash, true)
|
98
98
|
end
|
99
99
|
|
100
|
-
should
|
100
|
+
should "equal to the message status" do
|
101
101
|
@response.should == @status
|
102
102
|
end
|
103
103
|
|
104
|
-
should
|
104
|
+
should "have text" do
|
105
105
|
@response.text.should == @text
|
106
106
|
end
|
107
107
|
|
108
|
-
should
|
108
|
+
should "have created_time" do
|
109
109
|
@response.created_time.should == Time.at(@created_time)
|
110
110
|
end
|
111
111
|
|
112
|
-
should
|
112
|
+
should "have completed_time" do
|
113
113
|
@response.completed_time.should == Time.at(@completed_time)
|
114
114
|
end
|
115
115
|
|
116
|
-
should
|
116
|
+
should "have reply_number" do
|
117
117
|
@response.reply_number.should == @reply_number
|
118
118
|
end
|
119
119
|
|
120
|
-
should
|
120
|
+
should "have credits_cost" do
|
121
121
|
@response.credits_cost.should be_close(@credits_cost, 1e-10)
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
125
|
-
context
|
125
|
+
context "Response to message_status command with multiple ids" do
|
126
126
|
|
127
127
|
setup do
|
128
128
|
@text = random_string
|
@@ -132,97 +132,97 @@ class ResponseTest < Test::Unit::TestCase
|
|
132
132
|
@completed_time = (Time.now - 20).to_i
|
133
133
|
@credits_cost = 0.01 * rand(300)
|
134
134
|
@hash = {
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
135
|
+
"141421" => {
|
136
|
+
"text" => @text,
|
137
|
+
"status" => @status,
|
138
|
+
"created_time" => @created_time,
|
139
|
+
"reply_number" => @reply_number,
|
140
|
+
"completed_time" => @completed_time,
|
141
|
+
"credits_cost" => @credits_cost
|
142
142
|
}
|
143
143
|
}
|
144
144
|
@response = TextMagic::API::Response.message_status(@hash, false)
|
145
145
|
end
|
146
146
|
|
147
|
-
should
|
147
|
+
should "be a hash" do
|
148
148
|
@response.class.should == Hash
|
149
149
|
end
|
150
150
|
|
151
|
-
should
|
152
|
-
@response.keys.should == [
|
151
|
+
should "have message_ids as keys" do
|
152
|
+
@response.keys.should == ["141421"]
|
153
153
|
end
|
154
154
|
|
155
|
-
should
|
155
|
+
should "contain statuses" do
|
156
156
|
@response.values.first.should == @status
|
157
157
|
end
|
158
158
|
|
159
|
-
should
|
159
|
+
should "have text for all statuses" do
|
160
160
|
@response.values.first.text.should == @text
|
161
161
|
end
|
162
162
|
|
163
|
-
should
|
163
|
+
should "have created_time for all statuses" do
|
164
164
|
@response.values.first.created_time.should == Time.at(@created_time)
|
165
165
|
end
|
166
166
|
|
167
|
-
should
|
167
|
+
should "have completed_time for all statuses" do
|
168
168
|
@response.values.first.completed_time.should == Time.at(@completed_time)
|
169
169
|
end
|
170
170
|
|
171
|
-
should
|
171
|
+
should "have reply_number for all statuses" do
|
172
172
|
@response.values.first.reply_number.should == @reply_number
|
173
173
|
end
|
174
174
|
|
175
|
-
should
|
175
|
+
should "have credits_cost for all statuses" do
|
176
176
|
@response.values.first.credits_cost.should be_close(@credits_cost, 1e-10)
|
177
177
|
end
|
178
178
|
end
|
179
179
|
|
180
|
-
context
|
180
|
+
context "Response to receive command" do
|
181
181
|
|
182
182
|
setup do
|
183
183
|
@timestamp = (Time.now - 30).to_i
|
184
184
|
@text, @phone, @message_id = random_string, random_phone, random_string
|
185
185
|
@message = {
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
186
|
+
"timestamp" => @timestamp,
|
187
|
+
"from" => @phone,
|
188
|
+
"text" => @text,
|
189
|
+
"message_id" => @message_id
|
190
190
|
}
|
191
191
|
@unread = rand(1e4)
|
192
|
-
@hash = {
|
192
|
+
@hash = { "unread" => @unread, "messages" => [@message] }
|
193
193
|
@response = TextMagic::API::Response.receive(@hash)
|
194
194
|
end
|
195
195
|
|
196
|
-
should
|
196
|
+
should "have unread" do
|
197
197
|
@response.unread.should == @unread
|
198
198
|
end
|
199
199
|
|
200
|
-
should
|
200
|
+
should "be an array" do
|
201
201
|
@response.class.should == Array
|
202
202
|
end
|
203
203
|
|
204
|
-
should
|
204
|
+
should "contain strings with phones numbers and texts" do
|
205
205
|
@response.first.should == "#{@phone}: #{@text}"
|
206
206
|
end
|
207
207
|
|
208
|
-
should
|
208
|
+
should "have timestamp for all messages" do
|
209
209
|
@response.first.timestamp.should == Time.at(@timestamp)
|
210
210
|
end
|
211
211
|
|
212
|
-
should
|
212
|
+
should "have from for all messages" do
|
213
213
|
@response.first.from.should == @phone
|
214
214
|
end
|
215
215
|
|
216
|
-
should
|
216
|
+
should "have text for all messages" do
|
217
217
|
@response.first.text.should == @text
|
218
218
|
end
|
219
219
|
|
220
|
-
should
|
220
|
+
should "have message_id for all messages" do
|
221
221
|
@response.first.message_id.should == @message_id
|
222
222
|
end
|
223
223
|
end
|
224
224
|
|
225
|
-
context
|
225
|
+
context "Response to check_number command with single phone" do
|
226
226
|
|
227
227
|
setup do
|
228
228
|
@phone = random_phone
|
@@ -230,27 +230,27 @@ class ResponseTest < Test::Unit::TestCase
|
|
230
230
|
@country = random_string
|
231
231
|
@hash = {
|
232
232
|
@phone => {
|
233
|
-
|
234
|
-
|
233
|
+
"price" => @price,
|
234
|
+
"country" => @country
|
235
235
|
}
|
236
236
|
}
|
237
237
|
@response = TextMagic::API::Response.check_number(@hash, true)
|
238
238
|
end
|
239
239
|
|
240
|
-
should
|
240
|
+
should "be an OpenStruct instance" do
|
241
241
|
@response.class.should == OpenStruct
|
242
242
|
end
|
243
243
|
|
244
|
-
should
|
244
|
+
should "have price" do
|
245
245
|
@response.price.should be_close(@price, 1e-10)
|
246
246
|
end
|
247
247
|
|
248
|
-
should
|
248
|
+
should "have country" do
|
249
249
|
@response.country.should == @country
|
250
250
|
end
|
251
251
|
end
|
252
252
|
|
253
|
-
context
|
253
|
+
context "Response to check_number command with multiple phones" do
|
254
254
|
|
255
255
|
setup do
|
256
256
|
@phone = random_phone
|
@@ -258,30 +258,30 @@ class ResponseTest < Test::Unit::TestCase
|
|
258
258
|
@country = random_string
|
259
259
|
@hash = {
|
260
260
|
@phone => {
|
261
|
-
|
262
|
-
|
261
|
+
"price" => @price,
|
262
|
+
"country" => @country
|
263
263
|
}
|
264
264
|
}
|
265
265
|
@response = TextMagic::API::Response.check_number(@hash, false)
|
266
266
|
end
|
267
267
|
|
268
|
-
should
|
268
|
+
should "be a hash" do
|
269
269
|
@response.class.should == Hash
|
270
270
|
end
|
271
271
|
|
272
|
-
should
|
272
|
+
should "have phones as keys" do
|
273
273
|
@response.keys.should == [@phone]
|
274
274
|
end
|
275
275
|
|
276
|
-
should
|
276
|
+
should "contain OpenStruct instances" do
|
277
277
|
@response.values.first.class.should == OpenStruct
|
278
278
|
end
|
279
279
|
|
280
|
-
should
|
280
|
+
should "have price for all phones" do
|
281
281
|
@response.values.first.price.should be_close(@price, 1e-10)
|
282
282
|
end
|
283
283
|
|
284
|
-
should
|
284
|
+
should "have country for all phones" do
|
285
285
|
@response.values.first.country.should == @country
|
286
286
|
end
|
287
287
|
end
|
data/test/test_validation.rb
CHANGED
@@ -1,107 +1,106 @@
|
|
1
|
-
require
|
2
|
-
require 'json'
|
1
|
+
require "test_helper"
|
3
2
|
|
4
3
|
class ValidationTest < Test::Unit::TestCase
|
5
4
|
|
6
|
-
context
|
5
|
+
context "validate_text_length method for non-unicode texts" do
|
7
6
|
|
8
|
-
should
|
7
|
+
should "use real_length method to determine the real length of the message" do
|
9
8
|
text = random_string
|
10
9
|
TextMagic::API.expects(:real_length).with(text, false).returns(0)
|
11
10
|
TextMagic::API.validate_text_length(text, false, 1)
|
12
11
|
end
|
13
12
|
|
14
|
-
should
|
13
|
+
should "return true if parts limit is set to 1 and text length is less than or equal to 160" do
|
15
14
|
TextMagic::API.validate_text_length(random_string(160), false, 1).should == true
|
16
15
|
end
|
17
16
|
|
18
|
-
should
|
17
|
+
should "return false if parts limit is set to 1 and text length is greater than 160" do
|
19
18
|
TextMagic::API.validate_text_length(random_string(161), false, 1).should == false
|
20
19
|
end
|
21
20
|
|
22
|
-
should
|
21
|
+
should "return true if parts limit is set to 2 and text length is less than or equal to 306" do
|
23
22
|
TextMagic::API.validate_text_length(random_string(306), false, 2).should == true
|
24
23
|
end
|
25
24
|
|
26
|
-
should
|
25
|
+
should "return false if parts limit is set to 2 and text length is greater than 306" do
|
27
26
|
TextMagic::API.validate_text_length(random_string(307), false, 2).should == false
|
28
27
|
end
|
29
28
|
|
30
|
-
should
|
29
|
+
should "return true if parts limit is set to 3 or is not specified and text length is less than or equal to 459" do
|
31
30
|
TextMagic::API.validate_text_length(random_string(459), false).should == true
|
32
31
|
TextMagic::API.validate_text_length(random_string(459), false, 3).should == true
|
33
32
|
end
|
34
33
|
|
35
|
-
should
|
34
|
+
should "return false if parts limit is set to 3 or is not specified and text length is greater than 459" do
|
36
35
|
TextMagic::API.validate_text_length(random_string(460), false).should == false
|
37
36
|
TextMagic::API.validate_text_length(random_string(460), false, 3).should == false
|
38
37
|
end
|
39
38
|
end
|
40
39
|
|
41
|
-
context
|
40
|
+
context "validate_text_length method for unicode texts" do
|
42
41
|
|
43
|
-
should
|
42
|
+
should "use real_length method to determine the real length of the message" do
|
44
43
|
text = random_string
|
45
44
|
TextMagic::API.expects(:real_length).with(text, true).returns(0)
|
46
45
|
TextMagic::API.validate_text_length(text, true, 1)
|
47
46
|
end
|
48
47
|
|
49
|
-
should
|
48
|
+
should "return true if parts limit is set to 1 and text length is less than or equal to 70" do
|
50
49
|
TextMagic::API.validate_text_length(random_string(70), true, 1).should == true
|
51
50
|
end
|
52
51
|
|
53
|
-
should
|
52
|
+
should "return false if parts limit is set to 1 and text length is greater than 70" do
|
54
53
|
TextMagic::API.validate_text_length(random_string(71), true, 1).should == false
|
55
54
|
end
|
56
55
|
|
57
|
-
should
|
56
|
+
should "return true if parts limit is set to 2 and text length is less than or equal to 134" do
|
58
57
|
TextMagic::API.validate_text_length(random_string(134), true, 2).should == true
|
59
58
|
end
|
60
59
|
|
61
|
-
should
|
60
|
+
should "return false if parts limit is set to 2 and text length is greater than 134" do
|
62
61
|
TextMagic::API.validate_text_length(random_string(135), true, 2).should == false
|
63
62
|
end
|
64
63
|
|
65
|
-
should
|
64
|
+
should "return true if parts limit is set to 3 or is not specified and text length is less than or equal to 201" do
|
66
65
|
TextMagic::API.validate_text_length(random_string(201), true).should == true
|
67
66
|
TextMagic::API.validate_text_length(random_string(201), true, 3).should == true
|
68
67
|
end
|
69
68
|
|
70
|
-
should
|
69
|
+
should "return false if parts limit is set to 3 or is not specified and text length is greater than 201" do
|
71
70
|
TextMagic::API.validate_text_length(random_string(202), true).should == false
|
72
71
|
TextMagic::API.validate_text_length(random_string(202), true, 3).should == false
|
73
72
|
end
|
74
73
|
end
|
75
74
|
|
76
|
-
context
|
75
|
+
context "validate_phones method" do
|
77
76
|
|
78
|
-
should
|
77
|
+
should "return true if phone number consists of up to 15 digits" do
|
79
78
|
TextMagic::API.validate_phones(rand(10 ** 15).to_s).should == true
|
80
79
|
end
|
81
80
|
|
82
|
-
should
|
81
|
+
should "return false if phone number is longer than 15 digits" do
|
83
82
|
TextMagic::API.validate_phones((10 ** 16 + rand(10 ** 15)).to_s).should == false
|
84
83
|
end
|
85
84
|
|
86
|
-
should
|
85
|
+
should "return false if phone number contains non-digits" do
|
87
86
|
TextMagic::API.validate_phones(random_string).should == false
|
88
87
|
end
|
89
88
|
|
90
|
-
should
|
91
|
-
TextMagic::API.validate_phones(
|
89
|
+
should "return false if phone number is empty" do
|
90
|
+
TextMagic::API.validate_phones("").should == false
|
92
91
|
end
|
93
92
|
|
94
|
-
should
|
93
|
+
should "return true if all phone numbers in a list are valid" do
|
95
94
|
phone1, phone2 = rand(10 ** 15).to_s, rand(10 ** 15).to_s
|
96
95
|
TextMagic::API.validate_phones(phone1, phone2).should == true
|
97
96
|
TextMagic::API.validate_phones([phone1, phone2]).should == true
|
98
97
|
end
|
99
98
|
|
100
|
-
should
|
99
|
+
should "return false if phone numbers list is empty" do
|
101
100
|
TextMagic::API.validate_phones().should == false
|
102
101
|
end
|
103
102
|
|
104
|
-
should
|
103
|
+
should "return false if format of any of phone numbers in a list is invalid" do
|
105
104
|
phone1 = rand(10 ** 15).to_s, rand(10 ** 15).to_s
|
106
105
|
phone2 = random_string
|
107
106
|
TextMagic::API.validate_phones(phone1, phone2).should == false
|