Module: Bioroebe::Biomart

Included in:
Database, Dataset, Server
Defined in:
lib/bioroebe/biomart/filter.rb,
lib/bioroebe/biomart/server.rb,
lib/bioroebe/biomart/biomart.rb,
lib/bioroebe/biomart/dataset.rb,
lib/bioroebe/biomart/database.rb,
lib/bioroebe/biomart/attribute.rb

Defined Under Namespace

Classes: ArgumentError, Attribute, AttributeError, BiomartError, Database, Dataset, DatasetError, Filter, FilterError, HTTPError, Server

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.proxyObject

Bioroebe::Biomart.proxy = “proxy.example.com/”



208
209
210
# File 'lib/bioroebe/biomart/biomart.rb', line 208

def proxy
  @proxy
end

.timeoutObject

Returns the value of attribute timeout.



209
210
211
# File 'lib/bioroebe/biomart/biomart.rb', line 209

def timeout
  @timeout
end

Instance Method Details

#request(params) ⇒ String

#

request

Centralised request function for handling all of the HTTP requests to the biomart servers.

#

Examples:

request({
  :url     => 'http://www.example.com',   # the url
  :method  => 'get',                      # get/post
  :query   => 'a string',                 # when using post, send this as 'query' (i.e. the biomart query xml)
  :timeout => 60                          # override the default timeout
})

Parameters:

  • params (Hash)

    Parameters to be passed to the request method.

Returns:

  • (String)

    The response body

Raises:

  • Biomart::ArgumentError Raised if a params hash is not passed (or it is empty)

  • Biomart::HTTPError Raised if a HTTP error was encountered

  • Biomart::FilterError Raised when a filter is not found

  • Biomart::AttributeError Raised when an attribute is not found

  • Biomart::DatasetError Raised when a dataset is not found

  • Biomart::BiomartError Raised for any other unhandled error



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/bioroebe/biomart/biomart.rb', line 166

def request(params)
  raise ArgumentError if !params.is_a?(Hash) || params.empty?

  require 'uri'

  if params[:url] =~ / /
    params[:url].gsub!(' ','+')
  end

  uri      = URI.parse( params[:url] )
  client   = net_http_client()
  req      = nil
  response = nil

  case params[:method]
  when 'post'
    req           = Net::HTTP::Post.new(uri.path)
    req.form_data = { 'query' => params[:query] }
  else
    req           = Net::HTTP::Get.new(uri.request_uri)
  end

  client.start(uri.host, uri.port) { |http|
    if Biomart.timeout or params[:timeout]
      http.read_timeout = params[:timeout] ? params[:timeout] : Biomart.timeout
      http.open_timeout = params[:timeout] ? params[:timeout] : Biomart.timeout
    end
    response = http.request(req)
  }
  response_code = response.code
  response_body = response.body
  if defined? Encoding && response_body.encoding == Encoding::ASCII_8BIT
    response_body = response_body.force_encoding(Encoding::UTF_8).encode
  end
  check_response(response_body, response_code)
  return response_body
end