Sha256: 4dd8f92cf4b78bf21fb6b0bf89e4f14d4ec463b8974933f58f7c57a11e399ee2

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module MooMoo
  class ParseOpenSRS < Faraday::Response::Middleware

    def on_complete(env)
      env[:body] = parse_response(env[:body])
    end
    
    def response_values(env)
      {:status => env[:status], :headers => env[:response_headers], :body => env[:body]}
    end
    
  private
    
    # 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
    def parse_response(data)
      doc = REXML::Document.new(data)

      values = {}

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

      build_xml_hash(elements)
    end

    # Builds a hash from a collection of XML elements
    #
    # ==== Required
    #  * <tt>elements</tt> - collection of elemenents
    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
moo_moo-0.2.0 lib/moo_moo/middleware/parse_open_srs.rb