Class: Qa::Authorities::Getty::TGN

Inherits:
Base
  • Object
show all
Includes:
WebServiceBase
Defined in:
lib/qa/authorities/getty/tgn.rb

Instance Attribute Summary

Attributes included from WebServiceBase

#raw_response

Instance Method Summary collapse

Methods included from WebServiceBase

#get_json, #response

Methods inherited from Base

#all, #full_record

Instance Method Details

#build_query_url(q) ⇒ Object



14
15
16
17
18
19
# File 'lib/qa/authorities/getty/tgn.rb', line 14

def build_query_url(q)
  query = URI.escape(sparql(untaint(q)))
  # Replace ampersands, otherwise the query will fail
  # Gsub hack to convert the encoded regex in the REPLACE into a form Getty understands
  "http://vocab.getty.edu/sparql.json?query=#{query.gsub('&', '%26').gsub(',[%5E,]+,[%5E,]+$', '%2C[^%2C]%2B%2C[^%2C]%2B%24')}&_implicit=false&implicit=true&_equivalent=false&_form=%2Fsparql"
end

#find(id) ⇒ Object



58
59
60
# File 'lib/qa/authorities/getty/tgn.rb', line 58

def find(id)
  json(find_url(id))
end

#find_url(id) ⇒ Object



62
63
64
# File 'lib/qa/authorities/getty/tgn.rb', line 62

def find_url(id)
  "http://vocab.getty.edu/tgn/#{id}.json"
end

#json(*args) ⇒ Object

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



10
11
12
# File 'lib/qa/authorities/getty/tgn.rb', line 10

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

#request_optionsObject



66
67
68
# File 'lib/qa/authorities/getty/tgn.rb', line 66

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

#search(q) ⇒ Object



5
6
7
# File 'lib/qa/authorities/getty/tgn.rb', line 5

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

#sparql(q) ⇒ Object

Use a regex to exclude the continent and ‘world’ from the query If only one word is entered only search the name (not the parent string) If more than one word is entered, one word must appear in the name, and all words in either parent or name



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/qa/authorities/getty/tgn.rb', line 24

def sparql(q)
  search = untaint(q)
  if search.include?(' ')
    ex = "(("
    search.split(' ').each do |i|
      ex += "regex(CONCAT(?name, ', ', REPLACE(str(?par), \",[^,]+,[^,]+$\", \"\")), \"#{i}\",\"i\" ) && "
    end
    ex = ex[0..ex.length - 4]
    ex += ') && ('
    search.split(' ').each do |i|
      ex += "regex(?name, \"#{i}\",\"i\" ) || "
    end
    ex = ex[0..ex.length - 4]
    ex += ") )"

  else
    ex = "regex(?name, \"#{search}\", \"i\")"
  end

  # The full text index matches on fields besides the term, so we filter to ensure the match is in the term.
  sparql = "SELECT DISTINCT ?s ?name ?par {
          ?s a skos:Concept; luc:term \"#{search}\";
             skos:inScheme <http://vocab.getty.edu/tgn/> ;
             gvp:prefLabelGVP [skosxl:literalForm ?name] ;
              gvp:parentString ?par .
          FILTER #{ex} .
        } ORDER BY ?name ASC(?par)"
  sparql
end

#untaint(q) ⇒ Object



54
55
56
# File 'lib/qa/authorities/getty/tgn.rb', line 54

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