textmagic 0.2.2 → 0.3.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/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
- # Used to cleanup response hash and extend it with custom reader methods.
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
- def message_id(phone = nil)
75
- phone ? self[phone] : self['message_ids'].first
76
- end
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
- module MessageStatus #:nodoc: all
80
-
81
- def self.extended(base)
82
- return unless base.is_a?(Hash)
83
- base.values.each do |status|
84
- status['credits_cost'] = status['credits_cost'].to_f if status['credits_cost']
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
- module Receive #:nodoc: all
104
-
105
- def self.extended(base)
106
- return unless base.is_a?(Hash) && base['messages']
107
- base['message_ids'] = base['messages'].collect { |message| message['message_id'] }.sort
108
- base['messages'].each do |message|
109
- message['timestamp'] = Time.at(message['timestamp'].to_i) if message['timestamp']
110
- message.extend Message
111
- end
112
- end
113
-
114
- %w(messages message_ids unread).each do |method|
115
- module_eval <<-EOS, __FILE__, __LINE__ + 1
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
- module DeleteReply #:nodoc: all
135
-
136
- def deleted
137
- self['deleted']
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 'return a hash extended with TextMagic::API::Response::Account' do
27
- TextMagic::API::Executor.expects(:execute).returns({ 'balance' => @balance.to_s })
28
- response = @api.account
29
- response.class.should == Hash
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
- TextMagic::API::Executor.stubs(:execute)
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 'return a hash extended with TextMagic::API::Response::Send' do
107
- message_id = random_string
108
- TextMagic::API::Executor.expects(:execute).returns({ 'message_id' => { message_id => @phone }, 'sent_text' => @text, 'parts_count' => 1 })
109
- response = @api.send(@text, @phone)
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 'return a hash with message_ids, sent_text and parts_count values' do
115
- message_id = random_string
116
- TextMagic::API::Executor.expects(:execute).returns({ 'message_id' => { message_id => @phone }, 'sent_text' => @text, 'parts_count' => 1 })
117
- response = @api.send(@text, @phone)
118
- response['message_ids'].should == [message_id]
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
- @id = random_string
130
- @status = { 'text' => 'Hi Vilma', 'status' => 'd' , 'created_time' => Time.now.to_i, 'reply_number' => '447624800500', 'completed_time' => nil, 'credits_cost' => 0.5 }
131
- @response = { @id => @status }
132
- TextMagic::API::Executor.stubs(:execute)
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
- TextMagic::API::Executor.expects(:execute).with('message_status', @username, @password, :ids => @id).returns(@response)
137
- @api.message_status(@id)
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 'return a hash extended with TextMagic::API::Response::MessageStatus for an array of ids' do
158
- TextMagic::API::Executor.expects(:execute).returns(@response)
159
- response = @api.message_status([@id])
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 'return a hash extended with TextMagic::API::Response::MessageStatus::Status for a single id' do
184
- TextMagic::API::Executor.expects(:execute).returns(@response)
185
- response = @api.message_status(@id)
186
- response.class.should == Hash
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 'return a hash extended with TextMagic::API::Response::Receive' do
216
- TextMagic::API::Executor.expects(:execute).returns({ 'messages' => [], 'unread' => 0 })
217
- response = @api.receive
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 a hash extended with TextMagic::API::Response::DeleteReply' do
261
- ids = Array.new(3) { random_string }
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