Class: Pho::Sparql::SparqlClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pho/sparql.rb

Overview

A simple SPARQL client that handles the basic HTTP traffic

Direct Known Subclasses

Pho::StoreSparqlClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, client = HTTPClient.new()) ⇒ SparqlClient

Initialize a client for a specific endpoint

endpoint

uri of the SPARQL endpoint

client

optionally, a reference to an existing HTTPClient object instance



118
119
120
121
122
123
124
125
126
# File 'lib/pho/sparql.rb', line 118

def initialize(endpoint, client=HTTPClient.new() )
 @endpoint = endpoint
 @graphs = nil
 @named_graphs = nil
 @client = client
 @output_parameter_name = nil
 @supports_rdf_json = false
 @supports_sparql_json = true
end

Instance Attribute Details

#clientObject (readonly)

HTTPClient object



100
101
102
# File 'lib/pho/sparql.rb', line 100

def client
  @client
end

#endpointObject (readonly)

URI of the endpoint



98
99
100
# File 'lib/pho/sparql.rb', line 98

def endpoint
  @endpoint
end

#graphsObject (readonly)

Returns the value of attribute graphs.



104
105
106
# File 'lib/pho/sparql.rb', line 104

def graphs
  @graphs
end

#named_graphsObject (readonly)

Returns the value of attribute named_graphs.



105
106
107
# File 'lib/pho/sparql.rb', line 105

def named_graphs
  @named_graphs
end

#output_parameter_nameObject

Name of output parameter to use to control response format. If set then this parameter is added to the query string, rather than using Content Negotiation



103
104
105
# File 'lib/pho/sparql.rb', line 103

def output_parameter_name
  @output_parameter_name
end

#supports_rdf_jsonObject

Configures whether the remote endpoint supports the RDF-in-JSON specification for serializing RDF graphs as JSON. Will default to false.



109
110
111
# File 'lib/pho/sparql.rb', line 109

def supports_rdf_json
  @supports_rdf_json
end

#supports_sparql_jsonObject

Configures whether the remote endpoint supports SPARQL JSON Results format Will default to true.



112
113
114
# File 'lib/pho/sparql.rb', line 112

def supports_sparql_json
  @supports_sparql_json
end

Instance Method Details

#add_default_graph(graph_uri) ⇒ Object

Add a default graph. This will be added as a default graph in the request protocol



129
130
131
132
133
134
# File 'lib/pho/sparql.rb', line 129

def add_default_graph(graph_uri)
  if @graphs == nil
     @graphs = []
  end  
  @graphs << graph_uri
end

#add_named_graph(graph_uri) ⇒ Object

Add a named graph. This will be added as a named graph in the request protocol



137
138
139
140
141
142
# File 'lib/pho/sparql.rb', line 137

def add_named_graph(graph_uri)
  if @named_graphs == nil
    @named_graphs = []
  end  
  @named_graphs << graph_uri
end

#ask(query, format = Pho::Sparql::SPARQL_RESULTS_XML) ⇒ Object

Perform a SPARQL ASK query.

query

the SPARQL query

format

the preferred response format



228
229
230
# File 'lib/pho/sparql.rb', line 228

def ask(query, format=Pho::Sparql::SPARQL_RESULTS_XML)
  return query(query, format)
end

#construct(query, format = "application/rdf+xml") ⇒ Object

Perform a SPARQL CONSTRUCT query.

query

the SPARQL query

format

the preferred response format



220
221
222
# File 'lib/pho/sparql.rb', line 220

def construct(query, format="application/rdf+xml")
  return query(query, format)
end

#describe(query, format = "application/rdf+xml") ⇒ Object

Perform a SPARQL DESCRIBE query.

query

the SPARQL query

format

the preferred response format



199
200
201
# File 'lib/pho/sparql.rb', line 199

def describe(query, format="application/rdf+xml")
  return query(query, format)
end

#describe_uri(uri, format = "application/rdf+xml", type = :cbd) ⇒ Object

Describe a uri, optionally specifying a form of bounded description

uri

the uri to describe

format

mimetype for results

type

symbol indicating type of description, i.e. :cbd, :scbd, :lcbd, or :slcbd



186
187
188
189
190
191
192
193
# File 'lib/pho/sparql.rb', line 186

def describe_uri(uri, format="application/rdf+xml", type=:cbd)
  template = Pho::Sparql::DESCRIPTIONS[type]
  if template == nil
    raise "Unknown description type"
  end
  query = Pho::Sparql::SparqlHelper.apply_initial_bindings(template, {"uri" => "<#{uri}>"} )
  return describe(query, format)
end

#multi_describe(uris, format = "application/rdf+xml") ⇒ Object

DESCRIBE multiple resources in a single query. The provided array should contain the uris that are to be described

This will generate a query like: DESCRIBE <www.example.org> <www.example.com> …

uris

list of the uris to be described

format

the preferred response format. Default is RDF/XML



211
212
213
214
# File 'lib/pho/sparql.rb', line 211

def multi_describe(uris, format="application/rdf+xml")
  query = "DESCRIBE " + uris.map {|u| "<#{u}>" }.join(" ")
  return query(query, format)
end

#query(sparql, format = nil, graphs = nil, named_graphs = nil) ⇒ Object

Perform a sparql query

sparql

a valid SPARQL query

format

specific a request format. Usually a media-type, but may be a name for a type, if not using Conneg

graphs

an array of default graphs

named_graphs

an array of named graphs



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pho/sparql.rb', line 150

def query(sparql, format=nil, graphs=nil, named_graphs=nil)
  
  params = {}
  params["query"] = sparql
  
  if graphs != nil
    params["default-graph-uri"] = graphs
  elsif @graphs != nil
    params["default-graph-uri"] = @graphs
  end          

  if named_graphs != nil
    params["named-graph-uri"] = named_graphs
  elsif @named_graphs != nil
    params["named-graph-uri"] = @named_graphs
  end
  
  headers = {}
  if format != nil
    
    if @output_parameter_name != nil
      params[@output_parameter_name] = format
    else 
      headers["Accept"] = format  
    end
    
  end
  
  return @client.get( @endpoint, params, headers )
end

#select(query, format = Pho::Sparql::SPARQL_RESULTS_XML) ⇒ Object

Perform a SPARQL SELECT query.

query

the SPARQL query

format

the preferred response format



236
237
238
# File 'lib/pho/sparql.rb', line 236

def select(query, format=Pho::Sparql::SPARQL_RESULTS_XML)
  return query(query, format)
end