Skip to content

Using the Web Services: Ruby

pm9-sanger edited this page Jan 3, 2017 · 2 revisions

The following two code examples give an overview of how you can use various gems to easily interact with the iMits REST API.

Rest-Client

This example uses the "rest-client":https://rubygems.org/gems/rest-client gem.

#!/usr/bin/env ruby
# encoding: utf-8

require 'rubygems'
require 'rest_client'
require 'json'
require 'logger'
require 'awesome_print'

class Imits
  SERVICE_URL = 'https://www.i-dcc.org/staging/imits'

  @@log           = Logger.new(STDOUT)
  @@log.level     = Logger::INFO
  RestClient.log = @@log

  def initialize(imits_user, imits_pass)
    @client = RestClient::Resource.new(SERVICE_URL, :user => imits_user, :password => imits_pass)
  end

  def request(method, url, data = nil)
    begin
      method = method.upcase
      response =
        case method
        when 'GET'    then @client[url].get        :content_type => 'application/json'
        when 'POST'   then @client[url].post data, :content_type => 'application/json'
        when 'PUT'    then @client[url].put  data, :content_type => 'application/json'
        when 'DELETE' then @client[url].delete     :content_type => 'application/json'
        else
          raise "Method #{method} unknown when requesting url #{url}"
        end

      if method == 'DELETE'
        return true
      else
        return JSON.parse(response.body)
      end
    rescue RuntimeError => e
      @@log.error "URL: #{url}"
      @@log.error "DATA: #{data}"
      @@log.error "RESPONSE: #{e.http_body}"
      @@log.error e
      raise e
    end
  end
  private :request

  def find_mi(params_hash={})
    url          = 'mi_attempts.json'
    params_array = []
    params_hash.each{ |key,val| params_array.push("#{key}=#{val}") }
    url << "?#{params_array.join('&')}" unless params_array.empty?
    request('GET', url)
  end

  def update_mi(id, data={})
    request('PUT', "mi_attempts/#{id}.json", { :mi_attempt => data })
  end

  def create_mi(data={})
    request('POST', 'mi_attempts.json', { :mi_attempt => data })
  end
end

##
## Actual application logic
##

# Use your actual email address here!
client = Imits.new('htgt@sanger.ac.uk', 'password')

doc = client.find_mi(:colony_name_eq => 'MAAN').first
ap doc
# NOTE: the MI being updated should be from the same production centre
# as you or the update will fail!
client.update_mi( doc['id'], { :qc_loa_qpcr_result => 'fail' } )
ap client.find_mi(:colony_name_eq => 'MAAN').first

ap client.create_mi( :mi_plan_id => 123, :es_cell_name => 'EPD0027_2_B01', :mi_date => '2011-06-17' )

HTTParty

This example uses the "httparty":https://rubygems.org/gems/httparty gem.

#!/usr/bin/env ruby
# encoding: utf-8

require 'rubygems'
require 'httparty'
require 'awesome_print'

class Imits
  include HTTParty
  base_uri 'https://www.i-dcc.org/staging/imits'
  # Use your actual email address here!
  basic_auth 'htgt@sanger.ac.uk', 'password'
  format :json
end

# Find an MI
query = { :production_centre_name_eq => 'WTSI', :es_cell_name_eq => 'EPD0127_4_E01', :qc_loxp_confirmation_result => 'pass', :is_active_eq => 'true' }
doc = Imits.get( '/mi_attempts.json', :query => query ).first
ap doc

# Update the MI
Imits.put( "/mi_attempts/#{doc['id']}.json", :body => { :mi_attempt => { :qc_loa_qpcr_result => 'pass' } } )

# Find the MI again
doc = Imits.get( '/mi_attempts.json', :query => { :colony_name_eq => doc['colony_name'] } ).first
ap doc

# Create a MI
doc = Imits.post( '/mi_attempts.json', :body => { :mi_attempt => { :mi_plan_id => 124, :es_cell_name => 'EPD0027_2_B01', :mi_date => '2011-06-17' } } )
ap doc