Class: Rumeme::SmsInterface

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeSmsInterface

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.use_message_id
    @secure = cfg.secure

    @long_messages_processor = case cfg.long_messages_strategy
      when :send
        lambda {|message| [message]}
      when :cut
        lambda {|message| [message[0..159]]}
      when :split
        lambda {|message| SmsInterface.split_message message}
      else
        lambda {|message| 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.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
# File 'lib/rumeme/sms_interface.rb', line 51

def add_message args
  phone_number = self.class.strip_invalid(args[:phone_number]) #not good idea, modifying original args, from outer scope (antlypls)
  message = args[:message]

  raise ArgumentError.new("phone_number is empty") if phone_number.nil? || phone_number.empty?
  raise ArgumentError.new("message is empty") if message.nil? || message.empty?

  messages = process_long_message(message)
  @message_list.concat(messages.map{|msg| SmsMessage.new(args.merge({:message => msg, :phone_number => phone_number}))})
end

#change_passwordObject

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_repliesObject

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_message, response_code = post_data_to_server("CHECKREPLY2.0\r\n.\r\n")
  return if response_code != 150

  messages = response_message.split("\r\n")[1..-2].map{|message_line| SmsReply.parse(message_line)} # check @use_message_id
  confirm_replies_received if @replies_auto_confirm && messages.size > 0

  return messages
end

#clear_messagesObject

Clear all the messages from the list.



63
64
65
# File 'lib/rumeme/sms_interface.rb', line 63

def clear_messages
  @message_list.clear
end

#confirm_replies_receivedObject

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_remainingObject

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_message, response_code = post_data_to_server("MESSAGES\r\n.\r\n")

  if response_message =~ /^(\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: #{response_message}"
  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_messagesObject

Sends all the messages that have been added with the add_message command.

Raises:



114
115
116
117
118
119
120
# File 'lib/rumeme/sms_interface.rb', line 114

def send_messages
  post_string = @message_list.map(&:post_string).join
  text_buffer = "MESSAGES2.0\r\n#{post_string}.\r\n"
  response_message, response_code = post_data_to_server(text_buffer)

  raise BadServerResponse.new('error during sending messages') if response_code != 100
end