Skip to content

Instantly share code, notes, and snippets.

@dustMason
Created February 25, 2011 22:35
Show Gist options
  • Save dustMason/844641 to your computer and use it in GitHub Desktop.
Save dustMason/844641 to your computer and use it in GitHub Desktop.
Simple API wrapper for Specify using HTTParty and HTTMultiParty
require 'rubygems'
require 'httparty'
require 'HTTMultiParty'
require 'base64'
# This is a quick example to demonstrate the use of ruby to interact with the Specify API.
# I am a beginner rubyist, so please excuse my rudimentary code!
# Depends on httparty for clean and simple API wrapper code https://github.com/jnunemaker/httparty
# Depends on httpmultiparty to allow image and file uploads https://github.com/jwagener/httmultiparty
# API docs at https://docs.google.com/document/d/1-VcvcuGay67pSmpvITErqngqXfS88xBmBwJ6JSTK-bA/edit?hl=en&authkey=CJLvu8oE
class Specify
include HTTMultiParty
base_uri 'specifyapp.com:443/api'
# base_uri 'specify.localhost/api'
format :json
def initialize(user, pass)
@api_key = self.class.post('/auth', :query => {:username => user, :password => pass})['apiKey']
self.class.basic_auth @api_key, "x"
end
def projects
self.class.get('/projects').parsed_response
end
def itemtypes
self.class.get('/itemtypes').parsed_response
end
def items(project_id)
self.class.get("/project/#{project_id}/items").parsed_response
end
def get_item(project_id,item_id)
self.class.get("/project/#{project_id}/item/#{item_id}").parsed_response
end
def create_item(project_id,item)
self.class.post("/project/#{project_id}/item", { :body => item }).parsed_response
end
def contacts
self.class.get("/contacts").parsed_response
end
end
# EXAMPLES
# 1. Initialize a connection with user credentials
#### Make sure to use a real password!
my_specify = Specify.new('[email protected]', 'xxx')
# 2. Get a list of all projects the user has access to
p my_specify.projects
# 3. Get a list of all items in a project
p my_specify.items(180)
# 4. Get detailed info on a single item
p my_specify.get_item(180,73659)
# 5. Get a list of all contacts
p my_specify.contacts
# 6. Create a new item
#### "item[itemtypeid]" is a required field
#### The "images" field may contain either a publicly accessible URL or Base64-encoded image data
new_specify_item = {
:"item[itemtypeid]" => 1,
:"item[code]" => "RUBY123",
:"item[description]" => "File Test",
:images => Base64.encode64(File.read("face.jpg"))
}
p my_specify.create_item(180,new_specify_item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment