Sha256: dfb7ad07f0a1a4795205b1ddd441dc7f0e09bb20cf360ab5467322e14a87beda

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

require 'open-uri'
require 'nokogiri'

module Qa::Authorities
  class Oclcts < WebServiceBase
  
    SRU_SERVER_CONFIG = YAML.load_file(Rails.root.join("config", "oclcts-authorities.yml"))

    def initialize *args
      super
      @sub_authority ||= args.first
      raise "No sub-authority provided" if sub_authority.nil?
    end

    def sub_authorities
      SRU_SERVER_CONFIG["authorities"].map { |sub_authority| sub_authority[0] }
    end

    def search q
      get_raw_response("prefix-query", q)
      r = Array.new
      raw_response.xpath('sru:searchRetrieveResponse/sru:records/sru:record/sru:recordData', 'sru' => 'http://www.loc.gov/zing/srw/').each do |record|
        r.append({"id" => record.xpath('Zthes/term/termId').first.content, "label" => record.xpath('Zthes/term/termName').first.content})
      end
      r
    end

    def find id
      get_raw_response("id-lookup", id)
      parse_full_record(id)
    end

    private

    def parse_full_record id
      a = {}
      zthes_record = raw_response.xpath("sru:searchRetrieveResponse/sru:records/sru:record/sru:recordData/Zthes/term[termId='#{id}']", 'sru' => 'http://www.loc.gov/zing/srw/')
      zthes_record.children.each do |child|
        if (child.is_a? Nokogiri::XML::Element) && (!child.children.nil?) && (child.children.size == 1) && (child.children.first.is_a? Nokogiri::XML::Text)
          a[child.name] = child.children.first.to_s
        end
      end
      a
    end

    def get_raw_response query_type, id
      query_url = SRU_SERVER_CONFIG["url-pattern"][query_type].gsub("{query}", id).gsub("{id}", id).gsub("{authority-id}", sub_authority)
      @raw_response = Nokogiri::XML(open(query_url))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qa-0.4.2 lib/qa/authorities/oclcts.rb
qa-0.4.1 lib/qa/authorities/oclcts.rb
qa-0.4.0 lib/qa/authorities/oclcts.rb