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
|
2015-02-05 20:14:53 +00:00
|
|
|
rescue RestClient::GatewayTimeout => e
|
|
|
|
e.response
|
|
|
|
rescue RestClient::RequestTimeout => e
|
|
|
|
e.response
|
2014-07-14 06:08:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-05 20:14:53 +00:00
|
|
|
def ClassyFireAPI.get_query(query_id,format="json")
|
|
|
|
# format can be either 'json' or 'sdf'
|
2014-07-14 06:08:02 +01:00
|
|
|
begin
|
2015-02-05 20:14:53 +00:00
|
|
|
if format == "json"
|
|
|
|
puts "#{URL}/queries/#{query_id}.json"
|
|
|
|
RestClient.get "#{URL}/queries/#{query_id}.json", :accept => :json
|
|
|
|
elsif format == "sdf"
|
|
|
|
puts "#{URL}/queries/#{query_id}.sdf"
|
|
|
|
RestClient.get "#{URL}/queries/#{query_id}.sdf", :accept => :sdf
|
|
|
|
end
|
2014-07-14 06:08:02 +01:00
|
|
|
rescue RestClient::ResourceNotFound => e
|
|
|
|
e.response
|
|
|
|
rescue RestClient::InternalServerError => e
|
|
|
|
e.response
|
2015-02-05 20:14:53 +00:00
|
|
|
rescue RestClient::GatewayTimeout => e
|
|
|
|
e.response
|
|
|
|
rescue RestClient::RequestTimeout => e
|
|
|
|
e.response
|
2014-07-14 06:08:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-05 20:14:53 +00:00
|
|
|
def ClassyFireAPI.get_entity_classification(inchikey,format="json")
|
|
|
|
# format can be either 'json' or 'sdf'
|
2014-07-14 06:08:02 +01:00
|
|
|
inchikey_id = inchikey.to_s.gsub('InChIKey=','')
|
|
|
|
begin
|
2015-02-05 20:14:53 +00:00
|
|
|
if format == "json"
|
|
|
|
RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :json
|
|
|
|
elsif format == "sdf"
|
|
|
|
RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :sdf
|
|
|
|
end
|
2014-07-14 06:08:02 +01:00
|
|
|
rescue RestClient::ResourceNotFound => e
|
|
|
|
e.response
|
|
|
|
rescue RestClient::InternalServerError => e
|
|
|
|
e.response
|
2015-02-05 20:14:53 +00:00
|
|
|
rescue RestClient::GatewayTimeout => e
|
|
|
|
e.response
|
|
|
|
rescue RestClient::RequestTimeout => e
|
|
|
|
e.response
|
2014-07-14 06:08:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ClassyFireAPI.query_status(query_id)
|
|
|
|
s = JSON.parse(get_query(query_id))
|
|
|
|
if s['error']
|
|
|
|
s
|
2015-02-05 20:14:53 +00:00
|
|
|
elsif s['classification_status:']
|
|
|
|
s['classification_status']
|
2014-07-14 06:08:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|