Module: Biomart

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.2.3"

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.proxyObject

Returns the value of attribute proxy.



105
106
107
# File 'lib/biomart.rb', line 105

def proxy
  @proxy
end

.timeoutObject

Returns the value of attribute timeout.



105
106
107
# File 'lib/biomart.rb', line 105

def timeout
  @timeout
end

Instance Method Details

#request(params) ⇒ String

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

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/biomart.rb', line 64

def request( params )
  raise ArgumentError if !params.is_a?(Hash) || params.empty?
  
  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) do |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)
  end
  
  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