Sha256: ff8566792e0931c91ad4d12487d77ee702a8195478f1230a5e1e2d6686e8b760

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 KB

Contents

module Qa::Authorities
  class Loc::GenericAuthority < Base
    attr_reader :subauthority
    def initialize(subauthority)
      @subauthority = subauthority
    end

    include WebServiceBase

    def search q
      @raw_response = get_json(build_query_url(q))
      parse_authority_response
    end

    def build_query_url q
      escaped_query = URI.escape(q)
      authority_fragment = Loc.get_url_for_authority(subauthority) + URI.escape(subauthority)
      return "http://id.loc.gov/search/?q=#{escaped_query}&q=#{authority_fragment}&format=json"
    end

    def find id
      get_json(find_url(id))
    end

    def find_url id
      "http://id.loc.gov/authorities/#{@subauthority}/#{id}.json"
    end

    private

    # Reformats the data received from the LOC service
    def parse_authority_response
      @raw_response.select {|response| response[0] == "atom:entry"}.map do |response|
        loc_response_to_qa(response_to_struct(response))
      end
    end

    # Converts most of the atom data into an OpenStruct object.
    #
    # Note that this is a pretty naive conversion.  There should probably just
    # be a class that properly translates and stores the various pieces of
    # data, especially if this logic could be useful in other auth lookups.
    def response_to_struct response
      result = response.each_with_object({}) do |result_parts, result|
        next unless result_parts[0]
        key = result_parts[0].sub('atom:', '').sub('dcterms:', '')
        info = result_parts[1]
        val = result_parts[2]

        case key
          when 'title', 'id', 'name', 'updated', 'created'
            result[key] = val
          when 'link'
            result["links"] ||= []
            result["links"] << [info["type"], info["href"]]
        end
      end

      OpenStruct.new(result)
    end

    # Simple conversion from LoC-based struct to QA hash
    def loc_response_to_qa data
      {
        "id" => data.id || data.title,
        "label" => data.title
      }
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
qa-0.10.1 lib/qa/authorities/loc/generic_authority.rb
qa-0.10.0 lib/qa/authorities/loc/generic_authority.rb
qa-0.9.0 lib/qa/authorities/loc/generic_authority.rb
qa-0.8.0 lib/qa/authorities/loc/generic_authority.rb
qa-0.7.0 lib/qa/authorities/loc/generic_authority.rb
qa-0.6.0 lib/qa/authorities/loc/generic_authority.rb
qa-0.5.0 lib/qa/authorities/loc/generic_authority.rb