rumeme 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ Version 0.1.4 - 2010-03-12
2
+ ===============================================================================
3
+
4
+ antlypls (3):
5
+ reimplement SmsReply.parse
6
+ minor refactoring of constructor code
7
+ refactoring
8
+
9
+
1
10
  Version 0.1.3 - 2010-03-11
2
11
  ===============================================================================
3
12
 
@@ -33,3 +42,4 @@ antlypls (5):
33
42
 
34
43
 
35
44
 
45
+
@@ -5,6 +5,7 @@ module Rumeme
5
5
  attr_accessor :password
6
6
  attr_accessor :use_message_id
7
7
  attr_accessor :secure
8
+ attr_accessor :replies_auto_confirm
8
9
 
9
10
  #
10
11
  # possible values
@@ -12,6 +13,10 @@ module Rumeme
12
13
  # :split - splits messages into small (less than 160 ch) messages
13
14
  # :cut - sends only first 160 symbols
14
15
  attr_accessor :long_messages_strategy
16
+
17
+ def initialize
18
+ @replies_auto_confirm = true
19
+ end
15
20
  end
16
21
  end
17
22
 
@@ -20,55 +20,32 @@ module Rumeme
20
20
  # characters to be sent as special concatenated messages. For this
21
21
  # to take effect, the allowSplitting parameter must be set to false.
22
22
  def initialize
23
- @username = Rumeme.configuration.username
24
- @password = Rumeme.configuration.password
25
- @use_message_id = Rumeme.configuration.use_message_id
26
- @secure = Rumeme.configuration.secure
27
- @long_messages_strategy = Rumeme.configuration.long_messages_strategy
23
+ Rumeme.configuration.tap{ |cfg|
24
+ @username = cfg.username
25
+ @password = cfg.password
26
+ @use_message_id = cfg.use_message_id
27
+ @secure = cfg.secure
28
+ @long_messages_strategy = cfg.long_messages_strategy
29
+ @replies_auto_confirm = cfg.replies_auto_confirm
30
+ }
28
31
 
29
32
  @response_code = -1
30
33
  @response_message = nil
31
34
  @message_list = []
32
- @http_proxy = nil
33
- @http_proxy_port = 80
34
- @http_proxy_auth = nil
35
- @https_proxy = nil
36
- @https_proxy_port = 443
37
- @https_proxy_auth = nil
38
- @text_buffer = nil
39
35
  @server_list = ["smsmaster.m4u.com.au", "smsmaster1.m4u.com.au", "smsmaster2.m4u.com.au"]
40
36
  end
41
37
 
42
-
43
- # Set the HTTP proxy server, if one is being used.
44
- # Also specify an optional proxy username and password.
45
- # only for php version
46
- def set_http_proxy proxy, port = 80, username = nil, password = nil
47
- @http_proxy, @http_proxy_port = proxy, port
48
- @http_proxy_username, @http_proxy_password = username, password
49
- @http_proxy_auth = Base64.encode64("#{username}:#{password}").chop unless username.nil? || password.nil?
50
- raise 'proxy is not supported'
51
- end
52
-
53
- # Set the HTTPS proxy server, if one is being used.
54
- # Also specify an optional proxy username and password.
55
- # only for php version
56
- def set_https_proxy proxy, port = 443, username = nil, password = nil
57
- @https_proxy, @https_proxy_port = proxy, port
58
- @https_proxy_auth = Base64.encode64("#{username}:#{password}").chop unless username.nil? || password.nil?
59
- raise 'proxy is not supported'
60
- end
61
-
62
38
  # Add a message to be sent.
63
39
  def add_message args
64
40
  p 'in add_message '
65
- args[:phone_number] = strip_invalid(args[:phone_number]) #not good idea, modifying original args, from outer scope (antlypls)
41
+ phone_number = self.class.strip_invalid(args[:phone_number]) #not good idea, modifying original args, from outer scope (antlypls)
42
+ message = args[:message]
66
43
 
67
- raise ArgumentError.new("phone_number is empty") if args[:phone_number].nil? || args[:phone_number].empty?
68
- raise ArgumentError.new("message is empty") if args[:message].nil? || args[:message].empty?
44
+ raise ArgumentError.new("phone_number is empty") if phone_number.nil? || phone_number.empty?
45
+ raise ArgumentError.new("message is empty") if message.nil? || message.empty?
69
46
 
70
- messages = process_long_message(args[:message])
71
- @message_list.concat(messages.map{|m| SmsMessage.new(args.merge({:message => m}))})
47
+ messages = process_long_message(message)
48
+ @message_list.concat(messages.map{|msg| SmsMessage.new(args.merge({:message => msg, :phone_number => phone_number}))})
72
49
  end
