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

Class Method Summary collapse

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.



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.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 = %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 message, max_len
  return [message, nil] if message.length < max_len
  pattern = /\s\.,!;:-\)/
  index = message[0..max_len].rindex(pattern) || max_len
  [message[0..index], message[index+1 .. -1]]
end

.split_message(message) ⇒ Object



146
147
148
149
150
151
# File 'lib/rumeme/sms_interface.rb', line 146

def split_message message
  messages = split_message_internal message
  message_index = 1
  total_messages = messages.size
  ["#{messages[0]}...(1/#{total_messages})"].concat(messages[1..-1].map {|msg| "(#{message_index+=1}/#{total_messages})#{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 split_message_internal message
  list = []
  sizes = Enumerator.new {|yielder| yielder << 152; yielder << 155 while true}

  until message.nil? do
    head, message = head_tail_split(message, 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.

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
# File 'lib/rumeme/sms_interface.rb', line 48

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



75
76
77
# File 'lib/rumeme/sms_interface.rb', line 75

def change_password
  raise 'Not Implemented'
end

#check_repliesObject

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

  messages
end

#clear_messagesObject

Clear all the messages from the list.



60
61
62
# File 'lib/rumeme/sms_interface.rb', line 60

def clear_messages
  @message_list.clear
end

#confirm_replies_receivedObject

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_remainingObject

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_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
    $2.to_i
  else
    raise BadServerResponse.new "cant parse response: #{response_message}"
  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_messagesObject

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 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)
  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

Raises:



120
121
122
# File 'lib/rumeme/sms_interface.rb', line 120

def send_messages!
  raise BadServerResponse.new('error during sending messages') unless send_messages
end