-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from samvera/ld_refactor_qa_graph
Move rdf_helper methods to graph_service
- Loading branch information
Showing
8 changed files
with
329 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Extend the RDF graph to include additional processing methods. | ||
module Qa | ||
module LinkedData | ||
class GraphService | ||
attr_reader :graph | ||
|
||
# Retrieve linked data from specified url | ||
# @param [String] url from which to retrieve linked data | ||
# @param [String | Symbol | Array<String|Symbol>] language for filtering graph (e.g. "en" or :en or ["en", "fr"] or [:en, :fr]) | ||
# @returns [RDF::Graph] graph of linked data | ||
def initialize(url:) | ||
@graph = RDF::Graph.load(url) | ||
rescue IOError => e | ||
process_error(e, url) | ||
end | ||
|
||
# Apply filters to the graph | ||
# @param language [String | Symbol | Array<String|Symbol>] will keep any statement whose object's language matches the language filter | ||
# (only applies to statements that respond to language) (e.g. "en" or :en or ["en", "fr"] or [:en, :fr]) | ||
# @param remove_blanknode_subjects [Boolean] will remove any statement whose subject is a blanknode, if true | ||
def filter(language: nil, remove_blanknode_subjects: false) | ||
return unless @graph.present? | ||
return unless language.present? || remove_blanknode_subjects | ||
language = normalize_language(language) | ||
@graph.each do |st| | ||
@graph.delete(st) if filter_out_blanknode(remove_blanknode_subjects, st.subject) || filter_out_language(language, st.object) | ||
end | ||
end | ||
|
||
private | ||
|
||
def filter_out_blanknode(remove, subj) | ||
remove && subj.anonymous? | ||
end | ||
|
||
def filter_out_language(language, obj) | ||
return false if language.blank? | ||
return false unless obj.respond_to?(:language) | ||
return false if obj.language.blank? | ||
!language.include?(obj.language) | ||
end | ||
|
||
def process_error(e, url) | ||
uri = URI(url) | ||
raise RDF::FormatError, "Unknown RDF format of results returned by #{uri}. (RDF::FormatError) You may need to include gem 'linkeddata'." if e.is_a? RDF::FormatError | ||
response_code = ioerror_code(e) | ||
case response_code | ||
when '404' | ||
raise Qa::TermNotFound, "#{uri} Not Found - Term may not exist at LOD Authority. (HTTPNotFound - 404)" | ||
when '500' | ||
raise Qa::ServiceError, "#{uri.hostname} on port #{uri.port} is not responding. Try again later. (HTTPServerError - 500)" | ||
when '503' | ||
raise Qa::ServiceUnavailable, "#{uri.hostname} on port #{uri.port} is not responding. Try again later. (HTTPServiceUnavailable - 503)" | ||
else | ||
raise Qa::ServiceError, "Unknown error for #{uri.hostname} on port #{uri.port}. Try again later. (Cause - #{e.message})" | ||
end | ||
end | ||
|
||
def ioerror_code(e) | ||
msg = e.message | ||
return 'format' if msg.start_with? "Unknown RDF format" | ||
a = msg.size - 4 | ||
z = msg.size - 2 | ||
msg[a..z] | ||
end | ||
|
||
# Normalize language | ||
# @param [String | Symbol | Array] language for filtering graph (e.g. "en" OR :en OR ["en", "fr"] OR [:en, :fr]) | ||
# @returns [Array<Symbol>] an array of languages encoded as symbols (e.g. [:en] OR [:en, :fr]) | ||
def normalize_language(language) | ||
return language if language.blank? | ||
language = [language] unless language.is_a? Array | ||
language.map(&:to_sym) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<http://id.worldcat.org/fast/530369> <http://purl.org/dc/terms/identifier> "530369" . | ||
<http://id.worldcat.org/fast/530369> <http://schema.org/name> "Cornell University" . | ||
<http://id.worldcat.org/fast/530369> <http://schema.org/sameAs> _:b0 . | ||
_:b0 <http://www.w3.org/2000/01/rdf-schema#label> "Cornell University" . | ||
<http://id.worldcat.org/fast/5140> <http://purl.org/dc/terms/identifier> "5140" . | ||
<http://id.worldcat.org/fast/5140> <http://schema.org/name> "Cornell, Joseph" . | ||
<http://id.worldcat.org/fast/5140> <http://schema.org/sameAs> _:b1 . | ||
_:b1 <http://www.w3.org/2000/01/rdf-schema#label> "Cornell, Joseph" . | ||
<http://id.worldcat.org/fast/557490> <http://purl.org/dc/terms/identifier> "557490" . | ||
<http://id.worldcat.org/fast/557490> <http://schema.org/name> "New York State School of Industrial and Labor Relations" . | ||
<http://id.worldcat.org/fast/557490> <http://schema.org/sameAs> _:b2 . | ||
_:b2 <http://www.w3.org/2000/01/rdf-schema#label> "New York State School of Industrial and Labor Relations" . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<http://test.org/530369wbn> <http://purl.org/dc/terms/identifier> "530369" . | ||
<http://test.org/530369wbn> <http://www.w3.org/2004/02/skos/core#inScheme> <http://id.worldcat.org/fast/ontology/1.0/#fast> . | ||
<http://test.org/530369wbn> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Organization> . | ||
<http://test.org/530369wbn> <http://www.w3.org/2004/02/skos/core#prefLabel> "Cornell University" . | ||
<http://test.org/530369wbn> <http://schema.org/name> "Cornell University" . | ||
<http://test.org/530369wbn> <http://www.w3.org/2004/02/skos/core#altLabel> "Ithaca (N.Y.). Cornell University" . | ||
<http://test.org/530369wbn> <http://schema.org/sameAs> _:b0 . | ||
_:b0 <http://www.w3.org/2000/01/rdf-schema#label> "Cornell University" . |
Oops, something went wrong.