Module: Zuora::Utils::Envelope
- Defined in:
- lib/zuora/utils/envelope.rb
Constant Summary collapse
- CUSTOM_FIELD_STRING =
'__c'.freeze
Class Method Summary collapse
-
.authenticated_xml(token, &body) ⇒ Nokogiri::XML::Builder
Takes a body, and returns an envelope with session token merged in.
-
.build_field(builder, namespace, key, value) ⇒ Object
Builds one field using key and value.
-
.build_fields(builder, namespace, object = {}) ⇒ Object
Builds multiple fields in given object.
-
.build_objects(builder, type, objects) ⇒ Object
Builds multiple objects.
-
.from_zuora_key(key) ⇒ Symbol
Converts from Zuora key format to Ruby format.
-
.to_zuora_key(key) ⇒ Symbol
Converts from Ruby to Zuora key format.
-
.transform_sym(sym, operation) ⇒ Object
Transforms symbol as if were a string, using operation.
- .xml(header, body) ⇒ Object
Class Method Details
.authenticated_xml(token, &body) ⇒ Nokogiri::XML::Builder
Takes a body, and returns an envelope with session token merged in
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/zuora/utils/envelope.rb', line 21 def self.authenticated_xml(token, &body) = 'Session token not set. Did you call authenticate?' fail unless token.present? header = lambda do |builder| builder[:api].SessionHeader { builder[:api].session(token) } builder end xml(header, body) end |
.build_field(builder, namespace, key, value) ⇒ Object
Builds one field using key and value. Treats value differently:
- Hash: recursively builds fields
- ZObject: builds a nested z object, building fields inside
- Nil: does nothing
- Else: builds the field node
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/zuora/utils/envelope.rb', line 43 def self.build_field(builder, namespace, key, value) zuora_field_name = to_zuora_key(key) build_fields_thunk = -> { build_fields builder, namespace, value } case value when Hash builder[namespace].send(zuora_field_name) { build_fields_thunk[] } when Zuora::Soap::ZObject zuora_type = to_zuora_key(value.type) xsi = { 'xsi:type' => "obj:#{zuora_type}" } builder[:api].send(zuora_field_name) do builder[:api].send(zuora_type, xsi) { build_fields_thunk[] } end when NilClass else builder[namespace].send(zuora_field_name, value) end end |
.build_fields(builder, namespace, object = {}) ⇒ Object
Builds multiple fields in given object
66 67 68 69 70 |
# File 'lib/zuora/utils/envelope.rb', line 66 def self.build_fields(builder, namespace, object = {}) object.each do |key, value| build_field builder, namespace, key, value end end |
.build_objects(builder, type, objects) ⇒ Object
Builds multiple objects
77 78 79 80 81 82 83 |
# File 'lib/zuora/utils/envelope.rb', line 77 def self.build_objects(builder, type, objects) objects.each do |object| builder[:api].zObjects('xsi:type' => "obj:#{type}") do Zuora::Utils::Envelope.build_fields(builder, :obj, object) end end end |
.from_zuora_key(key) ⇒ Symbol
Converts from Zuora key format to Ruby format.
106 107 108 |
# File 'lib/zuora/utils/envelope.rb', line 106 def self.from_zuora_key(key) transform_sym key, :lower end |
.to_zuora_key(key) ⇒ Symbol
Converts from Ruby to Zuora key format.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/zuora/utils/envelope.rb', line 88 def self.to_zuora_key(key) custom_field_matcher = Regexp.new CUSTOM_FIELD_STRING matches = custom_field_matcher.match(key.to_s) suffix = '' if matches key = key.to_s[0...-3].to_sym suffix = CUSTOM_FIELD_STRING end key = transform_sym key, :camelize [key.to_s, suffix].join.to_sym end |
.transform_sym(sym, operation) ⇒ Object
Transforms symbol as if were a string, using operation. Helper method for building specific symbol converters.
114 115 116 |
# File 'lib/zuora/utils/envelope.rb', line 114 def self.transform_sym(sym, operation) sym.to_s.send(operation).to_sym end |
.xml(header, body) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/zuora/utils/envelope.rb', line 9 def self.xml(header, body) Nokogiri::XML::Builder.new do |builder| builder[:soapenv].Envelope(Zuora::NAMESPACES) do builder[:soapenv].Header { header.call builder } if header builder[:soapenv].Body { body.call builder } if body end end end |