sparql-client 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sparql/client.rb +31 -20
- data/lib/sparql/client/version.rb +1 -1
- metadata +11 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
data/lib/sparql/client.rb
CHANGED
@@ -89,13 +89,31 @@ module SPARQL
|
|
89
89
|
end
|
90
90
|
|
91
91
|
##
|
92
|
-
#
|
92
|
+
# A mapping of blank node results for this client
|
93
|
+
# @private
|
94
|
+
def nodes
|
95
|
+
@nodes ||= {}
|
96
|
+
end
|
97
|
+
|
98
|
+
##
|
99
|
+
# Executes a SPARQL query and returns parsed results.
|
93
100
|
#
|
94
101
|
# @param [String, #to_s] url
|
95
102
|
# @param [Hash{Symbol => Object}] options
|
96
103
|
# @option options [String] :content_type
|
97
104
|
# @return [Array<RDF::Query::Solution>]
|
98
105
|
def query(query, options = {})
|
106
|
+
parse_response(response(query, options), options)
|
107
|
+
end
|
108
|
+
|
109
|
+
##
|
110
|
+
# Executes a SPARQL query and returns the Net::HTTP::Response of the result.
|
111
|
+
#
|
112
|
+
# @param [String, #to_s] url
|
113
|
+
# @param [Hash{Symbol => Object}] options
|
114
|
+
# @option options [String] :content_type
|
115
|
+
# @return [String]
|
116
|
+
def response(query, options = {})
|
99
117
|
@headers['Accept'] = options[:content_type] if options[:content_type]
|
100
118
|
get(query) do |response|
|
101
119
|
case response
|
@@ -106,9 +124,10 @@ module SPARQL
|
|
106
124
|
when Net::HTTPServerError # 5xx
|
107
125
|
raise ServerError.new(response.body)
|
108
126
|
when Net::HTTPSuccess # 2xx
|
109
|
-
|
127
|
+
response
|
110
128
|
end
|
111
129
|
end
|
130
|
+
|
112
131
|
end
|
113
132
|
|
114
133
|
##
|
@@ -120,9 +139,9 @@ module SPARQL
|
|
120
139
|
when RESULT_BOOL # Sesame-specific
|
121
140
|
response.body == 'true'
|
122
141
|
when RESULT_JSON
|
123
|
-
parse_json_bindings(response.body)
|
142
|
+
self.class.parse_json_bindings(response.body, nodes)
|
124
143
|
when RESULT_XML
|
125
|
-
parse_xml_bindings(response.body)
|
144
|
+
self.class.parse_xml_bindings(response.body, nodes)
|
126
145
|
else
|
127
146
|
parse_rdf_serialization(response, options)
|
128
147
|
end
|
@@ -132,7 +151,7 @@ module SPARQL
|
|
132
151
|
# @param [String, Hash] json
|
133
152
|
# @return [Enumerable<RDF::Query::Solution>]
|
134
153
|
# @see http://www.w3.org/TR/rdf-sparql-json-res/#results
|
135
|
-
def parse_json_bindings(json)
|
154
|
+
def self.parse_json_bindings(json, nodes = {})
|
136
155
|
require 'json' unless defined?(::JSON)
|
137
156
|
json = JSON.parse(json.to_s) unless json.is_a?(Hash)
|
138
157
|
|
@@ -153,11 +172,10 @@ module SPARQL
|
|
153
172
|
# @param [Hash{String => String}] value
|
154
173
|
# @return [RDF::Value]
|
155
174
|
# @see http://www.w3.org/TR/rdf-sparql-json-res/#variable-binding-results
|
156
|
-
def parse_json_value(value)
|
175
|
+
def self.parse_json_value(value, nodes = {})
|
157
176
|
case value['type'].to_sym
|
158
177
|
when :bnode
|
159
|
-
|
160
|
-
@nodes[id = value['value']] ||= RDF::Node.new(id)
|
178
|
+
nodes[id = value['value']] ||= RDF::Node.new(id)
|
161
179
|
when :uri
|
162
180
|
RDF::URI.new(value['value'])
|
163
181
|
when :literal
|
@@ -172,8 +190,8 @@ module SPARQL
|
|
172
190
|
# @param [String, REXML::Element] xml
|
173
191
|
# @return [Enumerable<RDF::Query::Solution>]
|
174
192
|
# @see http://www.w3.org/TR/rdf-sparql-json-res/#results
|
175
|
-
def parse_xml_bindings(xml)
|
176
|
-
xml.force_encoding(
|
193
|
+
def self.parse_xml_bindings(xml, nodes = {})
|
194
|
+
xml.force_encoding(::Encoding::UTF_8) if xml.respond_to?(:force_encoding)
|
177
195
|
require 'rexml/document' unless defined?(::REXML::Document)
|
178
196
|
xml = REXML::Document.new(xml).root unless xml.is_a?(REXML::Element)
|
179
197
|
|
@@ -186,7 +204,7 @@ module SPARQL
|
|
186
204
|
result.elements.each do |binding|
|
187
205
|
name = binding.attributes['name'].to_sym
|
188
206
|
value = binding.select { |node| node.kind_of?(::REXML::Element) }.first
|
189
|
-
row[name] = parse_xml_value(value)
|
207
|
+
row[name] = parse_xml_value(value, nodes)
|
190
208
|
end
|
191
209
|
RDF::Query::Solution.new(row)
|
192
210
|
end
|
@@ -197,11 +215,10 @@ module SPARQL
|
|
197
215
|
# @param [REXML::Element] value
|
198
216
|
# @return [RDF::Value]
|
199
217
|
# @see http://www.w3.org/TR/rdf-sparql-json-res/#variable-binding-results
|
200
|
-
def parse_xml_value(value)
|
218
|
+
def self.parse_xml_value(value, nodes = {})
|
201
219
|
case value.name.to_sym
|
202
220
|
when :bnode
|
203
|
-
|
204
|
-
@nodes[id = value.text] ||= RDF::Node.new(id)
|
221
|
+
nodes[id = value.text] ||= RDF::Node.new(id)
|
205
222
|
when :uri
|
206
223
|
RDF::URI.new(value.text)
|
207
224
|
when :literal
|
@@ -224,12 +241,6 @@ module SPARQL
|
|
224
241
|
end
|
225
242
|
end
|
226
243
|
|
227
|
-
##
|
228
|
-
# @return [Encoding]
|
229
|
-
def encoding
|
230
|
-
@encoding ||= ::Encoding::UTF_8
|
231
|
-
end
|
232
|
-
|
233
244
|
##
|
234
245
|
# Outputs a developer-friendly representation of this object to `stderr`.
|
235
246
|
#
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 9
|
9
|
+
version: 0.0.9
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Arto Bendiken
|
@@ -15,13 +15,14 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-02-02 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: json_pure
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
25
26
|
requirements:
|
26
27
|
- - ">="
|
27
28
|
- !ruby/object:Gem::Version
|
@@ -36,6 +37,7 @@ dependencies:
|
|
36
37
|
name: rdf
|
37
38
|
prerelease: false
|
38
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
39
41
|
requirements:
|
40
42
|
- - ~>
|
41
43
|
- !ruby/object:Gem::Version
|
@@ -50,6 +52,7 @@ dependencies:
|
|
50
52
|
name: yard
|
51
53
|
prerelease: false
|
52
54
|
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
53
56
|
requirements:
|
54
57
|
- - ">="
|
55
58
|
- !ruby/object:Gem::Version
|
@@ -64,6 +67,7 @@ dependencies:
|
|
64
67
|
name: rspec
|
65
68
|
prerelease: false
|
66
69
|
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
67
71
|
requirements:
|
68
72
|
- - ">="
|
69
73
|
- !ruby/object:Gem::Version
|
@@ -78,6 +82,7 @@ dependencies:
|
|
78
82
|
name: rdf-spec
|
79
83
|
prerelease: false
|
80
84
|
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
81
86
|
requirements:
|
82
87
|
- - ~>
|
83
88
|
- !ruby/object:Gem::Version
|
@@ -117,6 +122,7 @@ rdoc_options: []
|
|
117
122
|
require_paths:
|
118
123
|
- lib
|
119
124
|
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
120
126
|
requirements:
|
121
127
|
- - ">="
|
122
128
|
- !ruby/object:Gem::Version
|
@@ -126,6 +132,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
132
|
- 1
|
127
133
|
version: 1.8.1
|
128
134
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
129
136
|
requirements:
|
130
137
|
- - ">="
|
131
138
|
- !ruby/object:Gem::Version
|
@@ -135,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
142
|
requirements: []
|
136
143
|
|
137
144
|
rubyforge_project: sparql
|
138
|
-
rubygems_version: 1.3.
|
145
|
+
rubygems_version: 1.3.7
|
139
146
|
signing_key:
|
140
147
|
specification_version: 3
|
141
148
|
summary: SPARQL client for RDF.rb.
|