class InterMine::Results::ResultsReader
The class responsible for retrieving results and processing them
query.each_row do |row| puts row[2..3] end
Queries delegate their handling of results to these objects, which are responsible for creating the ResultsRow
objects or model objects that the results represent.
- author
-
Alex Kalderimis dev@intermine.org
- homepage
- Licence
-
Copyright (C) 2002-2011 FlyMine
This code may be freely distributed and modified under the terms of the GNU Lesser General Public Licence. This should be distributed with the code. See the LICENSE file for more information or www.gnu.org/copyleft/lesser.html.
Public Class Methods
Construct a new ResultsReader
.
You will not need to do this yourself. It is handled by queries themselves.
# File lib/intermine/results.rb 231 def initialize(uri, query, start, size) 232 @uri = URI(uri) 233 @query = query 234 @start = start 235 @size = size 236 @http = Net::HTTP.new(@uri.host, @uri.port) 237 if @uri.scheme == 'https' 238 @http.use_ssl = true 239 end 240 end
Public Instance Methods
Iterate over the resultset, one object at a time, where the object is the instantiation of the type of object specified as the query's root type. The actual type returned depends on the query itself, and any subclass information returned by the webservice.
query = service.query("Gene").select("*") query.each_result do |gene| puts gene.symbol, gene.length end query = service.query("Organism").select("*") query.each_result do |organism| puts organism.shortName, organism.taxonId end
# File lib/intermine/results.rb 283 def each_result 284 model = @query.model 285 processor = lambda {|line| 286 x = line.chomp.chomp(",") 287 x.empty? ? nil : model.make_new(JSON.parse(x)) 288 } 289 read_result_set(params("jsonobjects"), processor) {|x| 290 yield x 291 } 292 end
Iterate over the result set one ResultsRow
at a time
# File lib/intermine/results.rb 258 def each_row 259 processor = lambda {|line| 260 x = line.chomp.chomp(",") 261 x.empty? ? nil : ResultsRow.new(x, @query.views) 262 } 263 read_result_set(params("jsonrows"), processor) {|x| 264 yield x 265 } 266 end
# File lib/intermine/results.rb 294 def each_summary(summary_path) 295 extra = {"summaryPath" => @query.add_prefix(summary_path)} 296 p = params("jsonrows").merge(extra) 297 processor = lambda {|line| 298 x = line.chomp.chomp(",") 299 x.empty? ? nil : JSON.parse(x) 300 } 301 read_result_set(p, processor) {|x| 302 yield x 303 } 304 end
Run a request to get the size of the result set.
# File lib/intermine/results.rb 243 def get_size 244 query = params("jsoncount") 245 res = @http.start() do |http| 246 Net::HTTP.post_form(@uri, query) 247 end 248 case res 249 when Net::HTTPSuccess 250 return check_result_set(res.body)["count"] 251 else 252 check_result_set(res.body) 253 res.error! 254 end 255 end
# File lib/intermine/results.rb 306 def read_result_set(parameters, processor) 307 container = '' 308 in_results = false 309 each_line(parameters) do |line| 310 if line.start_with?("]") 311 in_results = false 312 end 313 if in_results 314 begin 315 row = processor.call(line) 316 rescue => e 317 raise ServiceError, "Error parsing '#{line}': #{e.message}" 318 end 319 unless row.nil? 320 yield row 321 end 322 else 323 container << line 324 if line.chomp($/).end_with?("[") 325 in_results = true 326 end 327 end 328 end 329 check_result_set(container) 330 end
Private Instance Methods
Check that the request was successful according the metadata passed with the result.
# File lib/intermine/results.rb 371 def check_result_set(container) 372 begin 373 result_set = JSON.parse(container) 374 rescue JSON::ParserError => e 375 raise "Error parsing container: #{container}, #{e.message}" 376 end 377 unless result_set["wasSuccessful"] 378 raise ServiceError, result_set["error"] 379 end 380 result_set 381 end
Retrieve the results from the webservice, one line at a time. This method has responsibility for ensuring that the lines are complete, and not split over two or more chunks.
# File lib/intermine/results.rb 337 def each_line(data) 338 req = Net::HTTP::Post.new(@uri.path) 339 req.set_form_data(data) 340 @http.start {|http| 341 http.request(req) {|resp| 342 holdover = "" 343 resp.read_body {|chunk| 344 sock = StringIO.new(holdover + chunk) 345 sock.each_line {|line| 346 if sock.eof? 347 holdover = line 348 else 349 unless line.empty? 350 yield line 351 end 352 end 353 } 354 sock.close 355 } 356 yield holdover 357 } 358 } 359 end
Get the parameters for this request, given the specified format.
# File lib/intermine/results.rb 363 def params(format="tab") 364 p = @query.params.merge("start" => @start, "format" => format) 365 p["size"] = @size unless @size.nil? 366 return p 367 end