-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into Third-Party-Apps
- Loading branch information
Showing
88 changed files
with
5,130 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
# frozen_string_literal: true | ||
|
||
module Chemotion | ||
class ChemicalAPI < Grape::API | ||
include Grape::Kaminari | ||
resource :chemicals do | ||
desc 'update chemicals' | ||
params do | ||
requires :chemical_data, type: Array[Hash], desc: 'chemical data' | ||
optional :cas, type: String, desc: 'cas number' | ||
end | ||
route_param :sample_id do | ||
put do | ||
Chemotion::ChemicalsService.handle_exceptions do | ||
attributes = declared(params, include_missing: false) | ||
if params[:chemical_data].present? || params[:cas].present? | ||
Chemical.find_by(sample_id: params[:sample_id]).update!(attributes) | ||
else | ||
status 204 | ||
end | ||
end | ||
end | ||
end | ||
|
||
desc 'Return chemical by sample_id' | ||
params do | ||
requires :sample_id, type: Integer, desc: 'sample id' | ||
end | ||
|
||
get do | ||
Chemotion::ChemicalsService.handle_exceptions do | ||
Chemical.find_by(sample_id: params[:sample_id]) || Chemical.new | ||
end | ||
end | ||
|
||
resource :create do | ||
desc 'Create a Chemical Entry' | ||
params do | ||
requires :chemical_data, type: Array[Hash], desc: 'chemical data' | ||
requires :cas, type: String | ||
requires :sample_id, type: Integer | ||
end | ||
|
||
post do | ||
Chemotion::ChemicalsService.handle_exceptions do | ||
attributes = declared(params, include_missing: false) | ||
Chemical.create!(attributes) | ||
end | ||
end | ||
end | ||
|
||
resource :fetch_safetysheet do | ||
desc 'fetch safety data sheet' | ||
route_param :id do | ||
params do | ||
requires :data, type: Hash, desc: 'params' | ||
end | ||
get do | ||
Chemotion::ChemicalsService.handle_exceptions do | ||
data = params[:data] | ||
molecule = Molecule.find(params[:id]) | ||
vendor = data[:vendor] | ||
language = data[:language] | ||
case data[:option] | ||
when 'Common Name' | ||
name = data[:searchStr] || molecule.names[0] | ||
when 'CAS' | ||
name = data[:searchStr] || molecule.cas[0] | ||
end | ||
case vendor | ||
when 'Merck' | ||
{ merck_link: Chemotion::ChemicalsService.merck(name, language) } | ||
when 'Thermofisher' | ||
{ alfa_link: Chemotion::ChemicalsService.alfa(name, language) } | ||
else | ||
{ | ||
alfa_link: Chemotion::ChemicalsService.alfa(name, language), | ||
merck_link: Chemotion::ChemicalsService.merck(name, language), | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
resource :save_safety_datasheet do | ||
desc 'save safety data sheet' | ||
|
||
params do | ||
requires :sample_id, type: Integer, desc: 'sample id' | ||
requires :cas, type: String | ||
requires :chemical_data, type: Array[Hash] | ||
optional :vendor_product, type: String | ||
end | ||
post do | ||
Chemotion::ChemicalsService.handle_exceptions do | ||
chemical = Chemical.find_by(sample_id: params[:sample_id]) | ||
product_info = params[:chemical_data][0][params[:vendor_product]] | ||
if chemical.present? | ||
chemical.update!(chemical_data: params[:chemical_data]) | ||
else | ||
Chemical.create!(sample_id: params[:sample_id], cas: params[:cas], chemical_data: params[:chemical_data]) | ||
end | ||
file_path = "#{product_info['productNumber']}_#{product_info['vendor']}.pdf" | ||
Chemotion::ChemicalsService.create_sds_file(file_path, product_info['sdsLink']) | ||
end | ||
end | ||
end | ||
|
||
resources :safety_phrases do | ||
desc 'H and P safety phrases' | ||
|
||
params do | ||
requires :vendor, type: String, desc: 'params' | ||
end | ||
|
||
route_param :sample_id do | ||
get do | ||
Chemotion::ChemicalsService.handle_exceptions do | ||
chemical = Chemical.find_by(sample_id: params[:sample_id]) || Chemical.new | ||
if chemical.chemical_data.present? | ||
if params[:vendor] == 'thermofischer' && chemical.chemical_data[0]['alfaProductInfo'] | ||
product_number = chemical.chemical_data[0]['alfaProductInfo']['productNumber'] | ||
Chemotion::ChemicalsService.safety_phrases_thermofischer(product_number) | ||
elsif params[:vendor] == 'merck' && chemical.chemical_data[0]['merckProductInfo'] | ||
product_link = chemical.chemical_data[0]['merckProductInfo']['productLink'] | ||
Chemotion::ChemicalsService.safety_phrases_merck(product_link) | ||
else | ||
err_body = 'Please fetch and save corresponding safety data sheet first' | ||
err_body | ||
end | ||
else | ||
status 204 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
resources :chemical_properties do | ||
desc 'additional chemical properties' | ||
|
||
params do | ||
requires :link, type: String, desc: 'vendor product link' | ||
end | ||
|
||
get do | ||
Chemotion::ChemicalsService.handle_exceptions do | ||
if params[:link].include? 'alfa' | ||
Chemotion::ChemicalsService.chemical_properties_alfa(params[:link]) | ||
elsif params[:link].include? 'sigmaaldrich' | ||
Chemotion::ChemicalsService.chemical_properties_merck(params[:link]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.