class MooMoo::OpenSRSXMLBuilder

Public Class Methods

new(app, *args) click to toggle source
Calls superclass method
# File lib/moo_moo/middleware/open_srs_xml_builder.rb, line 6
def initialize(app, *args)
  @action = args[0]
  @object = args[1]
  @params = args[2] || {}
  @key    = args[3]
  @user   = args[4]
  super(app)
end

Public Instance Methods

call(env) click to toggle source
# File lib/moo_moo/middleware/open_srs_xml_builder.rb, line 15
def call(env)
  env[:body] = build_command.to_s
  env[:request_headers] = {
    'Content-Type' => 'text/xml',
    'X-Username' => @user,
    'X-Signature' => signature(env[:body]),
    'Content-Length' => env[:body].size.to_s
    }.merge(env[:request_headers])
  @app.call(env)
end

Private Instance Methods

build_child(elem, coll) click to toggle source

Adds XML child elements to the specified XML element for a given collection

Required

* <tt>:elem</tt> - XML element to add the child nodes to
* <tt>:coll</tt> - collection that will be added as XML child elements
# File lib/moo_moo/middleware/open_srs_xml_builder.rb, line 66
def build_child(elem, coll)
  if coll.is_a?(Hash)
    elem = elem.add_element("dt_assoc")
    coll.each do |key, val|
      child = elem.add_element('item', {'key' => key})
      build_child(child, val)
    end
  elsif coll.is_a?(Array)
    elem = elem.add_element("dt_array")
    coll.each_with_index do |val, key|
      child = elem.add_element('item', {'key' => key})
      build_child(child, val)
    end
  else
    elem.text = coll
  end
end
build_command() click to toggle source

Builds an XML string of the command which can be sent to OpenSRS

# File lib/moo_moo/middleware/open_srs_xml_builder.rb, line 29
    def build_command
      xml = <<-XML
      <?xml version='1.0' encoding='UTF-8' standalone='no' ?>
      <!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
      <OPS_envelope>
        <header>
          <version>0.9</version>
        </header>
        <body>
          <data_block>
            <dt_assoc>
              <item key="protocol">XCP</item>
              <item key="action">GET_BALANCE</item>
              <item key="object">BALANCE</item>
            </dt_assoc>
          </data_block>
        </body>
      </OPS_envelope>
      XML

      doc = REXML::Document.new(xml)
      doc.root.elements["body/data_block/dt_assoc/item[@key='action']"].text = @action
      doc.root.elements["body/data_block/dt_assoc/item[@key='object']"].text = @object

      @params.each do |key, value|
        elem = doc.root.elements["body/data_block/dt_assoc"].add_element('item', {'key' => key})
        build_child(elem, value)
      end

      doc
    end
signature(content) click to toggle source
# File lib/moo_moo/middleware/open_srs_xml_builder.rb, line 84
def signature(content)
  Digest::MD5.hexdigest(
    Digest::MD5.hexdigest(
      content + @key
    ) + @key
  )
end