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
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.
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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rumeme/sms_interface.rb', line 24 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 = ["smsmaster.m4u.com.au", "smsmaster1.m4u.com.au", "smsmaster2.m4u.com.au"] end |
Instance Method Details
#add_message(args) ⇒ Object
Add a message to be sent.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rumeme/sms_interface.rb', line 51 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
78 79 80 |
# File 'lib/rumeme/sms_interface.rb', line 78 def change_password raise 'Not Implemented' end |
#check_replies ⇒ Object
Return the list of replies we have received.
83 84 85 86 87 88 89 90 91 |
# File 'lib/rumeme/sms_interface.rb', line 83 def check_replies , response_code = post_data_to_server("CHECKREPLY2.0\r\n.\r\n") return if response_code != 150 = .split("\r\n")[1..-2].map{|| SmsReply.parse()} # check @use_message_id confirm_replies_received if @replies_auto_confirm && .size > 0 return end |
#clear_messages ⇒ Object
Clear all the messages from the list.
63 64 65 |
# File 'lib/rumeme/sms_interface.rb', line 63 def @message_list.clear end |
#confirm_replies_received ⇒ Object
sends confirmation to server
94 95 96 |
# File 'lib/rumeme/sms_interface.rb', line 94 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).
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rumeme/sms_interface.rb', line 99 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 return $2.to_i else raise BadServerResponse.new "cant parse response: #{}" end end |
#open_server_connection(server) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/rumeme/sms_interface.rb', line 67 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.
114 115 116 117 118 119 120 |
# File 'lib/rumeme/sms_interface.rb', line 114 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) raise BadServerResponse.new('error during sending messages') if response_code != 100 end |