Class: CloudhubWrapper
- Inherits:
-
Object
- Object
- CloudhubWrapper
- Defined in:
- lib/cloudhub_wrapper.rb
Instance Attribute Summary collapse
-
#xml ⇒ Object
Returns the value of attribute xml.
Instance Method Summary collapse
- #build_xml(subscriber, channel, origin, schema) ⇒ Object
-
#initialize(endpoint, user, password) ⇒ CloudhubWrapper
constructor
A new instance of CloudhubWrapper.
- #post_xml ⇒ Object
Constructor Details
#initialize(endpoint, user, password) ⇒ CloudhubWrapper
Returns a new instance of CloudhubWrapper.
8 9 10 11 12 |
# File 'lib/cloudhub_wrapper.rb', line 8 def initialize(endpoint, user, password) @endpoint = endpoint @user = user @password = password end |
Instance Attribute Details
#xml ⇒ Object
Returns the value of attribute xml.
14 15 16 |
# File 'lib/cloudhub_wrapper.rb', line 14 def xml @xml end |
Instance Method Details
#build_xml(subscriber, channel, origin, schema) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cloudhub_wrapper.rb', line 16 def build_xml(subscriber, channel, origin, schema) address_elements = [ [:address, "addressLine"], [:suburb, "suburb"], [:postcode, "postalcode"], [:post_code, "postalcode"], [:state, "state"] ] contact_elements = [ [:first_name, "firstName"], [:last_name, "lastName"], [:email, "email"], [:mobile, "phone"], [:phone, "phone"] ] doc = REXML::Document.new(File.read(File.join(File.dirname(__FILE__), "..", "data", "default.xml"))) root = doc.root address_el = root.elements[1].elements["ns0:address"] contact_el = root.elements[1].elements["ns0:contact"] root.add_attributes({"interchangeDate" => Time.now.to_s, "xmlns:ns0" => schema}) root.elements["ns0:account"].add_attributes({"id" => subscriber.email, "creationDateTime" => Time.now.to_s, "xmlns:ns0" => schema}) contact_el.elements["ns0:channelContactPreferences"].children[1].add_attributes({"channel" => channel, "origin" => origin }) address_elements.each do |array| if subscriber.respond_to? array[0] el = REXML::Element.new("ns0:#{array[1]}", address_el) el.add_text(subscriber.send(array[0])) end end contact_elements.each do |array| if subscriber.respond_to? array[0] el = REXML::Element.new("ns0:#{array[1]}", contact_el) el.add_text(subscriber.send(array[0])) end end @xml = doc.to_s end |
#post_xml ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cloudhub_wrapper.rb', line 62 def post_xml if @xml uri = URI.parse(@endpoint) request = Net::HTTP::Post.new(uri.path) request.body = @xml request.basic_auth(@user, @password) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true response = http.request(request) else #throw an error of some description end end |