Class: SMSBox::XMLResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/sms_box/xml_response.rb

Direct Known Subclasses

WebsendResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXMLResponse

Returns a new instance of XMLResponse.



21
22
23
24
25
# File 'lib/sms_box/xml_response.rb', line 21

def initialize
  if self.instance_of? XMLResponse
    raise 'XMLResponse needs to be subclassed'
  end
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



8
9
10
# File 'lib/sms_box/xml_response.rb', line 8

def command
  @command
end

#command_dataObject

Returns the value of attribute command_data.



11
12
13
# File 'lib/sms_box/xml_response.rb', line 11

def command_data
  @command_data
end

#errorObject

Returns the value of attribute error.



10
11
12
# File 'lib/sms_box/xml_response.rb', line 10

def error
  @error
end

#requestObject

Returns the value of attribute request.



7
8
9
# File 'lib/sms_box/xml_response.rb', line 7

def request
  @request
end

#requestUidObject

Returns the value of attribute requestUid.



9
10
11
# File 'lib/sms_box/xml_response.rb', line 9

def requestUid
  @requestUid
end

Class Method Details

.from_xml(xml_string) ⇒ Object



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

def self.from_xml(xml_string)
  doc = Nokogiri::XML(xml_string)
  command = doc.xpath('//SMSBoxXMLReply/command')
  command_name = command.attr('name').text
  res = instantize(command_name)
  res.command = command_name
  res.command_data = command
  res.requestUid = doc.xpath('//SMSBoxXMLReply/requestUid').first.text
  error = doc.xpath('//SMSBoxXMLReply/error')
  unless error.empty?
    res.error = error.attr('type').text
  end
  res
end

.instantize(command) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sms_box/xml_response.rb', line 42

def self.instantize(command)
  klass = nil
  begin
    class_name = "SMSBox::#{command.downcase.camelize}Response"
    klass = class_name.camelize.constantize
  rescue NameError => e
    raise NotImplementedError.new("'#{command}' not implemented")
  rescue Exception => e
    raise "'#{command}' could not be instantiated"
  end

  unless klass < XMLResponse
    raise "'#{command}' not a SMSBox::XMLResponse"
  end
  klass.new
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/sms_box/xml_response.rb', line 13

def error?
  error.present?
end

#success?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/sms_box/xml_response.rb', line 17

def success?
  not error?
end