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

JsonReader, 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.



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

def initialize(uri, query, start, size)
    @uri = URI(uri)
    @query = query
    @start = start
    @size = size
    @http = Net::HTTP.new(@uri.host, @uri.port)
    if @uri.scheme == 'https'
        @http.use_ssl = true
    end
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


283
284
285
286
287
288
289
290
291
292
# File 'lib/intermine/results.rb', line 283

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



258
259
260
261
262
263
264
265
266
# File 'lib/intermine/results.rb', line 258

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



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/intermine/results.rb', line 294

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.



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/intermine/results.rb', line 243

def get_size
    query = params("jsoncount")
    res = @http.start() do |http|
        Net::HTTP.post_form(@uri, query)
    end
    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



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/intermine/results.rb', line 306

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