Skip to content

Commit

Permalink
API for interacting with the Report Generator
Browse files Browse the repository at this point in the history
- using the Grape API
  • Loading branch information
jungrafael committed Oct 7, 2015
1 parent 2192897 commit 3b8c528
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 91 deletions.
30 changes: 30 additions & 0 deletions app/api/chemotion/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative './authentication'

module Chemotion
class Api < Grape::API
prefix 'api'
version 'v1'
format :json
formatter :json, Grape::Formatter::ActiveModelSerializers

# TODO needs to be tested,
# source: http://funonrails.com/2014/03/api-authentication-using-devise-token/
helpers do
def current_user
@current_user = Authentication.new(env).current_user
end

def authenticate!
error!('401 Unauthorized', 401) unless current_user
end
end

before do
authenticate!
end

mount Chemotion::CollectionAPI
mount Chemotion::SampleAPI
mount Chemotion::ReportAPI
end
end
File renamed without changes.
45 changes: 45 additions & 0 deletions app/api/chemotion/report_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Chemotion
class ReportAPI < Grape::API
resource :reports do
desc "Build a report using the contents of a JSON file"

params do
optional :header, type: Hash
requires :body, type: Array do
requires :type, type: String
end
end

post :rtf do
report = Report::RTFReport.new do |r|
r.header {|h| h.build(params[:header])}
params[:body].each do |text_item|
case text_item[:type]
when "line_break"
r.line_break
when "image"
r.add_image do |i|
i.set_path 'data/example.svg'
i.size x: 70, y: 10
end
when "paragraph"
r.add_paragraph {|p| p.build(text_item)}
when "table"
r.add_table(text_item[:data].count, text_item[:data].first.count) do |t|
t.table_data = text_item[:data]
end
else
raise "Fehler: Nicht implementierte funktion: " + text_item[:type]
end
end
end

content_type('text/rtf')

env['api.format'] = :binary

body report.generate_report
end
end
end
end
70 changes: 0 additions & 70 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,4 @@
class PagesController < ApplicationController
def welcome
end

def test
report = Report::RTFReport.new do |r|
r.header do |h|
h.experiment = 'Simone_18-100'
h.owner = 'ELNdmin'
h.created_date = Time.new(2015, 03, 23, 12, 9, 42, "+01:00")
h.printed_date = Time.new(2015, 03, 27, 14, 7, 41, "+01:00")
h.status = 'Open'
end
2.times {r.line_break}
r.add_image do |i|
i.set_path 'data/example.svg'
i.size x: 70, y: 10
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Reaction conditions:', font_style: :bold
end
r.add_table(2, 2) do |t|
t.add_line 'Reaction Molarity', '0,056 molar'
t.add_line 'Pressure', 'Temperature'
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Reactants:', font_style: :bold
end
r.add_table(3, 14) do |t|
t.add_line '', 'Reactant', 'MF', 'Limit?', 'MW', 'Eq', 'Moles (mmol)', 'Sample Mass (g)', 'Vol', 'Molarity', 'd', '% Wt', 'FM', 'Reactant Mass (g)'
t.add_line '1', 'Harz SG-V1892', 'C92H89F3 NO4', 'true', '1329,6 91', '1,00 0', '0,226', '0,300', '', '', '', '', '1329,6 91', '0,300'
t.add_line '2', '2-amino-4-(tert-butyl)phenol', 'C10H15NO', 'false', '165,23 2', '3,00 0', '0,677', '0,112', '', '', '', '', '165,23 2', '0,112'
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Solvents:', font_style: :bold
end
r.add_table(2, 4) do |t|
t.add_line '', 'Name', 'Ratio', 'Volume (ml)'
t.add_line '1', 'Methanol', '', '4'
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Products', font_style: :bold
end
r.add_table(2, 12) do |t|
t.add_line '', 'Product', 'MF', 'Actual Mass', 'Actual Mol', 'Yiel d', 'Purit y', 'MW', 'Eq', 'Theo Mol (mmol)', 'Theo Mass (g)', 'FM'
t.add_line '1', '6-(tert-butyl)-3-phenyl-2H-benzo[b][1,4]-2-one', 'C18H17N O2', '', '', '', '', '279,3 33', '1,00 0', '0,226', '0,063', '279,3 33'
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Preparation', font_style: :bold
p.line_break
p.add_text 'Das Harz SG-V1892 (0,300 g, 0,226 mmol) wird in Methanol (Volume: 4 ml) gequellt und mit 2-amino-4-(tert-butyl)phenol (0,112 g, 0,667 mmol) und aluminum chloride (0,030 g. 0,226 mmol) versetzt. Die Reaktion wird über das Wochenende bei 50C im Vialblock gerührt.'
p.line_break
p.add_text 'Zur Aufarbeitung wird eine Säulenchromatographie durchgeführt.'
p.line_break
p.line_break
p.add_text 'DC: CH/EE 2:1'
p.line_break
p.add_text 'Säule: CH/EE'
p.line_break
p.line_break
p.add_text 'Die Reaktion hat schon reagiert, aber zu wenig zu säulen.'
end
r.line_break
end