73
50
 
74
51
  # Clear all the messages from the list.
@@ -76,8 +53,8 @@ module Rumeme
76
53
  @message_list.clear
77
54
  end
78
55
 
79
- def open_server_connection server, secure
80
- port, use_ssl = secure ? [443, true] : [80, false]
56
+ def open_server_connection server
57
+ port, use_ssl = @secure ? [443, true] : [80, false]
81
58
 
82
59
  http_connection = Net::HTTP.new(server, port)
83
60
  http_connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -92,14 +69,14 @@ module Rumeme
92
69
  end
93
70
 
94
71
  # Return the list of replies we have received.
95
- def check_replies auto_confirm = true
72
+ def check_replies
96
73
  p 'in check_replies'
97
74
 
98
75
  response_message, response_code = post_data_to_server("CHECKREPLY2.0\r\n.\r\n")
99
76
  return if response_code != 150
100
77
 
101
- messages = response_message.split("\r\n")[1..-2].map{|message_line| SmsReply.parse(message_line, @use_message_id)}
102
- confirm_replies_received if auto_confirm && messages.size > 0
78
+ messages = response_message.split("\r\n")[1..-2].map{|message_line| SmsReply.parse(message_line)} # check @use_message_id
79
+ confirm_replies_received if @replies_auto_confirm && messages.size > 0
103
80
 
104
81
  return messages
105
82
  end
@@ -131,16 +108,11 @@ module Rumeme
131
108
  # Sends all the messages that have been added with the
132
109
  # add_message command.
133
110
  def send_messages
134
- text_buffer = "MESSAGES2.0\r\n"
135
-
136
- @message_list.each {|sm|
137
- s = "#{sm.message_id} #{sm.phone_number} #{sm.delay} #{sm.validity_period} "
138
- s << (sm.delivery_report ? "1 " : "0 ")
139
- s << "#{sm.message}\r\n"
140
- text_buffer << s
141
- }
142
- text_buffer << ".\r\n"
111
+ post_string = @message_list.map{ |message|
112
+ "#{message.message_id} #{message.phone_number} #{message.delay} #{message.validity_period} #{message.delivery_report ? 1 : 0} #{message.message}\r\n"
113
+ }.join
143
114
 
115
+ text_buffer = "MESSAGES2.0\r\n#{post_string}.\r\n"
144
116
  response_message, response_code = post_data_to_server(text_buffer)
145
117
 
146
118
  return response_code == 100 ? true : false
@@ -148,24 +120,11 @@ module Rumeme
148
120
 
149
121
  private
150
122
 
151
- def process_long_message message
152
- return [message] if message.length <= 160
153
- case @long_messages_strategy
154
- when :send
155
- [message]
156
- when :cut
157
- [message[0..160]]
158
- when :split
159
- SmsInterface.split_message message
160
- else
161
- raise 'unknown long_messages_strategy'
162
- end
163
- end
164
-
165
- def self.split_message message
166
- messages = split_message_internal message
167
- i = 1
168
- ["#{messages[0]}...(1/#{messages.size})"].concat(messages[1..-1].map {|m| "(#{i+=1}/#{messages.size})#{m}"})
123
+ def self.head_tail_split message, max_len
124
+ return [message, nil] if message.length < max_len
125
+ pattern = /\s\.,!;:-\)/
126
+ index = message[0..max_len].rindex(pattern) || max_len
127
+ [message[0..index], message[index+1 .. -1]]
169
128
  end
170
129
 
171
130
  def self.split_message_internal message
@@ -182,23 +141,36 @@ module Rumeme
182
141
  list
183
142
  end
184
143
 
185
- def self.head_tail_split message, max_len
186
- return [message, nil] if message.length < max_len
187
- pattern = /\s\.,!;:-\)/
188
- index = message[0..max_len].rindex(pattern) || max_len
189
- [message[0..index], message[index+1 .. -1]]
144
+ def self.split_message message
145
+ messages = split_message_internal message
146
+ message_index = 1
147
+ ["#{messages[0]}...(1/#{messages.size})"].concat(messages[1..-1].map {|msg| "(#{message_index+=1}/#{messages.size})#{msg}"})
148
+ end
149
+
150
+ def process_long_message message
151
+ return [message] if message.length <= 160
152
+ case @long_messages_strategy
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
190
162
  end
191
163
 
192
164
  # Strip invalid characters from the phone number.
193
- def strip_invalid phone
194
- return if phone.nil?
165
+ def self.strip_invalid phone
166
+ return nil if phone.nil?
195
167
  "+#{phone.gsub(/[^0-9]/, '')}"
