rumeme 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -0
- data/lib/rumeme/configuration.rb +1 -0
- data/lib/rumeme/sms_interface.rb +29 -45
- data/lib/rumeme/sms_message.rb +4 -0
- data/lib/rumeme/version.rb +1 -1
- data/lib/rumeme.rb +2 -0
- metadata +2 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
Version 0.1.5 - 2010-03-12
|
2
|
+
===============================================================================
|
3
|
+
|
4
|
+
antlypls (1):
|
5
|
+
refactoring
|
6
|
+
|
7
|
+
|
1
8
|
Version 0.1.4 - 2010-03-12
|
2
9
|
===============================================================================
|
3
10
|
|
@@ -43,3 +50,4 @@ antlypls (5):
|
|
43
50
|
|
44
51
|
|
45
52
|
|
53
|
+
|
data/lib/rumeme/configuration.rb
CHANGED
data/lib/rumeme/sms_interface.rb
CHANGED
@@ -2,11 +2,13 @@ require "net/http"
|
|
2
2
|
require "net/https"
|
3
3
|
require 'rubygems'
|
4
4
|
require 'nokogiri'
|
5
|
+
require 'generator'
|
5
6
|
|
6
7
|
module Rumeme
|
7
8
|
|
8
9
|
# This is the main class used to interface with the M4U SMS messaging server.
|
9
10
|
class SmsInterface
|
11
|
+
class BadServerResponse < StandardError; end
|
10
12
|
|
11
13
|
# allow_splitting, allow_long_messages, response_code, response_message, username, password, use_message_id, secure, http_connection, server_list, message_list,
|
12
14
|
# http_proxy, http_proxy_port, http_proxy_auth, https_proxy, https_proxy_port, https_proxy_auth, text_buffer,
|
@@ -28,16 +30,24 @@ module Rumeme
|
|
28
30
|
@long_messages_strategy = cfg.long_messages_strategy
|
29
31
|
@replies_auto_confirm = cfg.replies_auto_confirm
|
30
32
|
}
|
31
|
-
|
33
|
+
|
32
34
|
@response_code = -1
|
33
35
|
@response_message = nil
|
34
36
|
@message_list = []
|
35
37
|
@server_list = ["smsmaster.m4u.com.au", "smsmaster1.m4u.com.au", "smsmaster2.m4u.com.au"]
|
38
|
+
|
39
|
+
@long_messages_processor = case @long_messages_strategy
|
40
|
+
when :send
|
41
|
+
lambda {|message| [message]}
|
42
|
+
when :cut
|
43
|
+
lambda {|message| [message[0..159]]}
|
44
|
+
when :split
|
45
|
+
lambda {|message| SmsInterface.split_message message}
|
46
|
+
end
|
36
47
|
end
|
37
48
|
|
38
49
|
# Add a message to be sent.
|
39
50
|
def add_message args
|
40
|
-
p 'in add_message '
|
41
51
|
phone_number = self.class.strip_invalid(args[:phone_number]) #not good idea, modifying original args, from outer scope (antlypls)
|
42
52
|
message = args[:message]
|
43
53
|
|
@@ -70,8 +80,6 @@ module Rumeme
|
|
70
80
|
|
71
81
|
# Return the list of replies we have received.
|
72
82
|
def check_replies
|
73
|
-
p 'in check_replies'
|
74
|
-
|
75
83
|
response_message, response_code = post_data_to_server("CHECKREPLY2.0\r\n.\r\n")
|
76
84
|
return if response_code != 150
|
77
85
|
|
@@ -83,39 +91,31 @@ module Rumeme
|
|
83
91
|
|
84
92
|
# sends confirmation to server
|
85
93
|
def confirm_replies_received
|
86
|
-
p 'in confirm_replies_received'
|
87
94
|
post_data_to_server "CONFIRM_RECEIVED\r\n.\r\n"
|
88
95
|
end
|
89
96
|
|
90
97
|
# Returns the credits remaining (for prepaid users only).
|
91
98
|
def get_credits_remaining
|
92
|
-
p 'in get_credits_remaining'
|
93
|
-
|
94
99
|
response_message, response_code = post_data_to_server("MESSAGES\r\n.\r\n")
|
95
100
|
|
96
101
|
if response_message =~ /^(\d+)\s+OK\s+(\d+).+/
|
97
102
|
if response_code != 100
|
98
|
-
|
99
|
-
return -1
|
103
|
+
raise BadServerResponse.new 'M4U code is not 100'
|
100
104
|
end
|
101
105
|
return $2.to_i
|
102
106
|
else
|
103
|
-
|
104
|
-
return -1
|
107
|
+
raise BadServerResponse.new "cant parse response: #{response_message}"
|
105
108
|
end
|
106
109
|
end
|
107
110
|
|
108
111
|
# Sends all the messages that have been added with the
|
109
112
|
# add_message command.
|
110
113
|
def send_messages
|
111
|
-
post_string = @message_list.map
|
112
|
-
"#{message.message_id} #{message.phone_number} #{message.delay} #{message.validity_period} #{message.delivery_report ? 1 : 0} #{message.message}\r\n"
|
113
|
-
}.join
|
114
|
-
|
114
|
+
post_string = @message_list.map(&:post_string).join
|
115
115
|
text_buffer = "MESSAGES2.0\r\n#{post_string}.\r\n"
|
116
116
|
response_message, response_code = post_data_to_server(text_buffer)
|
117
117
|
|
118
|
-
|
118
|
+
raise 'error during sending messages' if response_code != 100
|
119
119
|
end
|
120
120
|
|
121
121
|
private
|
@@ -129,12 +129,10 @@ module Rumeme
|
|
129
129
|
|
130
130
|
def self.split_message_internal message
|
131
131
|
list =[]
|
132
|
-
|
133
|
-
head, message = head_tail_split(message, 152)
|
134
|
-
list << head
|
132
|
+
sizes = Generator.new { |g| g.yield 152; g.yield 155 while true }
|
135
133
|
|
136
134
|
while !message.nil? do
|
137
|
-
head, message = head_tail_split(message,
|
135
|
+
head, message = head_tail_split(message, sizes.next)
|
138
136
|
list << head
|
139
137
|
end
|
140
138
|
|
@@ -149,16 +147,7 @@ module Rumeme
|
|
149
147
|
|
150
148
|
def process_long_message message
|
151
149
|
return [message] if message.length <= 160
|
152
|
-
|
153
|
-
when :send
|
154
|
-
[message]
|
155
|
-
when :cut
|
156
|
-
[message[0..160]]
|
157
|
-
when :split
|
158
|
-
split_message message
|
159
|
-
else
|
160
|
-
raise 'unknown long_messages_strategy'
|
161
|
-
end
|
150
|
+
@long_messages_processor.call(message)
|
162
151
|
end
|
163
152
|
|
164
153
|
# Strip invalid characters from the phone number.
|
@@ -167,18 +156,16 @@ module Rumeme
|
|
167
156
|
"+#{phone.gsub(/[^0-9]/, '')}"
|
168
157
|
end
|
169
158
|
|
159
|
+
def create_login_string # can be calculate once at initialization
|
160
|
+
message_id_sign = @use_message_id? '#' :''
|
161
|
+
"m4u\r\nUSER=#{@username}#{message_id_sign}\r\nPASSWORD=#{@password}\r\nVER=PHP1.0\r\n"
|
162
|
+
end
|
163
|
+
|
170
164
|
def post_data_to_server data
|
171
165
|
p 'post_data_to_server'
|
172
166
|
|
173
167
|
http_connection = open_server_connection(@server_list[0])
|
174
|
-
|
175
|
-
text_buffer = "m4u\r\nUSER=#{@username}"
|
176
|
-
if @use_message_id
|
177
|
-
text_buffer << "#"
|
178
|
-
end
|
179
|
-
text_buffer << "\r\nPASSWORD=#{@password}\r\nVER=PHP1.0\r\n"
|
180
|
-
|
181
|
-
text_buffer << data
|
168
|
+
text_buffer = create_login_string + data
|
182
169
|
|
183
170
|
p "buffer: #{text_buffer}"
|
184
171
|
headers = {'Content-Length' => text_buffer.length.to_s}
|
@@ -191,17 +178,14 @@ module Rumeme
|
|
191
178
|
p data.inspect
|
192
179
|
rescue
|
193
180
|
p "error: #{$!}"
|
194
|
-
|
181
|
+
raise BadServerResponse.new("error: #{$!}")
|
195
182
|
end
|
196
183
|
|
197
|
-
if resp.code.to_i != 200
|
198
|
-
p 'http response code != 200'
|
199
|
-
return false
|
200
|
-
end
|
184
|
+
raise BadServerResponse.new('http response code != 200') if resp.code.to_i != 200
|
201
185
|
|
202
186
|
doc = Nokogiri::HTML(data)
|
203
187
|
|
204
|
-
|
188
|
+
raise BadServerResponse.new('bad title') if doc.xpath('//title').text != "M4U SMSMASTER"
|
205
189
|
|
206
190
|
response_message = doc.xpath('//body').text.strip
|
207
191
|
response_code = nil
|
@@ -210,7 +194,7 @@ module Rumeme
|
|
210
194
|
end
|
211
195
|
|
212
196
|
p "latest response code: #{response_code}"
|
213
|
-
p "response #{response_message }"
|
197
|
+
p "response: #{response_message }"
|
214
198
|
|
215
199
|
[response_message, response_code]
|
216
200
|
end
|
data/lib/rumeme/sms_message.rb
CHANGED
data/lib/rumeme/version.rb
CHANGED
data/lib/rumeme.rb
CHANGED