Class: Mblox::Sms

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

Defined Under Namespace

Classes: BatchIdOutOfRangeError, InvalidMessageError, InvalidPhoneNumberError, InvalidSenderIdError, MessageTooLongError

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 MessageTooLongError, "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.



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

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.



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

def message
  @message
end

#phoneObject (readonly)

Returns the value of attribute phone.



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

def phone
  @phone
end

Class Method Details

.httpObject



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

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

.requestObject



114
115
116
117
118
119
# File 'lib/mblox/sms.rb', line 114

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

.urlObject



108
109
110
# File 'lib/mblox/sms.rb', line 108

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

Instance Method Details

#sendObject



51
52
53
# File 'lib/mblox/sms.rb', line 51

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

#send_from(sender_id, service_id = nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/mblox/sms.rb', line 42

def send_from(sender_id, service_id=nil)
  raise InvalidSenderIdError, "You can only send from a 5-digit shortcode" unless Mblox.is_a_five_digit_number?(sender_id)
  @sender_id = sender_id.to_i.to_s
  unless service_id.nil?
    raise InvalidSenderIdError, "You can only send using a 5-digit service ID.  Leave out the 2nd argument of send_from to use the globally configured '#{Mblox.config.service_id}'" unless Mblox.is_a_five_digit_number?(service_id)
    @service_id = service_id.to_i.to_s
  end
end