spacer
spacer

net/http

For REST services using HTTP GET and/or POST the Ruby module net/http can be used to access the service.

HTTP GET

For services available through a HTTP GET request, for example WSDbfetch (REST):

# Load libraries
require 'net/http'
require 'uri'
 
# Base URL for service
baseUrl = 'http://www.ebi.ac.uk/Tools/webservices/rest/dbfetch'
 
# Defaults
dbName = 'uniprot'
entryId = 'wap_rat'
format = nil
 
# Split URL into components
baseUri = URI.parse(baseUrl)
 
# Create a HTTP connection
httpConn = Net::HTTP.new(baseUri.host, baseUri.port)
 
# Build the path for the resource
reqPath = baseUri.path + '/' + dbName + '/' + entryId
if format != nil
  reqPath += '/' + format
end
 
# Get the entry
resp, data = httpConn.get(reqPath)
 
# Print the result
puts data

HTTP POST

For services which require the use of a HTTP POST request, for example WSWUBlast (see REST tutorial):

# Tool name
toolName = 'wublast'
# Query sequence
seq = ">Q8E5Q5_STRA3
MKLSKRYRFWQKVIKALGVLALIATLVLVVYLYKLGILNDSNELKDLVHKYEFWGPMIFI
VAQIVQIVFPVIPGGVTTVAGFLIFGPTLGFIYNYIGIIIGSVILFWLVKFYGRKFVLLF
MDQKTFDKYESKLETSGYEKFFIFCMASPISPADIMVMITGLSNMSIKRFVTIIMITKPI
SIIGYSYLWIYGGDILKNFLN"
# Structure containing parameters
params = {
          'tool' => toolName,
          'srchType' => 'interactive',
          'sequence' => seq,
          'program' => 'blastp',
          'database' => 'swissprot',
          'email' => 'your@email'
}
 
# Build the path for the resource
submitUrl = baseUrl + '/submit'
# Submit the job
resp, jobId = Net::HTTP.post_form(URI(submitUrl), params)
# Print the job identifier
puts jobId

Examples

Sample Clients

Most REST Web Services at EMBL-EBI have sample clients which provide command-line access to the service and example code. For Ruby some of the clients are based on net/http, for example:

Service Sample client
InterProScan (REST) iprscan_net_http.rb
NCBI BLAST (REST) ncbiblast_net_http.rb
WSDbfetch (REST) dbfetch_net_http.rb
 
tutorials/06_programming/ruby/rest/net_http.txt · Last modified: 2011/03/29 17:30 by hpm
spacer
spacer