Class: Qa::Authorities::Getty

Inherits:
WebServiceBase show all
Defined in:
lib/qa/authorities/getty.rb

Instance Attribute Summary

Attributes inherited from WebServiceBase

#raw_response

Attributes inherited from Base

#sub_authority

Instance Method Summary collapse

Methods inherited from WebServiceBase

#get_json

Methods inherited from Base

#all, #full_record

Constructor Details

#initialize(*args) ⇒ Getty

Returns a new instance of Getty.



6
7
8
9
10
# File 'lib/qa/authorities/getty.rb', line 6

def initialize *args
  super
  @sub_authority ||= args.first
  raise "No sub-authority provided" if sub_authority.nil?
end

Instance Method Details

#build_query_url(q) ⇒ Object



25
26
27
28
# File 'lib/qa/authorities/getty.rb', line 25

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

#find(id) ⇒ Object



44
45
46
# File 'lib/qa/authorities/getty.rb', line 44

def find id
  json(find_url(id))
end

#find_url(id) ⇒ Object



48
49
50
# File 'lib/qa/authorities/getty.rb', line 48

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

#json(*args) ⇒ Object

get_json is not ideomatic, so we’ll make an alias



21
22
23
# File 'lib/qa/authorities/getty.rb', line 21

def json(*args)
  get_json(*args)
end

#request_optionsObject



52
53
54
55
# File 'lib/qa/authorities/getty.rb', line 52

def request_options
  # Don't pass a request header. See http://answers.semanticweb.com/questions/31906/getty-sparql-gives-a-404-if-you-pass-accept-applicationjson
  { }
end

#search(q) ⇒ Object



16
17
18
# File 'lib/qa/authorities/getty.rb', line 16

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

#sparql(q) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/qa/authorities/getty.rb', line 30

def sparql(q)
  sparql = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
            SELECT * WHERE { ?s skos:prefLabel ?name .
              ?s skos:inScheme <http://vocab.getty.edu/#{@sub_authority}/> .
              ?s rdf:type <http://vocab.getty.edu/ontology#Concept> .
              FILTER regex(?name, \"#{untaint(q)}\", \"i\") .
              FILTER langMatches( lang(?name), \"EN\" ) .
            } LIMIT 10"
end

#sub_authoritiesObject



12
13
14
# File 'lib/qa/authorities/getty.rb', line 12

def sub_authorities
  [ "aat" ]
end

#untaint(q) ⇒ Object



40
41
42
# File 'lib/qa/authorities/getty.rb', line 40

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