Class: Mblox::SmsResponse

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

Defined Under Namespace

Classes: Result

Constant Summary collapse

ATTRIBUTES =
[:request, :result, :subscriber_result]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SmsResponse

Returns a new instance of SmsResponse.

Raises:

  • (::ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mblox/sms_response.rb', line 43

def initialize args
  args = args.symbolize_keys
  ATTRIBUTES.each do |attr|
    __send__("#{attr}=", args[attr])
    args.delete(attr)
  end
  raise ::ArgumentError, "Unrecognized attributes: #{args.inspect}" unless args.empty?

  wrong_type_fields = ATTRIBUTES.reject { |attr| __send__(attr).nil? ||  __send__(attr).is_a?(self.class::Result) }
  if 1 == wrong_type_fields.count
    raise ValidationError, "#{wrong_type_fields.first} must be of type Mblox::SmsResponse::Result"
  elsif wrong_type_fields.count > 1
    raise ValidationError, "The following fields must be of type Mblox::SmsResponse::Result: #{wrong_type_fields.join(', ')}"
  end

  missing_fields = [:request, :result].reject { |attr| __send__(attr) }
  missing_fields << :subscriber_result if result && result.ok? && subscriber_result.nil?
  if 1 == missing_fields.count
    raise ValidationError, "#{missing_fields.first} cannot be blank"
  elsif missing_fields.count > 1
    raise ValidationError, "The following fields cannot be blank: #{missing_fields.join(', ')}"
  end
end

Class Method Details

.from_xml(xml) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mblox/sms_response.rb', line 79

def from_xml(xml)
  args = {}
  data = Mblox.from_xml(xml).xpath '//NotificationRequestResult'

  raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' node, but was #{xml}" if data.blank?
  header = data.xpath '//NotificationResultHeader'
  raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultHeader' node, but was #{xml}" if header.blank?
  args[:request] = Result.from_xml(header, :RequestResult)
  args[:request] = nil unless args[:request].valid?

  result_list = data.xpath '//NotificationResultList'
  raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultList' node, but was #{xml}" if result_list.blank?
  result_list = result_list.xpath '//NotificationResult'
  raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultList' -> 'NotificationResult' node, but was #{xml}" if result_list.blank?
  args[:result] = Result.from_xml(result_list, :NotificationResult)
  args[:result] = nil unless args[:result].valid?

  if args[:result].ok?
    result_list = result_list.xpath '//SubscriberResult'
    raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultList' -> 'NotificationResult' -> 'SubscriberResult' node, but was #{xml}" if result_list.blank?
    args[:subscriber_result] = Result.from_xml(result_list, :SubscriberResult)
    args[:subscriber_result] = nil unless args[:subscriber_result].valid?
  end
  new(args)
end

Instance Method Details

#ok?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/mblox/sms_response.rb', line 67

def ok?
  @request.ok? && @result.ok? && @subscriber_result.ok?
end

#unroutable?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/mblox/sms_response.rb', line 71

def unroutable?
  @request.ok? && @result.ok? && Result::UNROUTABLE == @subscriber_result
end