Sha256: 3aff969016df74a5c5787dccc7200f594d87a1d39ae770126b9409c02a4a0797

Contents?: true

Size: 952 Bytes

Versions: 1

Compression:

Stored size: 952 Bytes

Contents

require 'uri'

module Qa::Authorities
  class Lcsh < Qa::Authorities::Base

    # Initialze the Lcsh class with a query and get the http response from LOC's server.
    # This is set to a JSON object
    def initialize(q, sub_authority='')
      self.query_url= "http://id.loc.gov/authorities/suggest/?q=" + q

      super
    end

    # Format response to the correct JSON structure
    def parse_authority_response
      self.response = build_response
    end

    def query
      self.raw_response[0]
    end

    def suggestions
      self.raw_response[1]
    end

    def urls_for_suggestions
      self.raw_response[3]
    end

    private

    def build_response a = Array.new
      self.suggestions.each_index do |i|
        a << {"id"=>get_id_from_url(urls_for_suggestions[i]), "label"=>suggestions[i]}
      end
      return a
    end

    def get_id_from_url url
      uri = URI(url)
      return uri.path.split(/\//).last
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qa-0.0.1 lib/qa/authorities/lcsh.rb