bobes-textmagic 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/error.rb CHANGED
@@ -6,8 +6,8 @@ module TextMagic
6
6
 
7
7
  attr_reader :code, :message
8
8
 
9
- # Creates an instance of TextMagic::API::Error. Error code and message
10
- # can be supplied as arguments or as a response hash.
9
+ # Creates an instance of TextMagic::API::Error. Error code and error message
10
+ # can be supplied as arguments or in a hash.
11
11
  #
12
12
  # TextMagic::API::Error.new(code, message)
13
13
  # TextMagic::API::Error.new('error_code' => code, 'error_message' => message)
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
@@ -2,175 +2,223 @@ require 'test_helper'
2
2
 
3
3
  class ResponseTest < Test::Unit::TestCase
4
4
 
5
- context 'Account response' do
5
+ context 'Response to account command' do
6
6
 
7
7
  setup do
8
8
  @balance = 0.1 * rand(1e4)
9
- @response = { 'balance' => @balance.to_s }
10
- @response.extend TextMagic::API::Response::Account
9
+ @hash = { 'balance' => @balance.to_s }
10
+ @response = TextMagic::API::Response.account(@hash)
11
11
  end
12
12
 
13
- should 'allow access to balance' do
13
+ should 'be an OpenStruct instance' do
14
+ @response.class.should == OpenStruct
15
+ end
16
+
17
+ should 'have balance' do
14
18
  @response.balance.should be_close(@balance, 1e-10)
15
19
  end
16
20
  end
17
21
 
18
- context 'Send response' do
22
+ context 'Response to send command with single phone number' do
19
23
 
20
24
  setup do
21
- @message_id = {
22
- '141421' => '999314159265',
23
- '173205' => '999271828182'
24
- }
25
+ @message_id, @phone = random_string, random_phone
25
26
  @text = random_string
26
- @parts_count = rand(10)
27
- @response = {
28
- 'message_id' => @message_id,
29
- 'sent_text' => @text,
30
- 'parts_count' => @parts_count
31
- }
32
- @response.extend TextMagic::API::Response::Send
27
+ @parts_count = 1 + rand(3)
28
+ @hash = { 'message_id' => { @message_id => @phone }, 'sent_text' => @text, 'parts_count' => @parts_count }
29
+ @response = TextMagic::API::Response.send(@hash, true)
30
+ end
31
+
32
+ should 'equal to the message_id' do
33
+ @response.should == @message_id
34
+ end
35
+
36
+ should 'have sent_text' do
37
+ @response.sent_text.should == @text
38
+ end
39
+
40
+ should 'have parts_count' do
41
+ @response.parts_count.should == @parts_count
42
+ end
43
+ end
44
+
45
+ context 'Response to send command with multiple phone numbers' do
46
+
47
+ setup do
48
+ @message_id1, @phone1 = random_string, random_phone
49
+ @message_id2, @phone2 = random_string, random_phone
50
+ @text = random_string
51
+ @parts_count = 1 + rand(3)
52
+ @hash = { 'message_id' => { @message_id1 => @phone1, @message_id2 => @phone2 }, 'sent_text' => @text, 'parts_count' => @parts_count }
53
+ @response = TextMagic::API::Response.send(@hash, false)
54
+ end
55
+
56
+ should 'be a hash' do
57
+ @response.class.should == Hash
33
58
  end
34
59
 
35
- should 'allow access to message_ids array' do
36
- @response.message_ids.should == ['141421', '173205']
60
+ should 'have phone numbers as keys' do
61
+ @response.keys.sort.should == [@phone1, @phone2].sort
37
62
  end
38
63
 
39
- should 'allow access to message_id for a given phone number' do
40
- @response['999314159265'].should == '141421'
41
- @response['999271828182'].should == '173205'
64
+ should 'have message ids as values' do
65
+ @response[@phone1].should == @message_id1
66
+ @response[@phone2].should == @message_id2
42
67
  end
43
68
 
44
- should 'allow access to sent_text' do
69
+ should 'have sent_text' do
45
70
  @response.sent_text.should == @text
46
71
  end
