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 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
+
@@ -16,6 +16,7 @@ module Rumeme
16
16
 
17
17
  def initialize
18
18
  @replies_auto_confirm = true
19
+ @long_messages_strategy = :send
19
20
  end
20
21
  end
21
22
  end
@@ -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
- p 'M4U code is not 100'
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
- p "cant parse response: #{response_message}"
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{ |message|
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
- return response_code == 100 ? true : false
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, 155)
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
- 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
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
- return false
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
- return false if doc.xpath('//title').text != "M4U SMSMASTER"
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
@@ -15,5 +15,9 @@ module Rumeme
15
15
 
16
16
  @message = @message.gsub("\n",'\n').gsub("\r",'\r').gsub("\\",'\\\\')
17
17
  end
18
+
19
+ def post_string
20
+ "#{@message_id} #{@phone_number} #{@delay} #{@validity_period} #{@delivery_report ? 1 : 0} #{@message}\r\n"
21
+ end
18
22
  end
19
23
  end
@@ -1,3 +1,3 @@
1
1
  module Rumeme
2
- VERSION = "0.1.4".freeze
2
+ VERSION = "0.1.5".freeze
3
3
  end
data/lib/rumeme.rb CHANGED
@@ -12,6 +12,8 @@ module Rumeme
12
12
  def configure
13
13
  self.configuration ||= Configuration.new
14
14
  yield(configuration)
15
+
16
+ raise 'unknown long_messages_strategy' unless [:split, :send, :cut].include?(configuration.long_messages_strategy)
15
17
  end
16
18
  end
17
19
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - antlypls