Class: InterMine::Results::ResultsReader

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



232
233
234
235
236
237
# File 'lib/intermine/results.rb', line 232

def initialize(uri, query, start, size)
    @uri = URI(uri)
    @query = query
    @start = start
    @size = size
end

Instance Method Details

#each_resultObject

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


285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/intermine/results.rb', line 285

def each_result
    model = @query.model
    container = ''
    each_line(params("jsonobjects")) do |line|            
        line.chomp!("," + $/)
        if line.start_with?("{") and line.end_with?("}")
            begin
                data = JSON.parse(line)
                result = model.make_new(data)
            rescue JSON::ParserError => e
                raise ServiceError, "Error parsing #{line}: #{e.message}"
            rescue => e
                raise ServiceError, "Could not instantiate this result object: #{e.message}"
            end
            yield result
        else
            container << line
        end
    end
    check_result_set(container)
end

#each_rowObject

Iterate over the result set one ResultsRow at a time



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/intermine/results.rb', line 253

def each_row
    container = ''
    each_line(params("jsonrows")) do |line|
        if line.start_with?("[")
            begin
                row = ResultsRow.new(line.chomp("," + $/), @query.views)
            rescue => e
                raise ServiceError, "Error parsing #{line}: #{e.message}"
            end
            yield row
        else
            container << line
        end
    end
    check_result_set(container)
end

#get_sizeObject

Run a request to get the size of the result set.



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/intermine/results.rb', line 240

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