textmagic 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.rdoc +64 -47
- data/Rakefile +38 -2
- data/VERSION.yml +2 -2
- data/lib/api.rb +114 -104
- data/lib/error.rb +2 -2
- data/lib/response.rb +47 -124
- data/test/test_api.rb +54 -98
- data/test/test_response.rb +141 -93
- data/textmagic.gemspec +18 -7
- metadata +49 -8
data/lib/response.rb
CHANGED
@@ -1,141 +1,64 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
1
3
|
module TextMagic
|
2
4
|
|
3
5
|
class API
|
4
6
|
|
5
|
-
|
6
|
-
#
|
7
|
-
# === Account response hash
|
8
|
-
#
|
9
|
-
# When extended, it
|
10
|
-
# * converts the +balance+ value to +float+ and
|
11
|
-
# * adds a reader method +balance+.
|
12
|
-
#
|
13
|
-
# === Send response hash
|
14
|
-
#
|
15
|
-
# When extended, it
|
16
|
-
# * inverts the +message_id+ hash and puts it in +message_id_hash+,
|
17
|
-
# * adds an array of ids to +message_ids+,
|
18
|
-
# * adds reader methods +message_id_hash+, +message_ids+, +sent_text+ and
|
19
|
-
# +parts_count+ to the hash.
|
20
|
-
# * adds a reader method +message_id+, which returns a +message_id+ for
|
21
|
-
# a given phone number, or the first message_id if no phone number
|
22
|
-
# is specified.
|
23
|
-
#
|
24
|
-
# === Message status response hash
|
25
|
-
#
|
26
|
-
# When extended, it
|
27
|
-
# * converts the +credits_cost+ value to +float+,
|
28
|
-
# * converts the +created_time+ and +completed_time+ values to +Time+,
|
29
|
-
# * adds reader methods +text+, +status+, +reply_number+, +credits_cost+,
|
30
|
-
# +created_time+ and +completed_time+ to all values of the hash.
|
31
|
-
#
|
32
|
-
# === Receive status response hash
|
33
|
-
#
|
34
|
-
# When extended, it
|
35
|
-
# * converts the +timestamp+ value to +Time+,
|
36
|
-
# * adds reader methods +messages+ and +unread+ to the hash
|
37
|
-
# * adds reader methods +message_id+, +timestamp+, +text+ and +from+
|
38
|
-
# to all members of the +messages+ array.
|
39
|
-
#
|
40
|
-
# === Delete reply response hash
|
41
|
-
#
|
42
|
-
# When extended, it
|
43
|
-
# * adds a reader method +deleted+.
|
44
|
-
module Response
|
45
|
-
|
46
|
-
module Account #:nodoc: all
|
47
|
-
|
48
|
-
def self.extended(base)
|
49
|
-
return unless base.is_a?(Hash)
|
50
|
-
base['balance'] = base['balance'].to_f if base['balance']
|
51
|
-
end
|
52
|
-
|
53
|
-
def balance
|
54
|
-
self['balance']
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
module Send #:nodoc: all
|
59
|
-
|
60
|
-
def self.extended(base)
|
61
|
-
return unless base.is_a?(Hash) && base['message_id']
|
62
|
-
base['message_ids'] = base['message_id'].keys.sort
|
63
|
-
base.merge! base.delete('message_id').invert
|
64
|
-
end
|
65
|
-
|
66
|
-
%w(message_ids sent_text parts_count).each do |method|
|
67
|
-
module_eval <<-EOS
|
68
|
-
def #{method}
|
69
|
-
self['#{method}']
|
70
|
-
end
|
71
|
-
EOS
|
72
|
-
end
|
7
|
+
class Response
|
73
8
|
|
74
|
-
|
75
|
-
|
76
|
-
|
9
|
+
def self.account(hash)
|
10
|
+
response = OpenStruct.new(hash)
|
11
|
+
response.balance = response.balance.to_f
|
12
|
+
response
|
77
13
|
end
|
78
14
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
status['created_time'] = Time.at(status['created_time'].to_i) if status['created_time']
|
86
|
-
status['completed_time'] = Time.at(status['completed_time'].to_i) if status['completed_time']
|
87
|
-
status.extend Status
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
module Status
|
92
|
-
|
93
|
-
%w(text status reply_number credits_cost created_time completed_time).each do |method|
|
94
|
-
module_eval <<-EOS
|
95
|
-
def #{method}
|
96
|
-
self['#{method}']
|
97
|
-
end
|
98
|
-
EOS
|
99
|
-
end
|
15
|
+
def self.send(hash, single)
|
16
|
+
response = nil
|
17
|
+
if single
|
18
|
+
response = hash['message_id'].keys.first.dup
|
19
|
+
else
|
20
|
+
response = hash['message_id'].invert
|
100
21
|
end
|
22
|
+
metaclass = class << response; self; end
|
23
|
+
metaclass.send :attr_accessor, :sent_text, :parts_count, :message_id
|
24
|
+
response.sent_text = hash['sent_text']
|
25
|
+
response.parts_count = hash['parts_count']
|
26
|
+
response.message_id = hash['message_id']
|
27
|
+
response
|
101
28
|
end
|
102
29
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
def #{method}
|
117
|
-
self['#{method}']
|
118
|
-
end
|
119
|
-
EOS
|
120
|
-
end
|
121
|
-
|
122
|
-
module Message
|
123
|
-
|
124
|
-
%w(message_id timestamp text from).each do |method|
|
125
|
-
module_eval <<-EOS
|
126
|
-
def #{method}
|
127
|
-
self['#{method}']
|
128
|
-
end
|
129
|
-
EOS
|
130
|
-
end
|
30
|
+
def self.message_status(hash, single)
|
31
|
+
response = {}
|
32
|
+
hash.each do |message_id, message_hash|
|
33
|
+
status = message_hash['status'].dup
|
34
|
+
metaclass = class << status; self; end
|
35
|
+
metaclass.send :attr_accessor, :text, :credits_cost, :reply_number, :message_status, :created_time, :completed_time
|
36
|
+
status.text = message_hash['text']
|
37
|
+
status.credits_cost = message_hash['credits_cost']
|
38
|
+
status.reply_number = message_hash['reply_number']
|
39
|
+
status.message_status = message_hash['message_status']
|
40
|
+
status.created_time = Time.at(message_hash['created_time'].to_i) if message_hash['created_time']
|
41
|
+
status.completed_time = Time.at(message_hash['completed_time'].to_i) if message_hash['completed_time']
|
42
|
+
response[message_id] = status
|
131
43
|
end
|
44
|
+
single ? response.values.first : response
|
132
45
|
end
|
133
46
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
self
|
47
|
+
def self.receive(hash)
|
48
|
+
response = hash['messages'].collect do |message_hash|
|
49
|
+
message = "#{message_hash['from']}: #{message_hash['text']}"
|
50
|
+
metaclass = class << message; self; end
|
51
|
+
metaclass.send :attr_accessor, :timestamp, :message_id, :text, :from
|
52
|
+
message.text = message_hash['text']
|
53
|
+
message.from = message_hash['from']
|
54
|
+
message.message_id = message_hash['message_id']
|
55
|
+
message.timestamp = Time.at(message_hash['timestamp'].to_i)
|
56
|
+
message
|
138
57
|
end
|
58
|
+
metaclass = class << response; self; end
|
59
|
+
metaclass.send :attr_accessor, :unread
|
60
|
+
response.unread = hash['unread']
|
61
|
+
response
|
139
62
|
end
|
140
63
|
end
|
141
64
|
end
|
data/test/test_api.rb
CHANGED
@@ -13,27 +13,23 @@ class APITest < Test::Unit::TestCase
|
|
13
13
|
context 'Account command' do
|
14
14
|
|
15
15
|
setup do
|
16
|
-
@balance = 0.01 * rand(1e4)
|
17
16
|
@username, @password = random_string, random_string
|
18
17
|
@api = TextMagic::API.new(@username, @password)
|
18
|
+
@response = random_string
|
19
|
+
@processed_response = random_string
|
20
|
+
TextMagic::API::Executor.stubs(:execute).returns(@response)
|
21
|
+
TextMagic::API::Response.stubs(:account).returns(@processed_response)
|
19
22
|
end
|
20
23
|
|
21
24
|
should 'call Executor execute with correct arguments' do
|
22
|
-
TextMagic::API::Executor.expects(:execute).with('account', @username, @password).returns(
|
25
|
+
TextMagic::API::Executor.expects(:execute).with('account', @username, @password).returns(@response)
|
23
26
|
@api.account
|
24
27
|
end
|
25
28
|
|
26
|
-
should '
|
27
|
-
|
28
|
-
response
|
29
|
-
|
30
|
-
response.is_a?(TextMagic::API::Response::Account).should == true
|
31
|
-
end
|
32
|
-
|
33
|
-
should 'return a hash with balance value' do
|
34
|
-
TextMagic::API::Executor.expects(:execute).returns({ 'balance' => @balance.to_s })
|
35
|
-
response = @api.account
|
36
|
-
response['balance'].should be_close(@balance, 1e-10)
|
29
|
+
should 'call Response.account method to process the response hash' do
|
30
|
+
processed_response = rand
|
31
|
+
TextMagic::API::Response.expects(:account).with(@response).returns(processed_response)
|
32
|
+
@api.account.should == processed_response
|
37
33
|
end
|
38
34
|
end
|
39
35
|
|
@@ -43,33 +39,36 @@ class APITest < Test::Unit::TestCase
|
|
43
39
|
@username, @password = random_string, random_string
|
44
40
|
@text, @phone = random_string, random_phone
|
45
41
|
@api = TextMagic::API.new(@username, @password)
|
46
|
-
|
42
|
+
@response = random_string
|
43
|
+
@processed_response = random_string
|
44
|
+
TextMagic::API::Executor.stubs(:execute).returns(@response)
|
45
|
+
TextMagic::API::Response.stubs(:send).returns(@processed_response)
|
47
46
|
end
|
48
47
|
|
49
48
|
should 'call Executor execute with correct arguments' do
|
50
|
-
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone, :unicode => 0)
|
49
|
+
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone, :unicode => 0).returns(@response)
|
51
50
|
@api.send(@text, @phone)
|
52
51
|
end
|
53
52
|
|
54
53
|
should 'join multiple phone numbers supplied as an array' do
|
55
54
|
phones = Array.new(3) { random_phone }
|
56
|
-
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => phones.join(','), :unicode => 0)
|
55
|
+
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => phones.join(','), :unicode => 0).returns(@response)
|
57
56
|
@api.send(@text, phones)
|
58
57
|
end
|
59
58
|
|
60
59
|
should 'join multiple phone numbers supplied as arguments' do
|
61
60
|
phones = Array.new(3) { random_phone }
|
62
|
-
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => phones.join(','), :unicode => 0)
|
61
|
+
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => phones.join(','), :unicode => 0).returns(@response)
|
63
62
|
@api.send(@text, *phones)
|
64
63
|
end
|
65
64
|
|
66
65
|
should 'replace true with 1 for unicode' do
|
67
|
-
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone, :unicode => 1)
|
66
|
+
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone, :unicode => 1).returns(@response)
|
68
67
|
@api.send(@text, @phone, :unicode => true)
|
69
68
|
end
|
70
69
|
|
71
70
|
should 'set unicode value to 0 if it is not set to by user and text contains only characters from GSM 03.38 charset' do
|
72
|
-
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone, :unicode => 0).times(2)
|
71
|
+
TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone, :unicode => 0).returns(@response).times(2)
|
73
72
|
@api.send(@text, @phone)
|
74
73
|
@api.send(@text, @phone, :unicode => false)
|
75
74
|
end
|
@@ -103,21 +102,17 @@ class APITest < Test::Unit::TestCase
|
|
103
102
|
lambda { @api.send(@text, @phone) }.should raise_error(TextMagic::API::Error)
|
104
103
|
end
|
105
104
|
|
106
|
-
should '
|
107
|
-
|
108
|
-
TextMagic::API::
|
109
|
-
|
110
|
-
response.class.should == Hash
|
111
|
-
response.is_a?(TextMagic::API::Response::Send).should == true
|
105
|
+
should 'call Response.send method to process the response hash (single phone)' do
|
106
|
+
processed_response = rand
|
107
|
+
TextMagic::API::Response.expects(:send).with(@response, true).returns(processed_response)
|
108
|
+
@api.send(@text, random_phone).should == processed_response
|
112
109
|
end
|
113
110
|
|
114
|
-
should '
|
115
|
-
|
116
|
-
TextMagic::API::
|
117
|
-
|
118
|
-
|
119
|
-
response['sent_text'].should == @text
|
120
|
-
response['parts_count'].should == 1
|
111
|
+
should 'call Response.send method to process the response hash (multiple phones)' do
|
112
|
+
processed_response = rand
|
113
|
+
TextMagic::API::Response.expects(:send).with(@response, false).returns(processed_response).twice
|
114
|
+
@api.send(@text, [random_phone]).should == processed_response
|
115
|
+
@api.send(@text, random_phone, random_phone).should == processed_response
|
121
116
|
end
|
122
117
|
end
|
123
118
|
|
@@ -126,15 +121,16 @@ class APITest < Test::Unit::TestCase
|
|
126
121
|
setup do
|
127
122
|
@username, @password = random_string, random_string
|
128
123
|
@api = TextMagic::API.new(@username, @password)
|
129
|
-
@
|
130
|
-
@
|
131
|
-
@response
|
132
|
-
TextMagic::API::
|
124
|
+
@response = random_string
|
125
|
+
@processed_response = random_string
|
126
|
+
TextMagic::API::Executor.stubs(:execute).returns(@response)
|
127
|
+
TextMagic::API::Response.stubs(:message_status).returns(@processed_response)
|
133
128
|
end
|
134
129
|
|
135
130
|
should 'call Executor execute with correct arguments' do
|
136
|
-
|
137
|
-
|
131
|
+
id = random_string
|
132
|
+
TextMagic::API::Executor.expects(:execute).with('message_status', @username, @password, :ids => id).returns(@response)
|
133
|
+
@api.message_status(id)
|
138
134
|
end
|
139
135
|
|
140
136
|
should 'join ids supplied as array' do
|
@@ -154,43 +150,15 @@ class APITest < Test::Unit::TestCase
|
|
154
150
|
lambda { @api.message_status }.should raise_error(TextMagic::API::Error)
|
155
151
|
end
|
156
152
|
|
157
|
-
should '
|
158
|
-
TextMagic::API::
|
159
|
-
|
160
|
-
response.class.should == Hash
|
161
|
-
response.is_a?(TextMagic::API::Response::MessageStatus).should == true
|
162
|
-
end
|
163
|
-
|
164
|
-
should 'return a hash with message ids as keys for an array of ids' do
|
165
|
-
TextMagic::API::Executor.expects(:execute).returns(@response)
|
166
|
-
response = @api.message_status([@id])
|
167
|
-
response[@id].should == @status
|
168
|
-
end
|
169
|
-
|
170
|
-
should 'return a hash extended with TextMagic::API::Response::MessageStatus for a list of ids' do
|
171
|
-
TextMagic::API::Executor.expects(:execute).returns(@response)
|
172
|
-
response = @api.message_status(@id, random_string)
|
173
|
-
response.class.should == Hash
|
174
|
-
response.is_a?(TextMagic::API::Response::MessageStatus).should == true
|
175
|
-
end
|
176
|
-
|
177
|
-
should 'return a hash with message ids as keys for a list of ids' do
|
178
|
-
TextMagic::API::Executor.expects(:execute).returns(@response)
|
179
|
-
response = @api.message_status(@id, random_string)
|
180
|
-
response[@id].should == @status
|
153
|
+
should 'call Response.message_status method to process the response hash (single id)' do
|
154
|
+
TextMagic::API::Response.expects(:message_status).with(@response, true).returns(@processed_response)
|
155
|
+
@api.message_status(random_string).should == @processed_response
|
181
156
|
end
|
182
157
|
|
183
|
-
should '
|
184
|
-
TextMagic::API::
|
185
|
-
|
186
|
-
|
187
|
-
response.is_a?(TextMagic::API::Response::MessageStatus::Status).should == true
|
188
|
-
end
|
189
|
-
|
190
|
-
should 'return a hash with message ids as keys for a single id' do
|
191
|
-
TextMagic::API::Executor.expects(:execute).returns(@response)
|
192
|
-
response = @api.message_status(@id)
|
193
|
-
response.should == @status
|
158
|
+
should 'call Response.message_status method to process the response hash (multiple ids)' do
|
159
|
+
TextMagic::API::Response.expects(:message_status).with(@response, false).returns(@processed_response).twice
|
160
|
+
@api.message_status([random_string]).should == @processed_response
|
161
|
+
@api.message_status(random_string, random_string).should == @processed_response
|
194
162
|
end
|
195
163
|
end
|
196
164
|
|
@@ -199,6 +167,10 @@ class APITest < Test::Unit::TestCase
|
|
199
167
|
setup do
|
200
168
|
@username, @password = random_string, random_string
|
201
169
|
@api = TextMagic::API.new(@username, @password)
|
170
|
+
@response = random_string
|
171
|
+
@processed_response = random_string
|
172
|
+
TextMagic::API::Executor.stubs(:execute).returns(@response)
|
173
|
+
TextMagic::API::Response.stubs(:receive).returns(@processed_response)
|
202
174
|
end
|
203
175
|
|
204
176
|
should 'call Executor execute with correct arguments' do
|
@@ -212,18 +184,9 @@ class APITest < Test::Unit::TestCase
|
|
212
184
|
@api.receive(last_retrieved_id)
|
213
185
|
end
|
214
186
|
|
215
|
-
should '
|
216
|
-
TextMagic::API::
|
217
|
-
|
218
|
-
response.class.should == Hash
|
219
|
-
response.is_a?(TextMagic::API::Response::Receive).should == true
|
220
|
-
end
|
221
|
-
|
222
|
-
should 'return a hash with unread and messages values' do
|
223
|
-
TextMagic::API::Executor.expects(:execute).returns({ 'messages' => [], 'unread' => 0 })
|
224
|
-
response = @api.receive
|
225
|
-
response['unread'].should == 0
|
226
|
-
response['messages'].should == []
|
187
|
+
should 'call Response.receive method to process the response hash' do
|
188
|
+
TextMagic::API::Response.expects(:receive).with(@response).returns(@processed_response)
|
189
|
+
@api.receive(random_string).should == @processed_response
|
227
190
|
end
|
228
191
|
end
|
229
192
|
|
@@ -232,6 +195,10 @@ class APITest < Test::Unit::TestCase
|
|
232
195
|
setup do
|
233
196
|
@username, @password = random_string, random_string
|
234
197
|
@api = TextMagic::API.new(@username, @password)
|
198
|
+
@response = random_string
|
199
|
+
@processed_response = random_string
|
200
|
+
TextMagic::API::Executor.stubs(:execute).returns(@response)
|
201
|
+
TextMagic::API::Response.stubs(:delete_reply).returns(@processed_response)
|
235
202
|
end
|
236
203
|
|
237
204
|
should 'call Executor execute with correct arguments' do
|
@@ -257,19 +224,8 @@ class APITest < Test::Unit::TestCase
|
|
257
224
|
lambda { @api.delete_reply }.should raise_error(TextMagic::API::Error)
|
258
225
|
end
|
259
226
|
|
260
|
-
should 'return
|
261
|
-
|
262
|
-
TextMagic::API::Executor.expects(:execute).returns({ 'deleted' => ids })
|
263
|
-
response = @api.delete_reply(ids)
|
264
|
-
response.class.should == Hash
|
265
|
-
response.is_a?(TextMagic::API::Response::DeleteReply).should == true
|
266
|
-
end
|
267
|
-
|
268
|
-
should 'return a hash with deleted value' do
|
269
|
-
ids = Array.new(3) { random_string }
|
270
|
-
TextMagic::API::Executor.expects(:execute).returns({ 'deleted' => ids })
|
271
|
-
response = @api.delete_reply(ids)
|
272
|
-
response.deleted.should == ids
|
227
|
+
should 'return true' do
|
228
|
+
@api.delete_reply(random_string).should == true
|
273
229
|
end
|
274
230
|
end
|
275
231
|
end
|