Class: Rumeme::SmsInterface
- Inherits:
-
Object
- Object
- Rumeme::SmsInterface
- Defined in:
- lib/rumeme/sms_interface.rb
Overview
This is the main class used to interface with the M4U SMS messaging server.
Defined Under Namespace
Classes: BadServerResponse
Class Method Summary collapse
- .head_tail_split(message, max_len) ⇒ Object
- .split_message(message) ⇒ Object
- .split_message_internal(message) ⇒ Object
-
.strip_invalid(phone) ⇒ Object
Strip invalid characters from the phone number.
Instance Method Summary collapse
-
#add_message(args) ⇒ Object
Add a message to be sent.
-
#change_password ⇒ Object
Change the password on the local machine and server.
-
#check_replies ⇒ Object
Return the list of replies we have received.
-
#clear_messages ⇒ Object
Clear all the messages from the list.
-
#confirm_replies_received ⇒ Object
sends confirmation to server.
-
#get_credits_remaining ⇒ Object
Returns the credits remaining (for prepaid users only).
-
#initialize ⇒ SmsInterface
constructor
Constructor.
- #open_server_connection(server) ⇒ Object
-
#send_messages ⇒ Object
Sends all the messages that have been added with the add_message command.
-
#send_messages! ⇒ Object
Sends all the messages that have been added with the add_message command.
Constructor Details
#initialize ⇒ SmsInterface
Constructor.
The allowSplitting parameter determines whether messages over 160 characters will be split over multiple SMSes or truncated.
The allowLongMessages parameter enables messages longer than 160 characters to be sent as special concatenated messages. For this to take effect, the allowSplitting parameter must be set to false.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rumeme/sms_interface.rb', line 21 def initialize Rumeme.configuration.tap{ |cfg| @username = cfg.username @password = cfg.password @use_message_id = cfg. @secure = cfg.secure @long_messages_processor = case cfg. when :send lambda {|| []} when :cut lambda {|| [[0..159]]} when :split lambda {|| SmsInterface. } else lambda {|| raise ArgumentError.new("invalid long_messages_strategy")} end @replies_auto_confirm = cfg.replies_auto_confirm } @message_list = [] @server_list = %W(smsmaster.m4u.com.au smsmaster1.m4u.com.au smsmaster2.m4u.com.au) end |
Class Method Details
.head_tail_split(message, max_len) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/rumeme/sms_interface.rb', line 127 def head_tail_split , max_len return [, nil] if .length < max_len pattern = /\s\.,!;:-\)/ index = [0..max_len].rindex(pattern) || max_len [[0..index], [index+1 .. -1]] end |
.split_message(message) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/rumeme/sms_interface.rb', line 146 def = = 1 = .size ["#{[0]}...(1/#{})"].concat([1..-1].map {|msg| "(#{+=1}/#{})#{msg}"}) end |
.split_message_internal(message) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rumeme/sms_interface.rb', line 134 def list = [] sizes = Enumerator.new {|yielder| yielder << 152; yielder << 155 while true} until .nil? do head, = head_tail_split(, sizes.next) list << head end list end |
.strip_invalid(phone) ⇒ Object
Strip invalid characters from the phone number.
154 155 156 |
# File 'lib/rumeme/sms_interface.rb', line 154 def strip_invalid phone "+#{phone.gsub(/[^0-9]/, '')}" if phone end |
Instance Method Details
#add_message(args) ⇒ Object
Add a message to be sent.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rumeme/sms_interface.rb', line 48 def args phone_number = self.class.strip_invalid(args[:phone_number]) #not good idea, modifying original args, from outer scope (antlypls) = args[:message] raise ArgumentError.new("phone_number is empty") if phone_number.nil? || phone_number.empty? raise ArgumentError.new("message is empty") if .nil? || .empty? = () @message_list.concat(.map{|msg| SmsMessage.new(args.merge({:message => msg, :phone_number => phone_number}))}) end |
#change_password ⇒ Object
Change the password on the local machine and server. not implemented
75 76 77 |
# File 'lib/rumeme/sms_interface.rb', line 75 def change_password raise 'Not Implemented' end |
#check_replies ⇒ Object
Return the list of replies we have received.
80 81 82 83 84 85 86 87 88 |
# File 'lib/rumeme/sms_interface.rb', line 80 def check_replies , response_code = post_data_to_server("CHECKREPLY2.0\r\n.\r\n") return unless response_code == 150 = .split("\r\n")[1..-2].map{|| SmsReply.parse()} # check @use_message_id confirm_replies_received if @replies_auto_confirm && .size > 0 end |
#clear_messages ⇒ Object
Clear all the messages from the list.
60 61 62 |
# File 'lib/rumeme/sms_interface.rb', line 60 def @message_list.clear end |
#confirm_replies_received ⇒ Object
sends confirmation to server
91 92 93 |
# File 'lib/rumeme/sms_interface.rb', line 91 def confirm_replies_received post_data_to_server "CONFIRM_RECEIVED\r\n.\r\n" end |
#get_credits_remaining ⇒ Object
Returns the credits remaining (for prepaid users only).
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rumeme/sms_interface.rb', line 96 def get_credits_remaining , response_code = post_data_to_server("MESSAGES\r\n.\r\n") if =~ /^(\d+)\s+OK\s+(\d+).+/ if response_code != 100 raise BadServerResponse.new 'M4U code is not 100' end $2.to_i else raise BadServerResponse.new "cant parse response: #{}" end end |
#open_server_connection(server) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/rumeme/sms_interface.rb', line 64 def open_server_connection server port, use_ssl = @secure ? [443, true] : [80, false] http_connection = Net::HTTP.new(server, port) http_connection.verify_mode = OpenSSL::SSL::VERIFY_NONE http_connection.use_ssl = use_ssl http_connection end |
#send_messages ⇒ Object
Sends all the messages that have been added with the add_message command. returns boolean. true if successful, false if not.
111 112 113 114 115 116 |
# File 'lib/rumeme/sms_interface.rb', line 111 def post_string = @message_list.map(&:post_string).join text_buffer = "MESSAGES2.0\r\n#{post_string}.\r\n" , response_code = post_data_to_server(text_buffer) response_code == 100 end |
#send_messages! ⇒ Object
Sends all the messages that have been added with the add_message command. Raises exception if not successful
120 121 122 |
# File 'lib/rumeme/sms_interface.rb', line 120 def raise BadServerResponse.new('error during sending messages') unless end |