Skip to content

Commit

Permalink
Merge branch 'master' into api_doc
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Nov 12, 2018
2 parents 2db3dc0 + cdcd992 commit 8156a40
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
19 changes: 13 additions & 6 deletions lib/qa/authorities/getty/aat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ 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)
def sparql(q) # rubocop:disable Metrics/MethodLength
search = untaint(q)
if search.include?(' ')
clauses = search.split(' ').collect do |i|
%((regex(?name, "#{i}", "i")))
end
ex = "(#{clauses.join(' && ')})"
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 ?s ?name {
?s a skos:Concept; luc:term \"#{search}\";
%(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
FILTER #{ex} .
} ORDER BY ?name).gsub(/[\s\n]+/, " ")
end

def untaint(q)
Expand Down
31 changes: 10 additions & 21 deletions lib/qa/authorities/getty/tgn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,20 @@ def build_query_url(q)
def sparql(q) # rubocop:disable Metrics/MethodLength
search = untaint(q)
if search.include?(' ')
ex = "(("
search.split(' ').each do |i|
ex += "regex(CONCAT(?name, ', ', REPLACE(str(?par), \",[^,]+,[^,]+$\", \"\")), \"#{i}\",\"i\" ) && "
clauses = search.split(' ').collect do |i|
%((regex(?name, "#{i}", "i") || regex(?alt, "#{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 += ") )"

ex = "(#{clauses.join(' && ')})"
else
ex = "regex(?name, \"#{search}\", \"i\")"
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] ;
%(SELECT DISTINCT ?s ?name ?par {
?s a skos:Concept; luc:term "#{search}";
skos:inScheme <http://vocab.getty.edu/ulan/> ;
gvp:prefLabelGVP [skosxl:literalForm ?name] ;
gvp:parentString ?par .
FILTER #{ex} .
} ORDER BY ?name ASC(?par)"
sparql
FILTER #{ex} .
} ORDER BY ?name ASC(?par)).gsub(/[\s\n]+/, " ")
end

def untaint(q)
Expand Down
23 changes: 14 additions & 9 deletions spec/lib/authorities/getty/aat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@
subject { authority.request_options }
it { is_expected.to eq(accept: "application/sparql-results+json") }
end

# rubocop:disable Metrics/LineLength
describe "#sparql" do
subject { authority.sparql('search_term') }
it {
is_expected.to eq 'SELECT ?s ?name {
?s a skos:Concept; luc:term "search_term";
skos:inScheme <http://vocab.getty.edu/aat/> ;
gvp:prefLabelGVP [skosxl:literalForm ?name].
FILTER regex(?name, "search_term", "i") .
} ORDER BY ?name' }
context "using a single subject term" do
subject { authority.sparql('search_term') }
it {
is_expected.to eq %(SELECT ?s ?name { ?s a skos:Concept; luc:term "search_term"; skos:inScheme <http://vocab.getty.edu/aat/> ; gvp:prefLabelGVP [skosxl:literalForm ?name]. FILTER regex(?name, "search_term", "i") . } ORDER BY ?name)
}
end
context "using a two subject terms" do
subject { authority.sparql('search term') }
it {
is_expected.to eq %(SELECT ?s ?name { ?s a skos:Concept; luc:term "search term"; skos:inScheme <http://vocab.getty.edu/aat/> ; gvp:prefLabelGVP [skosxl:literalForm ?name]. FILTER ((regex(?name, "search", "i")) && (regex(?name, "term", "i"))) . } ORDER BY ?name)
}
end
end
# rubocop:enable Metrics/LineLength
end
23 changes: 6 additions & 17 deletions spec/lib/authorities/getty/tgn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,20 @@
subject { authority.request_options }
it { is_expected.to eq(accept: "application/sparql-results+json") }
end

# rubocop:disable Metrics/LineLength
describe "#sparql" do
context "using a single subject term" do
subject { authority.sparql('search_term') }
it {
is_expected.to eq 'SELECT DISTINCT ?s ?name ?par {
?s a skos:Concept; luc:term "search_term";
skos:inScheme <http://vocab.getty.edu/tgn/> ;
gvp:prefLabelGVP [skosxl:literalForm ?name] ;
gvp:parentString ?par .
FILTER regex(?name, "search_term", "i") .
} ORDER BY ?name ASC(?par)' }
is_expected.to eq %(SELECT DISTINCT ?s ?name ?par { ?s a skos:Concept; luc:term "search_term"; skos:inScheme <http://vocab.getty.edu/ulan/> ; gvp:prefLabelGVP [skosxl:literalForm ?name] ; gvp:parentString ?par . FILTER regex(?name, "search_term", "i") . } ORDER BY ?name ASC(?par))
}
end
context "using a two subject terms" do
subject { authority.sparql('search term') }
# rubocop:disable Metrics/LineLength
it {
is_expected.to eq "SELECT DISTINCT ?s ?name ?par {
?s a skos:Concept; luc:term \"search term\";
skos:inScheme <http://vocab.getty.edu/tgn/> ;
gvp:prefLabelGVP [skosxl:literalForm ?name] ;
gvp:parentString ?par .
FILTER ((regex(CONCAT(?name, ', ', REPLACE(str(?par), \",[^,]+,[^,]+$\", \"\")), \"search\",\"i\" ) && regex(CONCAT(?name, ', ', REPLACE(str(?par), \",[^,]+,[^,]+$\", \"\")), \"term\",\"i\" ) ) && (regex(?name, \"search\",\"i\" ) || regex(?name, \"term\",\"i\" ) ) ) .
} ORDER BY ?name ASC(?par)" }
# rubocop:enable Metrics/LineLength
is_expected.to eq %(SELECT DISTINCT ?s ?name ?par { ?s a skos:Concept; luc:term "search term"; skos:inScheme <http://vocab.getty.edu/ulan/> ; gvp:prefLabelGVP [skosxl:literalForm ?name] ; gvp:parentString ?par . FILTER ((regex(?name, "search", "i") || regex(?alt, "search", "i")) && (regex(?name, "term", "i") || regex(?alt, "term", "i"))) . } ORDER BY ?name ASC(?par))
}
end
end
# rubocop:enable Metrics/LineLength
end

0 comments on commit 8156a40

Please sign in to comment.