class Mblox::Sms

Public Class Methods

new(phone, message, batch_id=nil) click to toggle source
Calls superclass method
# File lib/mblox/sms.rb, line 14
def initialize(phone, message, batch_id=nil)
  super(phone, message)
  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

Private Class Methods

http() click to toggle source
# File lib/mblox/sms.rb, line 71
def http
  @http ||= Net::HTTP.new(url.host, url.port)
end
request() click to toggle source
# File lib/mblox/sms.rb, line 74
def request
  return @request if @request
  @request = Net::HTTP::Post.new(url.request_uri)
  @request.content_type = 'text/xml'
  @request
end
url() click to toggle source
# File lib/mblox/sms.rb, line 68
def url
  @url ||= URI.parse(URI.escape(Mblox.config.outbound_url))
end

Public Instance Methods

send() click to toggle source
# File lib/mblox/sms.rb, line 29
def send
  messages.collect { |message| commit build(message) }
end
send_from(sender_id, service_id=nil) click to toggle source
# File lib/mblox/sms.rb, line 20
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

Private Instance Methods

build(message) click to toggle source
# File lib/mblox/sms.rb, line 42
def build(message)
  builder = Builder::XmlMarkup.new
  builder.instruct!(:xml, :encoding => "ISO-8859-1")
  builder.NotificationRequest(:Version => "3.5") do |nr|
    nr.NotificationHeader do |nh|
      nh.PartnerName(Mblox.config.partner_name)
      nh.PartnerPassword(Mblox.config.password)
    end
    nr.NotificationList(:BatchID => @batch_id || 1) do |nl|
      nl.Notification(:SequenceNumber => 1, :MessageType => :SMS, :Format => :UTF8) do |n|
        n.Message do |m|
          m.cdata!(message)
        end
        n.Profile(Mblox.config.profile_id)
        n.SenderID(@sender_id || Mblox.config.sender_id, :Type => :Shortcode)
        n.Tariff(Mblox.config.tariff)
        n.Subscriber do |s|
          s.SubscriberNumber(@phone)
        end
        n.ServiceId(@service_id || Mblox.config.service_id)
      end
    end
  end
end
commit(request_body) click to toggle source
# File lib/mblox/sms.rb, line 33
def commit(request_body)
  SmsValidation.log "Sending SMS to Mblox:\n#{request_body}"
  request = self.class.request
  request.body = request_body
  response = self.class.http.start{ |http| http.request(request) }.body
  SmsValidation.log "Mblox responds with:\n#{response}"
  SmsResponse.from_xml(response)
end