196
168
  end
197
169
 
198
170
  def post_data_to_server data
199
171
  p 'post_data_to_server'
200
172
 
201
- http_connection = open_server_connection(@server_list[0], @secure)
173
+ http_connection = open_server_connection(@server_list[0])
202
174
 
203
175
  text_buffer = "m4u\r\nUSER=#{@username}"
204
176
  if @use_message_id
@@ -8,7 +8,7 @@ module Rumeme
8
8
  # this defaults must be moved to global configuration
9
9
  defaults = {:phone_number => nil, :message => nil, :message_id => 0, :delay => 0, :validity_period => ValidityPeriod::THREE_DAYS, :delivery_report => false}
10
10
  params = defaults.merge args
11
- defaults.keys.each {|k| instance_variable_set("@#{k.to_s}".to_sym, params[k])}
11
+ defaults.keys.each {|key| instance_variable_set("@#{key.to_s}".to_sym, params[key])}
12
12
 
13
13
  raise ArgumentError.new("phone_number is empty") if @phone_number.nil? || @phone_number.empty?
14
14
  raise ArgumentError.new("message is empty") if @message.nil? || @message.empty?
@@ -14,60 +14,28 @@ module Rumeme
14
14
  line.nil? ? nil : line.gsub('\n', "\n").gsub('\r', "\r").gsub('\\\\', "\\")
15
15
  end
16
16
 
17
- #Parse a reply from a string.
18
- # Format is: messageID phone when message
19
- # Or if no message ID: phone when message
20
- # Or if delivery receipt: messageID messageStatus when
21
- # php suxx. reimplement using regex
22
- def self.parse line, use_message_id
23
- p "parsing line: #{line}. #{use_message_id}."
24
- message_id = nil;
25
- status = MessageStatus::NONE
26
-
27
- prev_idx = 0;
28
- if (idx = line.index(' ')) == nil
29
- return nil
30
- end
31
-
32
- if (use_message_id)
33
- if line[0..idx] =~ /\d+/
34
- message_id = line[0..idx].to_i
17
+ # Parse a reply from a string.
18
+ # Format is: messageID phone when message /(\d+)\s(\d+)\s(\d+)\s(.+)/
19
+ # Or if no message ID: phone when message /(\d+)\s(\d+)\s(.+)/
20
+ # Or if delivery receipt: messageID messageStatus when /(\d+)\s(\d)\s(\d+)/
21
+ # current implementation ignores use_message_id setting (as original code)
22
+ def self.parse line
23
+ p "parsing line: #{line}"
24
+
25
+ message_id, status, message, phone, when_ = case line
26
+ when /(\d+)\s(\d)\s(\d+)/
27
+ #process delivery report
28
+ [$1.to_i, $2.to_i, nil, nil, $3.to_i]
29
+ when /(\d+)\s(\d+)\s(\d+)\s(.+)/
30
+ #process message with id
31
+ [$1.to_i, MessageStatus::NONE, unescape($4), $2, $3.to_i]
32
+ when /(\d+)\s(\d+)\s(.+)/
33
+ #process message without id
34
+ [nil, MessageStatus::NONE, unescape($3), $1, $2.to_i]
35
35
  else
36
- return nil
37
- end
38
-
39
- prev_idx = idx + 1
40
- if (idx = line.index(' ', idx + 1)) == nil
41
- return nil
42
- end
43
- end
44
-
45
- phone = line[prev_idx .. idx - 1]
46
-
47
- if phone.length == 1
48
- status = case phone # why not use to_i ??
49
- when "1"
50
- MessageStatus::PENDING
51
- when "2"
52
- MessageStatus::DELIVERED
53
- when "3"
54
- MessageStatus::FAILED
55
- else
56
- nil
57
- end
58
- phone = ""
59
- end
60
-
61
- prev_idx = idx + 1;
62
- idx = line.index(' ', idx + 1) || line.length
63
-
64
- if line[prev_idx .. idx-1] =~ /\d+/
65
- when_ = $&.to_i
66
- else
67
- return nil
36
+ raise ArgumentError.new("can't parse line: #{line}")
68
37
  end
69
38
 
70
- message = (status != MessageStatus::NONE) || (line.length < idx + 2) ? "" : unescape(line[idx + 1 .. -1])
71
39
  return SmsReply.new(phone, message, message_id, when_, status)
72
40
  end
73
41
 
@@ -1,3 +1,3 @@
1
1
  module Rumeme
2
- VERSION = "0.1.3".freeze
2
+ VERSION = "0.1.4".freeze
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - antlypls
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-11 00:00:00 +04:00
17
+ date: 2010-03-12 00:00:00 +04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency