class Yardi::Utils::RequestGenerator
Generate a SOAP request for a specific action and sections
Constants
- URL_BASE
Attributes
interface[R]
sections[R]
soap_action[R]
Public Class Methods
new(soap_action, sections, interface)
click to toggle source
@param soap_action
[String] the action to request from Yardi
. @param sections [Array<RequestSection>] the section generators that will
be used to generate the body of the XML request
# File lib/yardi/utils/request_generator.rb, line 13 def initialize(soap_action, sections, interface) @soap_action = soap_action @sections = sections @interface = interface end
Public Instance Methods
body()
click to toggle source
@return [String] the XML request for the specified action and sections
# File lib/yardi/utils/request_generator.rb, line 20 def body xml_env = build_envelope do |xml_builder| # This section is the only one inside of the itf namespace xml_builder['itf'].send(soap_action) do sections[:soap_body].each do |section| section.generate(xml_builder) end xml_builder['itf'].XmlDoc 'REPLACE_ME' if xml_doc_sections? end end # This is a hack to handle Nokogiri's behavior with namespaces and still # build XML that adheres to Yardi's spec. # https://github.com/sparklemotion/nokogiri/issues/425 # When inserting a child node, it will automatically inherit its # parent's namespace. Unfortunately, Yardi only wants a select bunch of # the parent nodes to be namespaced and the bulk of the child nodes # (everything inside of the namespaced <itf:XmlDoc> node) to be without # a namespace. In order to make this happen, we build the bulk of the # xml inside #xml_doc_body, then do a string replace to plop it into # the properly namespaced parent node. xml_doc_sections? ? xml_env.sub('REPLACE_ME', xml_doc_body) : xml_env end
build_envelope(&block)
click to toggle source
# File lib/yardi/utils/request_generator.rb, line 56 def build_envelope(&block) Nokogiri::XML::Builder.new do |xml| xml.Envelope(envelope) do xml.parent.namespace = xml.parent.namespace_definitions.first xml['soapenv'].Body(&block) end end.to_xml end
headers()
click to toggle source
# File lib/yardi/utils/request_generator.rb, line 65 def headers { 'Content-Type' => 'text/xml; charset=utf-8', 'SOAPAction' => "#{URL_BASE}/#{interface}/#{soap_action}" } end
xml_doc_body()
click to toggle source
# File lib/yardi/utils/request_generator.rb, line 45 def xml_doc_body return nil if sections[:xml_doc].nil? body_fragment = Nokogiri::XML::DocumentFragment.parse('') Nokogiri::XML::Builder.with(body_fragment) do |xml_builder| sections[:xml_doc].each do |section| section.generate(xml_builder) end end body_fragment.to_xml end
Private Instance Methods
envelope()
click to toggle source
# File lib/yardi/utils/request_generator.rb, line 80 def envelope { 'xmlns:soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/', 'xmlns:itf' => "#{URL_BASE}/#{interface}" }.freeze end
xml_doc_sections?()
click to toggle source
# File lib/yardi/utils/request_generator.rb, line 76 def xml_doc_sections? !sections[:xml_doc].empty? end