Class: Mblox::Sms

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

Defined Under Namespace

Classes: BatchIdOutOfRangeError, InvalidSenderIdError

Constant Summary collapse

"~\`!\"#\$\%&'\(\)*+,-.\/:;<=>?@_£¤¥§¿i¡ÄÅÃÆÇÉÑÖØÜßâáäåãæçèéìíñòöøóùüú\n\r\t ©"
ILLEGAL_CHARACTERS =
/([^a-zA-Z0-9#{LEGAL_CHARACTERS}\\])/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Sms.

Raises:

  • (InvalidMessageError)


16
17
18
19
20
21
22
# File 'lib/mblox/sms.rb', line 16

def initialize(phone, message, batch_id=nil)
  super(phone, message)
  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?
  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

Class Method Details

.httpObject



75
76
77
# File 'lib/mblox/sms.rb', line 75

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

.requestObject



78
79
80
81
82
83
# File 'lib/mblox/sms.rb', line 78

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

.urlObject



72
73
74
# File 'lib/mblox/sms.rb', line 72

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

Instance Method Details

#sendObject



33
34
35
# File 'lib/mblox/sms.rb', line 33

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

#send_from(sender_id, service_id = nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/mblox/sms.rb', line 24

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