class MooMoo::ParseOpenSRS

Public Instance Methods

on_complete(env) click to toggle source
# File lib/moo_moo/middleware/parse_open_srs.rb, line 4
def on_complete(env)
  env[:body] = parse_response(env[:body])
end
response_values(env) click to toggle source
# File lib/moo_moo/middleware/parse_open_srs.rb, line 8
def response_values(env)
  {:status => env[:status], :headers => env[:response_headers], :body => env[:body]}
end

Private Instance Methods

build_xml_hash(elements) click to toggle source

Builds a hash from a collection of XML elements

Required

* <tt>elements</tt> - collection of elemenents
# File lib/moo_moo/middleware/parse_open_srs.rb, line 35
def build_xml_hash(elements)
  data_hash = {}

  elements.each do |elem|
    key = elem.attributes['key']

    if elem.elements.size > 0
      if key.nil?
        data_hash.merge!(build_xml_hash(elem.elements))
      else
        data_hash[key] = build_xml_hash(elem.elements)
      end
    else
      data_hash[key] = elem.text unless key.nil?
    end
  end

  data_hash
end
parse_response(data) click to toggle source

Parses an XML response from the OpenSRS registry and generates a hash containing all of the data. Elements with child elements are converted into hashes themselves, with the :element_text entry containing any raw text

Required

* <tt>data</tt> - data of the response
# File lib/moo_moo/middleware/parse_open_srs.rb, line 21
def parse_response(data)
  doc = REXML::Document.new(data)

  elements = doc.elements["/OPS_envelope/body/data_block/dt_assoc"].select { |item|
    item.is_a? REXML::Element
  }

  build_xml_hash(elements)
end