Sha256: d5ff16fa928668ad4011a73ba3f8688a471eb5a8cbe65004855d0b2072507fe7

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

module Qa::Authorities
  class Getty::AAT < Base
    include WebServiceBase

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

    def build_query_url(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/aat/> ;
                 gvp:prefLabelGVP [skosxl:literalForm ?name].
              FILTER regex(?name, \"#{search}\", \"i\") .
            } ORDER BY ?name"
      sparql
    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/aat/#{id}.json"
    end

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

    private

      # Reformats the data received from the Getty 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

8 entries across 8 versions & 1 rubygems

Version Path
qa-2.1.2 lib/qa/authorities/getty/aat.rb
qa-2.1.1 lib/qa/authorities/getty/aat.rb
qa-2.0.1 lib/qa/authorities/getty/aat.rb
qa-2.0.0 lib/qa/authorities/getty/aat.rb
qa-1.2.0 lib/qa/authorities/getty/aat.rb
qa-1.1.0 lib/qa/authorities/getty/aat.rb
qa-1.0.0 lib/qa/authorities/getty/aat.rb
qa-0.11.1 lib/qa/authorities/getty/aat.rb