#render text: report.to_yaml
send_data report.generate_report, :type => "text/rtf", :filename => 'report.rtf', :x_sendfile => true
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
root to: 'pages#welcome', as: :authenticated_root
end

mount API => '/'
mount Chemotion::Api => '/'

root :to => redirect("/users/sign_in")

Expand Down
64 changes: 64 additions & 0 deletions data/API use example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
report = Report::RTFReport.new do |r|
r.header do |h|
h.experiment = 'Simone_18-100'
h.owner = 'ELNdmin'
h.created_date = Time.new(2015, 03, 23, 12, 9, 42, "+01:00")
h.printed_date = Time.new(2015, 03, 27, 14, 7, 41, "+01:00")
h.status = 'Open'
end
2.times {r.line_break}
r.add_image do |i|
i.set_path 'data/example.svg'
i.size x: 70, y: 10
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Reaction conditions:', font_style: :bold
end
r.add_table(2, 2) do |t|
t.add_line 'Reaction Molarity', '0,056 molar'
t.add_line 'Pressure', 'Temperature'
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Reactants:', font_style: :bold
end
r.add_table(3, 14) do |t|
t.add_line '', 'Reactant', 'MF', 'Limit?', 'MW', 'Eq', 'Moles (mmol)', 'Sample Mass (g)', 'Vol', 'Molarity', 'd', '% Wt', 'FM', 'Reactant Mass (g)'
t.add_line '1', 'Harz SG-V1892', 'C92H89F3 NO4', 'true', '1329,6 91', '1,00 0', '0,226', '0,300', '', '', '', '', '1329,6 91', '0,300'
t.add_line '2', '2-amino-4-(tert-butyl)phenol', 'C10H15NO', 'false', '165,23 2', '3,00 0', '0,677', '0,112', '', '', '', '', '165,23 2', '0,112'
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Solvents:', font_style: :bold
end
r.add_table(2, 4) do |t|
t.add_line '', 'Name', 'Ratio', 'Volume (ml)'
t.add_line '1', 'Methanol', '', '4'
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Products', font_style: :bold
end
r.add_table(2, 12) do |t|
t.add_line '', 'Product', 'MF', 'Actual Mass', 'Actual Mol', 'Yiel d', 'Purit y', 'MW', 'Eq', 'Theo Mol (mmol)', 'Theo Mass (g)', 'FM'
t.add_line '1', '6-(tert-butyl)-3-phenyl-2H-benzo[b][1,4]-2-one', 'C18H17N O2', '', '', '', '', '279,3 33', '1,00 0', '0,226', '0,063', '279,3 33'
end
r.line_break
r.add_paragraph do |p|
p.add_text 'Preparation', font_style: :bold
p.line_break
p.add_text 'Das Harz SG-V1892 (0,300 g, 0,226 mmol) wird in Methanol (Volume: 4 ml) gequellt und mit 2-amino-4-(tert-butyl)phenol (0,112 g, 0,667 mmol) und aluminum chloride (0,030 g. 0,226 mmol) versetzt. Die Reaktion wird über das Wochenende bei 50C im Vialblock gerührt.'
p.line_break
p.add_text 'Zur Aufarbeitung wird eine Säulenchromatographie durchgeführt.'
p.line_break
p.line_break
p.add_text 'DC: CH/EE 2:1'
p.line_break
p.add_text 'Säule: CH/EE'
p.line_break
p.line_break
p.add_text 'Die Reaktion hat schon reagiert, aber zu wenig zu säulen.'
end
r.line_break
end
Loading

0 comments on commit 3b8c528

Please sign in to comment.