Sha256: c931c5774c4e9f9737b0a366f625c14d10ef857493d6aae9803c24cef5ca29ea

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require 'uri'

module Qa::Authorities
  class Getty < AuthorityWithSubAuthority
    include WebServiceBase

    def sub_authorities
      [ "aat" ]
    end

    def search q
      parse_authority_response(json(build_query_url(q)))
    end

    # get_json is not ideomatic, so we'll make an alias
    def json(*args)
      get_json(*args)
    end

    def build_query_url q
      query = URI.escape(sparql(untaint(q)))
      "http://vocab.getty.edu/sparql.json?query=#{URI.escape(sparql(q))}&_implicit=false&implicit=true&_equivalent=false&_form=%2Fsparql"
    end

    def sparql(q)
      search = untaint(q)
      # The full text index matches on fields besides the term, so we filter to ensure the match is in the term.
      sparql = "SELECT ?s ?name {
              ?s a skos:Concept; luc:term \"#{search}\";
                 skos:inScheme <http://vocab.getty.edu/#{@sub_authority}/> ;
                 gvp:prefLabelGVP [skosxl:literalForm ?name].
              FILTER regex(?name, \"#{search}\", \"i\") .
            } LIMIT 10"
    end

    def untaint(q)
      q.gsub(/[^\w\s-]/, '')
    end

    def find id
      json(find_url(id))
    end

    def find_url id
      "http://vocab.getty.edu/#{@sub_authority}/#{id}.json"
    end

    def request_options
      { accept: 'application/sparql-results+json'}
    end

    private

    # Reformats the data received from the LOC service
    def parse_authority_response(response)
      response['results']['bindings'].map do |result|
        { 'id' => result['s']['value'], 'label' => result['name']['value'] }
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qa-0.4.3 lib/qa/authorities/getty.rb