Sha256: 94f232f99e89341cf06378e6ce2e826f94ad2e066f4ccd0be28cbe1ca2a86370

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

module Qa::Authorities
  # A wrapper around the FAST api for use with questioning_authority
  # API documentation:
  # http://www.oclc.org/developer/develop/web-services/fast-api/assign-fast.en.html
  class AssignFast::GenericAuthority < Base
    attr_reader :subauthority
    def initialize(subauthority)
      @subauthority = subauthority
    end

    include WebServiceBase

    # Search the FAST api
    #
    # @param [String] the query
    # @return json results
    def search q
      url = build_query_url q
      begin
        raw_response = get_json(url)
      rescue JSON::ParserError => error
        Rails.logger.info "Could not parse response as JSON. Request url: #{url}"
        return []
      end
      parse_authority_response(raw_response)
    end

    # Build a FAST API url
    #
    # @param [String] the query
    # @return [String] the url
    def build_query_url q
      escaped_query = clean_query_string q
      index = AssignFast.index_for_authority(subauthority)
      return_data = "#{index}%2Cidroot%2Cauth%2Ctype"
      num_rows = 20 # max allowed by the API
      url = "http://fast.oclc.org/searchfast/fastsuggest?&query=#{escaped_query}&queryIndex=#{index}&queryReturn=#{return_data}&suggest=autoSubject&rows=#{num_rows}"
    end

    private

    # Removes characters from the query string that are not tolerated by the API
    #   See oclc sample code at
    #   http://experimental.worldcat.org/fast/assignfast/js/assignFASTComplete.js
    def clean_query_string q
      URI.escape(q.gsub(/-|\(|\)|:/, ""))
    end

    def parse_authority_response(raw_response)
      results = raw_response['response']['docs'].map do |doc|
        index = AssignFast.index_for_authority(subauthority)
        term = doc[index].first
        if doc['type'] == 'alt'
          term += ' USE ' + doc['auth']
        end
        { id: doc['idroot'], label: term, value: doc['auth'] }
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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