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

Direct Known Subclasses

ObjectReader, RowReader, SummaryReader

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.



244
245
246
247
248
249
# File 'lib/intermine/results.rb', line 244

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


292
293
294
295
296
297
298
299
300
301
# File 'lib/intermine/results.rb', line 292

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_rowObject

Iterate over the result set one ResultsRow at a time



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/intermine/results.rb', line 265

def each_row
    cls = @query.service.version >= 8 ? NewResultRow : ResultsRow
    format = @query.service.version >= 8 ? "json" : "jsonrows"
    processor = lambda {|line|
        x = line.chomp.chomp(",")
        x.empty? ? nil : cls.new(x, @query.views)
    }
    read_result_set(params(format), processor) {|x|
        yield x
    }
end

#each_summary(summary_path) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
# File 'lib/intermine/results.rb', line 303

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_sizeObject

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



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/intermine/results.rb', line 252

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



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/intermine/results.rb', line 315

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.message}"
            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