Class: Mblox::Sms

Inherits:
Object
  • Object
show all
Defined in:
lib/mblox/sms.rb

Defined Under Namespace

Classes: BatchIdOutOfRangeError, InvalidMessageError, InvalidPhoneNumberError, InvalidSenderIdError

Constant Summary collapse

MAX_LENGTH =
160
MAX_SECTION_LENGTH =
MAX_LENGTH - "(MSG XXX/XXX): ".size
"~\`!\"#\$\%&'\(\)*+,-.\/:;<=>?@_£¤¥§¿i¡ÄÅÆÇÉÑÖØÜßáäåæèéìñòöøóùüú\n\r\tí "
ILLEGAL_CHARACTERS =
/([^a-zA-Z0-9#{LEGAL_CHARACTERS}\\])/
ON_MESSAGE_TOO_LONG_HANDLER =
{
  :raise_error => Proc.new { raise InvalidMessageError, "Message cannot be longer than #{MAX_LENGTH} characters" },
  :truncate => Proc.new { |message| Mblox.log "Truncating message due to length.  Message was: \"#{message}\" but will now be \"#{message = message[0,MAX_LENGTH]}\""; [message] },
  :split => Proc.new { |message| split_message(message) }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phone, message, batch_id = nil) ⇒ Sms

Returns a new instance of Sms.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mblox/sms.rb', line 27

def initialize(phone, message, batch_id=nil)
  phone = phone.to_s
  raise InvalidPhoneNumberError, "Phone number must be ten digits" unless /\A[0-9]{10}\z/.match(phone)
  raise InvalidPhoneNumberError, "Phone number cannot begin with 0 or 1" if ['0','1'].include?(phone[0].to_s)
  raise InvalidMessageError, "Message cannot be blank" if message.empty?
  illegal_characters = ILLEGAL_CHARACTERS.match(message).to_a
  raise InvalidMessageError, "Message cannot contain the following special characters: #{illegal_characters.uniq.join(', ')}" unless illegal_characters.size.zero?
  Mblox.log "WARNING: Some characters may be lost because the message must be broken into at least 1000 sections" if message.size > (999 * MAX_SECTION_LENGTH)
  @message = (message.size > MAX_LENGTH) ? ON_MESSAGE_TOO_LONG_HANDLER[Mblox.config.on_message_too_long].call(message) : [message.dup]
  @phone = "1#{phone}"
  raise BatchIdOutOfRangeError, "batch_id must be in the range 1 to #{MAX_BATCH_ID}.  The batch_id specified (#{batch_id}) is out of range." if !batch_id.blank? && (MAX_BATCH_ID < batch_id.to_i)
  @batch_id = batch_id.to_i unless batch_id.blank?
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



19
20
21
# File 'lib/mblox/sms.rb', line 19

def message
  @message
end

#phoneObject (readonly)

Returns the value of attribute phone.



19
20
21
# File 'lib/mblox/sms.rb', line 19

def phone
  @phone
end

Class Method Details

.httpObject



106
107
108
# File 'lib/mblox/sms.rb', line 106

def http
  @http ||= Net::HTTP.new(url.host, url.port)
end

.requestObject



109
110
111
112
113
114
# File 'lib/mblox/sms.rb', line 109

def request
  return @request if @request
  @request = Net::HTTP::Post.new(url.request_uri)
  @request.content_type = 'text/xml'
  @request
end

.urlObject



103
104
105
# File 'lib/mblox/sms.rb', line 103

def url
  @url ||= URI.parse(URI.escape(Mblox.config.outbound_url))
end

Instance Method Details

#sendObject



46
47
48
# File 'lib/mblox/sms.rb', line 46

def send
  @message.collect { |message| commit build(message) }
end

#send_from(_) ⇒ Object



41
42
43
44
# File 'lib/mblox/sms.rb', line 41

def send_from(_)
  raise InvalidSenderIdError, "You can only send from a 5-digit shortcode" unless ((_.is_a?(Fixnum) || _.is_a?(String)) && 5 == _.to_i.to_s.size)
  @sender_id = _.to_i.to_s
end