Class: InterMine::Results::ResultsReader

Inherits:
Object
  • Object
show all
Defined in:
lib/intermine/results.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri, query, start, size) ⇒ ResultsReader

Returns a new instance of ResultsReader.



91
92
93
94
95
96
# File 'lib/intermine/results.rb', line 91

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

Instance Method Details

#each_line(data) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/intermine/results.rb', line 116

def each_line(data)
    req = Net::HTTP::Post.new(@uri.path)
    req.set_form_data(data)
    Net::HTTP.new(@uri.host, @uri.port).start {|http|
        http.request(req) {|resp|
            holdover = ""
            resp.read_body {|chunk|
                sock = StringIO.new(holdover + chunk)
                sock.each_line {|line|
                    if sock.eof?
                        holdover = line
                    else
                        yield line
                    end
                }
                sock.close
            }
            yield holdover
        }
    }
end

#each_resultObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/intermine/results.rb', line 155

def each_result
    model = @query.model
    container = ''
    self.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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/intermine/results.rb', line 138

def each_row
    container = ''
    self.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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/intermine/results.rb', line 104

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

#params(format) ⇒ Object



98
99
100
101
102
# File 'lib/intermine/results.rb', line 98

def params(format)
    p = @query.params.merge("start" => @start, "format" => format)
    p["size"] = @size unless @size.nil?
    return p
end