Class: Clockwork::XML::SMS
- Inherits:
-
Object
- Object
- Clockwork::XML::SMS
- Defined in:
- lib/clockwork/xml/sms.rb
Overview
XML building and parsing for sending SMS messages.
Class Method Summary collapse
-
.build_multiple(collection) ⇒ string
Build the XML data to send multiple SMS messages using the XML API.
-
.build_single(sms) ⇒ string
Build the XML data to send a single SMS using the XML API.
-
.parse_multiple(collection, http_response) ⇒ array
Parse the XML data from a multiple SMS response from the XML API.
-
.parse_single(sms, http_response) ⇒ Clockwork::SMS::Response
Parse the XML data from a single SMS response from the XML API.
Class Method Details
.build_multiple(collection) ⇒ string
Build the XML data to send multiple SMS messages using the XML API.
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..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.
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.
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. = collection.[i] if sms_response.css('ErrDesc').empty? response.success = true response. = 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.
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. = sms if http_response.code.to_i == 200 doc = Nokogiri.parse( http_response.body ) if doc.css('ErrDesc').empty? response.success = true response. = 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 |