Ruby.gif

Previous: Supported Languages

Contents


64-warn.gif
NOTE: GoGrid Support WILL NOT troubleshoot customer-modified code. The code snippets and examples provided in this API documentation can be modified to customize your own code, but GoGrid Support cannot troubleshoot any errors you may receive due to missing syntax or illegal arguments. We WILL support our API interface and can troubleshoot any API server-side errors you may be receiving. Please note that all of the code snippets and examples provided in this API documentation have been thoroughly tested and verified as properly working.


GoGridClient.rb

require 'digest/md5'
require 'cgi'
require 'open-uri'

class GoGridClient

  def initialize(server='https://api.gogrid.com/api',
                 apikey='YOUR API KEY',
                 secret='YOUR SHARED SECRET', 
                 format='xml',
                 version='1.0')
    @server = server
    @secret = secret
    @default_params = {'format'=>format, 'v'=>version,'api_key' => apikey}
  end    
  
  def getRequestURL(method,params)
    requestURL = @server+'/'+method+'?'
  	call_params = @default_params.merge(params)
  	call_params['sig']=getSignature(@default_params['api_key'],@secret)
  	requestURL = requestURL+encode_params(call_params)
  end
  
  def getSignature(key,secret)
    Digest::MD5.hexdigest(key+secret+"%.0f"%Time.new.to_f)
  end
  
  def sendAPIRequest(method,params={})
    open(getRequestURL(method,params)).read
  end
  
  def encode_params(params)
    params.map {|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
  end
    
end

GoGridAPIExample.rb

require 'GoGridClient.rb'

# create client with server, secret and key
g = GoGridClient.new('https://api.gogrid.com/api', 'YOUR API KEY', 'YOUR SHARED SECRET')

# make some calls
resp = g.sendAPIRequest("grid/server/list")
puts resp

resp = g.sendAPIRequest("grid/server/list",{'format'=>'xml'})
puts resp

Previous: Supported Languages

Personal tools