Class: ApricotEatsGorilla

Inherits:
Object
  • Object
show all
Defined in:
lib/apricoteatsgorilla/apricoteatsgorilla.rb

Overview

ApricotEatsGorilla

Apricot eats Gorilla is a SOAP communication helper. It translates between SOAP messages (XML) and Ruby Hashes and comes with some additional helpers for working with SOAP services.

Constant Summary collapse

SOAPDateTimeFormat =

SOAP dateTime format.

"%Y-%m-%dT%H:%M:%S"
SOAPDateTimeRegexp =

SOAP dateTime Regexp.

/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
SOAPNamespace =

SOAP namespaces by SOAP version.

{
  1 => "http://schemas.xmlsoap.org/soap/envelope/",
  2 => "http://www.w3.org/2003/05/soap-envelope"
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.disable_hash_keys_to_snake_caseObject

Flag to disable conversion of Hash keys to snake_case.



32
33
34
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 32

def disable_hash_keys_to_snake_case
  @disable_hash_keys_to_snake_case
end

.disable_hash_keys_to_symbolsObject

Flag to disable conversion of Hash keys to Symbols.



35
36
37
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 35

def disable_hash_keys_to_symbols
  @disable_hash_keys_to_symbols
end

.disable_tag_names_to_lower_camel_caseObject

Flag to disable conversion of XML tags to lowerCamelCase.



29
30
31
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 29

def disable_tag_names_to_lower_camel_case
  @disable_tag_names_to_lower_camel_case
end

.nodes_to_namespaceObject

Hash of namespaces (keys) and XML nodes (values) to apply these namespaces to.



39
40
41
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 39

def nodes_to_namespace
  @nodes_to_namespace
end

.sort_keysObject

Flag to enable sorting of Hash keys.



26
27
28
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 26

def sort_keys
  @sort_keys
end

Class Method Details

.[](source, root_node = nil) ⇒ Object

Shortcut method for translating between XML Strings and Ruby Hashes. Delegates to xml_to_hash in case source is a String or delegates to hash_to_xml in case source is a Hash. Returns nil otherwise.



44
45
46
47
48
49
50
51
52
53
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 44

def [](source, root_node = nil)
  case source
    when String
      xml_to_hash(source, root_node)
    when Hash
      hash_to_xml(source)
    else
      nil
  end
end

.hash_to_xml(hash) ⇒ Object

Translates a given Ruby hash into an XML String.

Examples

hash = { :apricot => { :eats => "Gorilla" } }
ApricotEatsGorilla.hash_to_xml(hash)

# => "<apricot><eats>Gorilla</eats></apricot>"

hash = { :apricot => { :eats => ["Gorillas", "Snakes"] } }
ApricotEatsGorilla.hash_to_xml(hash)

# => "<apricot><eats>Gorillas</eats><eats>Snakes</eats></apricot>"


116
117
118
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 116

def hash_to_xml(hash)
  nested_data_to_xml(hash.keys.first, hash.values.first)
end

.setup {|_self| ... } ⇒ Object

Yields this class to a given block for wrapping the setup of multiple flags at once.

Yields:

  • (_self)

Yield Parameters:



57
58
59
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 57

def setup
  yield self if block_given?
end

.soap_envelope(namespaces = {}, version = 1) ⇒ Object

Builds a SOAP request envelope and includes the content of a given block into the envelope body. Accepts a Hash of additional namespaces to set. Also accepts an optional version to specify the SOAP envelope namespace to use by SOAP version.

Examples

ApricotEatsGorilla.soap_envelope do
  "<apricot><eats>Gorilla</eats></apricot>"
end

# => '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
# =>    <env:Body>
# =>      <apricot><eats>Gorilla</eats></apricot>
# =>    </env:Body>
# =>  </env:Envelope>'

ApricotEatsGorilla.soap_envelope(:wsdl => "http://example.com") { "pureText" }

# => '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://example.com">
# =>    <env:Body>
# =>      pureText
# =>    </env:Body>
# =>  </env:Envelope>'


144
145
146
147
148
149
150
151
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 144

def soap_envelope(namespaces = {}, version = 1)
  namespaces[:env] = SOAPNamespace[version] unless
    namespaces[:env] || SOAPNamespace[version].nil?

  xml_node("env:Envelope", namespaces) do
    xml_node("env:Body") { yield if block_given? }
  end
end

.xml_to_hash(xml, root_node = nil) ⇒ Object

Translates a given xml String into a Ruby Hash.

Starts parsing at the XML root node by default. Accepts an optional root_node parameter for defining a custom root node to start parsing at using an XPath-Expression (Hpricot search).

Notice that both namespaces and attributes get removed and the root node itself won’t be included in the Hash.

Examples

xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
      <ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/">
        <return>
          <apricot>
            <eats>Gorilla</eats>
          </apricot>
        </return>
      </ns2:authenticateResponse>
    </soap:Body>
  </soap:Envelope>'

ApricotEatsGorilla.xml_to_hash(xml)
# => { :body => { :authenticate_response => { :return => { :apricot => { :eats => "Gorilla" } } } } }

ApricotEatsGorilla.xml_to_hash(xml, "//return")
# => { :apricot => { :eats => "Gorilla" } }


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/apricoteatsgorilla/apricoteatsgorilla.rb', line 89

def xml_to_hash(xml, root_node = nil)
  doc = Hpricot.XML remove_whitespace(xml)
  root = root_node ? doc.search(root_node) : doc.root

  return nil if root.nil? || (root.respond_to?(:size) && root.size == 0)
  if !root.respond_to? :each
    xml_node_to_hash(root)
  elsif root.size == 1
    single_xml_root_node_to_hash(root)
  else
    multiple_xml_root_nodes_to_hash(root)
  end
end