classyfire_api/lib/classyfire_api.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

2014-07-14 06:08:02 +01:00
require 'JSON'
require 'RestClient'
module ClassyFireAPI
URL = 'http://classyfire.wishartlab.com'
def ClassyFireAPI.submit_query(label,input)
begin
RestClient.post URL+'/queries', {:label => label, :query_input => input}.to_json, :accept => :json, :content_type => :json
rescue RestClient::BadRequest => e
e.response
rescue RestClient::InternalServerError => e
e.response
end
end
def ClassyFireAPI.get_query(query_id)
begin
puts "#{URL}/queries/#{query_id}.json"
RestClient.get "#{URL}/queries/#{query_id}.json", :accept => :json
rescue RestClient::ResourceNotFound => e
e.response
rescue RestClient::InternalServerError => e
e.response
end
end
def ClassyFireAPI.get_entity_classification(inchikey)
inchikey_id = inchikey.to_s.gsub('InChIKey=','')
begin
RestClient.get "#{URL}/entities/#{inchikey_id}.json", :accept => :json
rescue RestClient::ResourceNotFound => e
e.response
rescue RestClient::InternalServerError => e
e.response
end
end
def ClassyFireAPI.query_status(query_id)
s = JSON.parse(get_query(query_id))
if s['error']
s
elsif s['state']
s['state']
end
end
end