Sha256: c0779bffd0a3216ce2e10fe76fdae2bdbd8ee6da26eb458ee89bda376af87118

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require 'active_model/serialization'
require 'active_model/serializers/xml.rb'
module Mblox
  class SmsReceipt
    attr_reader :batch_id, :subscriber_number, :timestamp, :msg_reference, :status, :reason
    def initialize(xml)
      data = Hash.from_xml(xml)['NotificationService']
      raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' node, but was #{xml}" if data.blank?

      data = data['NotificationList']
      raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' node, but was #{xml}" if data.blank?

      data = data['Notification']
      raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' -> 'Notification' node, but was #{xml}" if data.blank?

      @batch_id = data['BatchID'].to_i

      data = data['Subscriber']
      raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' -> 'Notification' -> 'Subscriber' node, but was #{xml}" if data.blank?

      @subscriber_number = data['SubscriberNumber']
      @subscriber_number = @subscriber_number[1..-1] if '1' == @subscriber_number[0]

      unless data['TimeStamp'].blank?
        @timestamp = begin
          Time.strptime("#{data['TimeStamp']}+0000", '%Y%m%d%H%M%z')
        rescue ArgumentError
          nil
        end
      end

      @timestamp = @timestamp.to_datetime if @timestamp

      @msg_reference = data['MsgReference']
      @status = data['Status']
      @reason = data['Reason'].blank? ? nil : data['Reason'].to_i
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mblox-0.2.5 lib/mblox/sms_receipt.rb