Class: InterMine::Results::ResultsReader
- Inherits:
-
Object
- Object
- InterMine::Results::ResultsReader
- Defined in:
- lib/intermine/results.rb
Overview
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.
:include:contact_header.rdoc
Direct Known Subclasses
Instance Method Summary collapse
-
#each_result ⇒ Object
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.
-
#each_row ⇒ Object
Iterate over the result set one ResultsRow at a time.
- #each_summary(summary_path) ⇒ Object
-
#get_size ⇒ Object
Run a request to get the size of the result set.
-
#initialize(uri, query, start, size) ⇒ ResultsReader
constructor
Construct a new ResultsReader.
- #read_result_set(parameters, processor) ⇒ Object
Constructor Details
#initialize(uri, query, start, size) ⇒ ResultsReader
Construct a new ResultsReader.
You will not need to do this yourself. It is handled by queries themselves.
231 232 233 234 235 236 |
# File 'lib/intermine/results.rb', line 231 def initialize(uri, query, start, size) @uri = URI(uri) @query = query @start = start @size = size end |
Instance Method Details
#each_result ⇒ Object
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
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/intermine/results.rb', line 277 def each_result model = @query.model processor = lambda {|line| x = line.chomp.chomp(",") x.empty? ? nil : model.make_new(JSON.parse(x)) } read_result_set(params("jsonobjects"), processor) {|x| yield x } end |
#each_row ⇒ Object
Iterate over the result set one ResultsRow at a time
252 253 254 255 256 257 258 259 260 |
# File 'lib/intermine/results.rb', line 252 def each_row processor = lambda {|line| x = line.chomp.chomp(",") x.empty? ? nil : ResultsRow.new(x, @query.views) } read_result_set(params("jsonrows"), processor) {|x| yield x } end |
#each_summary(summary_path) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/intermine/results.rb', line 288 def each_summary(summary_path) extra = {"summaryPath" => @query.add_prefix(summary_path)} p = params("jsonrows").merge(extra) processor = lambda {|line| x = line.chomp.chomp(",") x.empty? ? nil : JSON.parse(x) } read_result_set(p, processor) {|x| yield x } end |
#get_size ⇒ Object
Run a request to get the size of the result set.
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/intermine/results.rb', line 239 def get_size query = params("jsoncount") res = Net::HTTP.post_form(@uri, query) case res when Net::HTTPSuccess return check_result_set(res.body)["count"] else check_result_set(res.body) res.error! end end |
#read_result_set(parameters, processor) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/intermine/results.rb', line 300 def read_result_set(parameters, processor) container = '' in_results = false each_line(parameters) do |line| if line.start_with?("]") in_results = false end if in_results begin row = processor.call(line) rescue => e raise ServiceError, "Error parsing '#{line}': #{e.}" end unless row.nil? yield row end else container << line if line.chomp($/).end_with?("[") in_results = true end end end check_result_set(container) end |