47
72
 
48
- should 'allow access to parts_count' do
73
+ should 'have parts_count' do
49
74
  @response.parts_count.should == @parts_count
50
75
  end
51
76
  end
52
77
 
53
- context 'MessageStatus response' do
78
+ context 'Response to message_status command with single id' do
54
79
 
55
80
  setup do
56
81
  @text = random_string
82
+ @status = random_string
57
83
  @reply_number = random_phone
58
84
  @created_time = (Time.now - 30).to_i
59
85
  @completed_time = (Time.now - 20).to_i
60
86
  @credits_cost = 0.01 * rand(300)
61
- @response = {
87
+ @hash = {
62
88
  '141421' => {
63
89
  'text' => @text,
64
- 'status' => 'd',
65
- 'created_time' => @created_time,
90
+ 'status' => @status,
91
+ 'created_time' => @created_time.to_s,
66
92
  'reply_number' => @reply_number,
67
- 'completed_time' => @completed_time,
93
+ 'completed_time' => @completed_time.to_s,
68
94
  'credits_cost' => @credits_cost
69
- },
70
- '173205' => {
71
- 'text' => 'test',
72
- 'status' => 'r',
73
- 'created_time' => '1242979839',
74
- 'reply_number' => '447624800500',
75
- 'completed_time' => nil,
76
- 'credits_cost' => 0.5
77
95
  }
78
96
  }
79
- @response.extend TextMagic::API::Response::MessageStatus
97
+ @response = TextMagic::API::Response.message_status(@hash, true)
80
98
  end
81
99
 
82
- should 'allow access to text for all statuses' do
83
- @response['141421'].text.should == @text
84
- @response['173205'].text.should == 'test'
100
+ should 'equal to the message status' do
101
+ @response.should == @status
85
102
  end
86
103
 
87
- should 'allow access to status for a given message_id' do
88
- @response['141421'].status.should == 'd'
89
- @response['173205'].status.should == 'r'
104
+ should 'have text' do
105
+ @response.text.should == @text
90
106
  end
91
107
 
92
- should 'allow access to reply_number for a given message_id' do
93
- @response['141421'].reply_number.should == @reply_number
94
- @response['173205'].reply_number.should == '447624800500'
108
+ should 'have created_time' do
109
+ @response.created_time.should == Time.at(@created_time)
95
110
  end
96
111
 
97
- should 'allow access to created_time for a given message_id' do
98
- @response['141421'].created_time.should == Time.at(@created_time)
99
- @response['173205'].created_time.should == Time.at(1242979839)
112
+ should 'have completed_time' do
113
+ @response.completed_time.should == Time.at(@completed_time)
100
114
  end
101
115
 
102
- should 'allow access to completed_time for a given message_id' do
103
- @response['141421'].completed_time.should == Time.at(@completed_time)
104
- @response['173205'].completed_time.should == nil
116
+ should 'have reply_number' do
117
+ @response.reply_number.should == @reply_number
105
118
  end
106
119
 
107
- should 'allow access to credits_cost for a given message_id' do
108
- @response['141421'].credits_cost.should be_close(@credits_cost, 1e-10)
109
- @response['173205'].credits_cost.should be_close(0.5, 1e-10)
120
+ should 'have credits_cost' do
121
+ @response.credits_cost.should be_close(@credits_cost, 1e-10)
110
122
  end
111
123
  end
112
-
113
- context 'Receive response' do
124
+
125
+ context 'Response to message_status command with multiple ids' do
114
126
 
115
127
  setup do
116
- @timestamp = (Time.now - 30).to_i
117
- @message1 = {
118
- 'timestamp' => @timestamp,
119
- 'from' => '999314159265',
120
- 'text' => 'Hi Fred',
121
- 'message_id' => '141421'
122
- }
123
- @message2 = {
124
- 'timestamp' => 1243244148,
125
- 'from' => '999271828182',
126
- 'text' => 'Hello buddy',
127
- 'message_id' => '173205'
128
+ @text = random_string
129
+ @status = random_string
130
+ @reply_number = random_phone
131
+ @created_time = (Time.now - 30).to_i
132
+ @completed_time = (Time.now - 20).to_i
133
+ @credits_cost = 0.01 * rand(300)
134
+ @hash = {
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
+ }
128
143
  }
129
- @messages = [@message1, @message2]
130
- @unread = rand(1e4)
131
- @response = { 'unread' => @unread, 'messages' => @messages }
132
- @response.extend TextMagic::API::Response::Receive
144
+ @response = TextMagic::API::Response.message_status(@hash, false)
133
145
  end
134
146
 
135
- should 'allow access to unread' do
136
- @response.unread.should == @unread
147
+ should 'be a hash' do
148
+ @response.class.should == Hash
149
+ end
150
+
151
+ should 'have message_ids as keys' do
152
+ @response.keys.should == ['141421']
137
153
  end
138
154
 
139
- should 'allow access to messages array' do
140
- @response.messages.should == @messages
155
+ should 'contain statuses' do
156
+ @response.values.first.should == @status
141
157
  end
142
158
 
143
- should 'allow access to message_ids array' do
144
- @response.message_ids.should == ['141421', '173205']
159
+ should 'have text for all statuses' do
160
+ @response.values.first.text.should == @text
145
161
  end
146
162
 
147
- should 'allow access to message_id for all messages' do
148
- @response.messages.first.message_id.should == '141421'
163
+ should 'have created_time for all statuses' do
164
+ @response.values.first.created_time.should == Time.at(@created_time)
149
165
  end
150
166
 
151
- should 'allow access to timestamp for all messages' do
152
- @response.messages.first.timestamp.should == Time.at(@timestamp)
167
+ should 'have completed_time for all statuses' do
168
+ @response.values.first.completed_time.should == Time.at(@completed_time)
153
169
  end
154
170
 
155
- should 'allow access to from for all messages' do
156
- @response.messages.first.from.should == '999314159265'
171
+ should 'have reply_number for all statuses' do
172
+ @response.values.first.reply_number.should == @reply_number
157
173
  end
158
174
 
159
- should 'allow access to text for all messages' do
160
- @response.messages.first.text.should == 'Hi Fred'
175
+ should 'have credits_cost for all statuses' do
176
+ @response.values.first.credits_cost.should be_close(@credits_cost, 1e-10)
161
177
  end
162
178
  end
163
179
 
164
- context 'DeleteReply response' do
180
+ context 'Response to receive command' do
165
181
 
166
182
  setup do
167
- @ids = ['141421', '1780826']
168
- @response = { 'deleted' => @ids }
169
- @response.extend TextMagic::API::Response::DeleteReply
183
+ @timestamp = (Time.now - 30).to_i
184
+ @text, @phone, @message_id = random_string, random_phone, random_string
185
+ @message = {
186
+ 'timestamp' => @timestamp,
187
+ 'from' => @phone,
188
+ 'text' => @text,
189
+ 'message_id' => @message_id
190
+ }
191
+ @unread = rand(1e4)
192
+ @hash = { 'unread' => @unread, 'messages' => [@message] }
193
+ @response = TextMagic::API::Response.receive(@hash)
194
+ end
195
+
196
+ should 'have unread' do
197
+ @response.unread.should == @unread
198
+ end
199
+
200
+ should 'be an array' do
201
+ @response.class.should == Array
202
+ end
203
+
204
+ should 'contain strings with phones numbers and texts' do
205
+ @response.first.should == "#{@phone}: #{@text}"
206
+ end
207
+
208
+ should 'have timestamp for all messages' do
209
+ @response.first.timestamp.should == Time.at(@timestamp)
210
+ end
211
+
212
+ should 'have from for allmessages' do
213
+ @response.first.from.should == @phone
214
+ end
215
+
216
+ should 'have text for all messages' do
217
+ @response.first.text.should == @text
170
218
  end
171
219
 
172
- should 'allow access to deleted' do
173
- @response.deleted.should == @ids
220
+ should 'have message_id for all messages' do
221
+ @response.first.message_id.should == @message_id
174
222
  end
175
223
  end
176
224
  end