Class: Clockwork::XML::SMS

Inherits:
Object
  • Object
show all
Defined in:
lib/clockwork/xml/sms.rb

Overview

XML building and parsing for sending SMS messages.

Author:

  • James Inman <james@mediaburst.co.uk>

Class Method Summary collapse

Class Method Details

.build_multiple(collection) ⇒ string

Build the XML data to send multiple SMS messages using the XML API.

Parameters:

Returns:

  • (string)

    XML data



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clockwork/xml/sms.rb', line 33

def self.build_multiple collection
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.Message {
      if collection.api.api_key
        xml.Key collection.api.api_key
      else
        xml.Username collection.api.username
        xml.Password collection.api.password                
      end
      collection.messages.each do |sms|
        xml.SMS {
          sms.translated_attributes.each do |k, v|
            xml.send "#{k}", v
          end
        }
      end
    }
  end
  builder.to_xml
end

.build_single(sms) ⇒ string

Build the XML data to send a single SMS using the XML API.

Parameters:

Returns:

  • (string)

    XML data



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clockwork/xml/sms.rb', line 11

def self.build_single sms
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.Message {
      if sms.api.api_key
        xml.Key sms.api.api_key
      else
        xml.Username sms.api.username
        xml.Password sms.api.password                
      end
      xml.SMS {
        sms.translated_attributes.each do |k, v|
          xml.send "#{k}", v
        end
      }
    }
  end
  builder.to_xml
end

.parse_multiple(collection, http_response) ⇒ array

Parse the XML data from a multiple SMS response from the XML API.

Parameters:

  • collection (Clockwork::MessageCollection)

    Instance of Clockwork::SMS

  • http_response (Net::HTTPResponse)

    Instance of Net:HTTPResponse

Returns:

  • (array)

    Array of Clockwork::SMS::Response objects relating to the messages

Raises:

  • Clockwork:HTTPError - if a connection to the Clockwork API cannot be made



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/clockwork/xml/sms.rb', line 85

def self.parse_multiple collection, http_response
  responses = []
  
  if http_response.code.to_i == 200
    doc = Nokogiri.parse( http_response.body )
  else
    raise Clockwork::Error::HTTP, "Could not connect to the Clockwork API to send SMS."
  end
  
  doc.css('SMS_Resp').each_with_index do |sms_response, i|
    response = Clockwork::SMS::Response.new
    response.message = collection.messages[i]
    if sms_response.css('ErrDesc').empty?
      response.success = true
      response.message_id = sms_response.css('MessageID').inner_html
    else
      response.success = false
      response.error_code = sms_response.css('ErrNo').inner_html.to_i
      response.error_description = sms_response.css('ErrDesc').inner_html
    end
    responses[sms_response.css('WrapperID').inner_html.to_i] = response
  end
  
  responses
end

.parse_single(sms, http_response) ⇒ Clockwork::SMS::Response

Parse the XML data from a single SMS response from the XML API.

Parameters:

  • sms (Clockwork::SMS)

    Instance of Clockwork::SMS

  • http_response (Net::HTTPResponse)

    Instance of Net:HTTPResponse

Returns:

Raises:

  • Clockwork:HTTPError - if a connection to the Clockwork API cannot be made



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/clockwork/xml/sms.rb', line 59

def self.parse_single sms, http_response
  response = Clockwork::SMS::Response.new
  response.message = sms
  
  if http_response.code.to_i == 200
    doc = Nokogiri.parse( http_response.body )
    if doc.css('ErrDesc').empty?
      response.success = true
      response.message_id = doc.css('MessageID').inner_html
    else
      response.success = false
      response.error_code = doc.css('ErrNo').inner_html.to_i
      response.error_description = doc.css('ErrDesc').inner_html
    end
  else
    raise Clockwork::Error::HTTP, "Could not connect to the Clockwork API to send SMS."
  end
  
  response        
end