class BELParser::Resource::SPARQLReader
Constants
- ALLOW_HEADER
- RESOLVE_CONCEPT
- RESOLVE_CONCEPTS
- RESOLVE_CONCEPT_SCHEME
- SCHEMES
Public Class Methods
new(sparql_endpoint_url, validate_url = false)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 21 def initialize(sparql_endpoint_url, validate_url = false) validate_sparql_endpoint_url(sparql_endpoint_url) if validate_url @sparql_repository = SPARQL::Client.new(sparql_endpoint_url) end
Public Instance Methods
retrieve_resource(resource_identifier)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 26 def retrieve_resource(resource_identifier) uri = URI(resource_identifier.to_s) template_binding = binding sparql_query = RESOLVE_CONCEPT_SCHEME.result(template_binding) hash_to_concept_scheme(resource_identifier, execute_select(sparql_query).first) end
retrieve_value_from_resource(resource_identifier, value)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 34 def retrieve_value_from_resource(resource_identifier, value) uri = URI(resource_identifier.to_s) template_binding = binding sparql_query = RESOLVE_CONCEPT.result(template_binding) concept_scheme = retrieve_resource(resource_identifier) to_concept = method(:hash_to_concept).to_proc.curry[concept_scheme] concepts = execute_select(sparql_query).map(&to_concept).compact return nil if concepts.empty? concepts end
retrieve_values_from_resource(resource_identifier)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 46 def retrieve_values_from_resource(resource_identifier) uri = URI(resource_identifier.to_s) template_binding = binding sparql_query = RESOLVE_CONCEPTS.result(template_binding) concept_scheme = retrieve_resource(resource_identifier) to_concept = method(:hash_to_concept).to_proc.curry[concept_scheme] concepts = execute_select(sparql_query).map(&to_concept).compact return nil if concepts.empty? concepts end
Protected Instance Methods
execute_select(sparql_query)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 60 def execute_select(sparql_query) @sparql_repository.query(sparql_query).map do |solution| solution.to_hash end end
hash_to_concept(concept_scheme, hash)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 72 def hash_to_concept(concept_scheme, hash) return nil if hash.empty? Concept.new(concept_scheme, *hash.values_at(:concept, :prefLabel, :identifier, :title, :altLabels, :types)) end
hash_to_concept_scheme(resource_identifier, hash)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 66 def hash_to_concept_scheme(resource_identifier, hash) return nil if hash.empty? or hash[:types].value.empty? ConceptScheme.new(resource_identifier, *hash.values_at(:domain, :prefix, :prefLabel, :types)) end
validate_200(response, url_s)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 98 def validate_200(response, url_s) unless response.code_type == Net::HTTPOK raise ArgumentError, <<-MSG.gsub(/ {14}/, '').delete("\n") expected URL to respond 200 (received #{response.code}) for OPTIONS request to: #{url_s}" MSG end end
validate_allowed_methods(response, url_s)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 108 def validate_allowed_methods(response, url_s) unless response[ALLOW_HEADER].to_s =~ /GET|POST/ raise ArgumentError, "expected URL to allow GET or POST: #{url_s}" end end
validate_sparql_endpoint_url(url)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 79 def validate_sparql_endpoint_url(url) url_s = url.to_s scheme, host, port = URI(url_s).select(:scheme, :host, :port) validate_uri_scheme(scheme, url_s) http = Net::HTTP.start(host, port) options_request = Net::HTTP::Options.new(url_s) options_response = http.request(options_request) validate_200(options_response, url_s) validate_allowed_methods(options_response, url_s) end
validate_uri_scheme(scheme, url_s)
click to toggle source
# File lib/bel_parser/resource/sparql_reader.rb, line 91 def validate_uri_scheme(scheme, url_s) unless SCHEMES.include?(URI.scheme_list[scheme.upcase]) raise ArgumentError, "expected HTTP or HTTPS scheme for url: #{url_s}" end end