Class: Yardi::Utils::RequestGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/yardi/utils/request_generator.rb

Overview

Generate a SOAP request for a specific action and sections

Constant Summary collapse

URL_BASE =
'http://tempuri.org/YSI.Interfaces.WebServices'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(soap_action, sections, interface) ⇒ RequestGenerator

Returns a new instance of RequestGenerator.

Parameters:

  • soap_action (String)

    the action to request from Yardi.

  • sections (Array<RequestSection>)

    the section generators that will be used to generate the body of the XML request



13
14
15
16
17
# File 'lib/yardi/utils/request_generator.rb', line 13

def initialize(soap_action, sections, interface)
  @soap_action = soap_action
  @sections = sections
  @interface = interface
end

Instance Method Details

#bodyString

Returns the XML request for the specified action and sections.

Returns:

  • (String)

    the XML request for the specified action and sections



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/yardi/utils/request_generator.rb', line 57

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

#headersObject



66
67
68
69
70
71
# File 'lib/yardi/utils/request_generator.rb', line 66

def headers
  {
    'Content-Type' => 'text/xml; charset=utf-8',
    'SOAPAction' => "#{URL_BASE}/#{interface}/#{soap_action}"
  }
end

#xml_doc_bodyObject



45
46
47
48
49
50
51
52
53
54
55
# 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