bobes-textmagic 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,8 +1,10 @@
1
1
  = TextMagic
2
2
 
3
- TextMagic is a Ruby interface to the TextMagic's HTTP API. You need to have
4
- a valid TextMagic account to use this gem. Sign up at http://www.textmagic.com
5
- to get one.
3
+ +textmagic+ gem is a Ruby interface to the TextMagic's Bulk SMS Gateway.
4
+ It can be used to send SMS messages and receive replies, check statuses
5
+ of sent messages and retrieve account balance.
6
+ You need to have a valid TextMagic[http://www.textmagic.com] account to use this gem. Sign up at
7
+ http://www.textmagic.com to get one.
6
8
 
7
9
  == Installation
8
10
 
@@ -22,18 +24,72 @@ To create an API instance, run:
22
24
  with your credentials. Created instance will remember the username and password
23
25
  and will use it for all commands.
24
26
 
27
+ === Account balance
28
+
25
29
  To retrieve your account's balance, run:
26
30
 
27
- api.account['balance']
31
+ api.account.balance
32
+ # => 314.15
33
+
34
+ === Sending messages
28
35
 
29
- To send a text message, run:
36
+ To send a message to a single phone number, run:
30
37
 
31
- api.send 'Hi Vilma!', '441234567890'
38
+ api.send 'Hi Vilma!', '999314159265'
32
39
 
33
40
  You can even specify multiple phone numbers:
34
41
 
35
- api.send 'Hi Vilma!', '314159265358', '271828182845'
42
+ api.send 'Hi everybody!', '999314159265', '999271828182'
43
+
44
+ Unicode messages are supported as well:
45
+
46
+ api.send 'Вильма Привет!', '999314159265'
47
+
48
+ Long messages will be split to up to 3 parts. If you want to limit maximum number
49
+ of parts, you can specify an optional +max_length+ parameter:
50
+
51
+ api.send 'Very long message...', '999314159265', :max_length => 2
52
+
53
+ === Checking sent message status
54
+
55
+ If you want to check sent message status, you have to use +message_id+
56
+ returned in respose to +send+ command.
57
+
58
+ api.send('Hi Vilma!', '999314159265').message_id
59
+ # => '141421'
60
+ api.message_status('141421').status
61
+ # => 'd'
62
+
63
+ You can also supply several message_ids, in which case you'll get a hash with
64
+ message_ids as keys:
65
+
66
+ api.send('Hi Vilma!', '999314159265', '999271828182').message_ids
67
+ # => ['141421', '173205']
68
+ statuses = api.message_status('141421', '173205')
69
+ statuses['141421'].status
70
+ # => 'r'
71
+
72
+ === Receiving replies
73
+
74
+ To receive all available replies, run:
75
+
76
+ replies = api.receive.messages
77
+ # => [{ 'timestamp' => Fri May 22 12:12:55 +0200 2009, 'from' => '999314159265', 'text' => 'Hi Fred!', 'message_id' => '1780826' }]
78
+ replies.first.text
79
+ # => 'Hi Fred!'
80
+
81
+ To prevent receiving old messages, supply +last_retrieved_id+ argument:
82
+
83
+ replies = api.receive('1780826').messages
84
+ # => []
85
+
86
+ === Deleting retrieved replies
87
+
88
+ After you retrieve replies, you can delete them from server by running:
36
89
 
90
+ message_ids = api.receive.message_ids
91
+ # => ['141421', '1780826']
92
+ api.delete_reply '141421', '1780826'
37
93
 
38
94
  == Copyright
39
95
 
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 0
4
+ :patch: 1
data/lib/api.rb CHANGED
@@ -88,6 +88,7 @@ module TextMagic
88
88
  raise Error.new(9, 'Invalid phone number format') unless API.validate_phones(phones)
89
89
  response = Executor.execute('send', @username, @password, options.merge(:text => text, :phone => phones.join(',')))
90
90
  response.extend(TextMagic::API::Response::Send)
91
+ response
91
92
  end
92
93
 
93
94
  # Executes a message_status command and returns a hash with states of
@@ -116,10 +117,12 @@ module TextMagic
116
117
  # response['141421'].created_time
117
118
  # # => Fri May 22 10:10:18 +0200 2009
118
119
  def message_status(*ids)
120
+ single = ids.size == 1 && ids.first.is_a?(String)
119
121
  ids.flatten!
120
122
  raise TextMagic::API::Error.new(4, 'Insufficient parameters') if ids.empty?
121
123
  response = Executor.execute('message_status', @username, @password, :ids => ids.join(','))
122
124
  response.extend(TextMagic::API::Response::MessageStatus)
125
+ single ? response[ids.first] : response
123
126
  end
124
127
 
125
128
  # Executes a receive command and returns a hash with unread messages
data/lib/response.rb CHANGED
@@ -58,12 +58,12 @@ module TextMagic
58
58
  module Send #:nodoc: all
59
59
 
60
60
  def self.extended(base)
61
- return unless base.is_a?(Hash)
62
- base['message_id_hash'] = base.delete('message_id').invert if base['message_id']
63
- base['message_ids'] = base['message_id_hash'].values.sort if base['message_id_hash']
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
64
  end
65
65
 
66
- %w(message_id_hash message_ids sent_text parts_count).each do |method|
66
+ %w(message_ids sent_text parts_count).each do |method|
67
67
  module_eval <<-EOS
68
68
  def #{method}
69
69
  self['#{method}']
@@ -72,7 +72,7 @@ module TextMagic
72
72
  end
73
73
 
74
74
  def message_id(phone = nil)
75
- phone ? message_id_hash[phone] : message_ids.first
75
+ phone ? self[phone] : self['message_ids'].first
76
76
  end
77
77
  end
78
78
 
@@ -104,13 +104,14 @@ module TextMagic
104
104
 
105
105
  def self.extended(base)
106
106
  return unless base.is_a?(Hash) && base['messages']
107
+ base['message_ids'] = base['messages'].collect { |message| message['message_id'] }.sort
107
108
  base['messages'].each do |message|
108
109
  message['timestamp'] = Time.at(message['timestamp'].to_i) if message['timestamp']
109
110
  message.extend Message
110
111
  end
111
112
  end
112
113
 
113
- %w(messages unread).each do |method|
114
+ %w(messages message_ids unread).each do |method|
114
115
  module_eval <<-EOS, __FILE__, __LINE__ + 1
115
116
  def #{method}
116
117
  self['#{method}']
data/test/test_api.rb CHANGED
@@ -111,12 +111,12 @@ class APITest < Test::Unit::TestCase
111
111
  response.is_a?(TextMagic::API::Response::Send).should == true
112
112
  end
113
113
 
114
- should 'return a hash with message_id_hash, message_ids, sent_text and parts_count values' do
114
+ should 'return a hash with message_ids, sent_text and parts_count values' do
115
115
  message_id = random_string
116
116
  TextMagic::API::Executor.expects(:execute).returns({ 'message_id' => { message_id => @phone }, 'sent_text' => @text, 'parts_count' => 1 })
117
117
  response = @api.send(@text, @phone)
118
- response['message_id_hash'].should == { @phone => message_id }
119
118
  response['message_ids'].should == [message_id]
119
+ response['sent_text'].should == @text
120
120
  response['parts_count'].should == 1
121
121
  end
122
122
  end
@@ -126,12 +126,15 @@ class APITest < Test::Unit::TestCase
126
126
  setup do
127
127
  @username, @password = random_string, random_string
128
128
  @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)
129
133
  end
130
134
 
131
135
  should 'call Executor execute with correct arguments' do
132
- id = random_string
133
- TextMagic::API::Executor.expects(:execute).with('message_status', @username, @password, :ids => id)
134
- @api.message_status(id)
136
+ TextMagic::API::Executor.expects(:execute).with('message_status', @username, @password, :ids => @id).returns(@response)
137
+ @api.message_status(@id)
135
138
  end
136
139
 
137
140
  should 'join ids supplied as array' do
@@ -151,17 +154,43 @@ class APITest < Test::Unit::TestCase
151
154
  lambda { @api.message_status }.should raise_error(TextMagic::API::Error)
152
155
  end
153
156
 
154
- should 'return a hash extended with TextMagic::API::Response::MessageStatus' do
155
- TextMagic::API::Executor.expects(:execute).returns({ '8659912' => {} })
156
- response = @api.message_status(random_string)
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])
157
160
  response.class.should == Hash
158
161
  response.is_a?(TextMagic::API::Response::MessageStatus).should == true
159
162
  end
160
163
 
161
- should 'return a hash with message ids as keys' do
162
- TextMagic::API::Executor.expects(:execute).returns({ '8659912' => {} })
163
- response = @api.message_status(random_string)
164
- response['8659912'].should == {}
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
181
+ end
182
+
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
165
194
  end
166
195
  end
167
196
 
@@ -32,17 +32,13 @@ class ResponseTest < Test::Unit::TestCase
32
32
  @response.extend TextMagic::API::Response::Send
33
33
  end
34
34
 
35
- should 'allow access to inverted message_id hash' do
36
- @response.message_id_hash.should == @message_id.invert
37
- end
38
-
39
35
  should 'allow access to message_ids array' do
40
36
  @response.message_ids.should == ['141421', '173205']
41
37
  end
42
38
 
43
39
  should 'allow access to message_id for a given phone number' do
44
- @response.message_id('999314159265').should == '141421'
45
- @response.message_id('999271828182').should == '173205'
40
+ @response['999314159265'].should == '141421'
41
+ @response['999271828182'].should == '173205'
46
42
  end
47
43
 
48
44
  should 'allow access to sent_text' do
@@ -144,6 +140,10 @@ class ResponseTest < Test::Unit::TestCase
144
140
  @response.messages.should == @messages
145
141
  end
146
142
 
143
+ should 'allow access to message_ids array' do
144
+ @response.message_ids.should == ['141421', '173205']
145
+ end
146
+
147
147
  should 'allow access to message_id for all messages' do
148
148
  @response.messages.first.message_id.should == '141421'
149
149
  end
data/textmagic.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{textmagic}
5
- s.version = "0.2.0"
5
+ s.version = "0.2.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Vladimir Bobes Tuzinsky"]
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Bobes Tuzinsky