summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormr <mr@mrautenberg.de>2011-09-23 12:10:48 +0200
committermr <mr@mrautenberg.de>2011-09-23 12:10:48 +0200
commitcab3ee76115703e460e1e03b13ca653f5aad27dc (patch)
treeb6dd0c9df03eee5463a959c7ee06cc578f4fccba
parent0c26f05e6ac26dcadfd43d49cb28df9a7033ca3d (diff)
parentb6ad14a839528beab98340beb7942a246a27037d (diff)
Merge branch 'release/v3.0.0'
-rw-r--r--.gitignore1
-rw-r--r--application.rb119
-rw-r--r--data/.gitignore3
3 files changed, 73 insertions, 50 deletions
diff --git a/.gitignore b/.gitignore
index f52f036..d21a58b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ api_key.rb
tmp/*
log/*
public/*
+data/*
diff --git a/application.rb b/application.rb
index 2f231f7..8b42c8c 100644
--- a/application.rb
+++ b/application.rb
@@ -1,12 +1,15 @@
require 'rubygems'
gem "opentox-ruby", "~> 2"
require 'opentox-ruby'
+require 'profiler'
set :lock, true
+@@datadir = "data"
+
helpers do
def next_id
- id = Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last
+ id = Dir["./#{@@datadir}/*json"].collect{|f| File.basename(f.sub(/.json/,'')).to_i}.sort.last
id = 0 if id.nil?
id + 1
end
@@ -22,7 +25,6 @@ helpers do
raise "store subject-id in dataset-object, not in params" if params.has_key?(:subjectid) and @subjectid==nil
content_type = "application/rdf+xml" if content_type.nil?
- #dataset = OpenTox::Dataset.new(@uri, @subjectid)
dataset = OpenTox::Dataset.new(nil, @subjectid)
case content_type
@@ -30,28 +32,33 @@ helpers do
when /yaml/
dataset.load_yaml(input_data)
+ when /json/
+ dataset.load_json(input_data)
+
when /application\/rdf\+xml/
- dataset.load_rdfxml(input_data)
+ dataset.load_rdfxml(input_data, @subjectid)
when "chemical/x-mdl-sdfile"
- dataset.load_sdf(input_data)
+ dataset.load_sdf(input_data, @subjectid)
when /multipart\/form-data/ , "application/x-www-form-urlencoded" # file uploads
case params[:file][:type]
when "chemical/x-mdl-sdfile"
- dataset.load_sdf(input_data)
+ dataset.load_sdf(input_data, @subjectid)
+
+ when /json/
+ dataset.load_json(params[:file][:tempfile].read)
when /yaml/
dataset.load_yaml(params[:file][:tempfile].read)
when "application/rdf+xml"
- dataset.load_rdfxml_file(params[:file][:tempfile])
+ dataset.load_rdfxml_file(params[:file][:tempfile], @subjectid)
when "text/csv"
- #dataset = OpenTox::Dataset.new @uri
- dataset.load_csv(params[:file][:tempfile].read)
+ dataset.load_csv(params[:file][:tempfile].read, @subjectid)
dataset.add_metadata({
DC.title => File.basename(params[:file][:filename],".csv"),
OT.hasSource => File.basename(params[:file][:filename])
@@ -71,7 +78,7 @@ helpers do
else
raise "#{params[:file][:filename]} is not a valid Excel input file."
end
- dataset.load_spreadsheet(book)
+ dataset.load_spreadsheet(book, @subjectid)
dataset.add_metadata({
DC.title => File.basename(params[:file][:filename],extension),
OT.hasSource => File.basename(params[:file][:filename])
@@ -87,11 +94,12 @@ helpers do
dataset.uri = @uri # update uri (also in metdata)
dataset.features.keys.each { |f| dataset.features[f][OT.hasSource] = dataset.metadata[OT.hasSource] unless dataset.features[f][OT.hasSource]}
- File.open("public/#{@id}.yaml","w+"){|f| f.puts dataset.to_yaml}
+ File.open("#{@@datadir}/#{@id}.json","w+"){|f| f.puts dataset.to_json}
end
end
before do
+
@accept = request.env['HTTP_ACCEPT']
@accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil?
@id = request.path_info.match(/^\/\d+/)
@@ -99,14 +107,16 @@ before do
@id = @id.to_s.sub(/\//,'').to_i
@uri = uri @id
- @yaml_file = "public/#{@id}.yaml"
- raise OpenTox::NotFoundError.new "Dataset #{@id} not found." unless File.exists? @yaml_file
+ @json_file = "#{@@datadir}/#{@id}.json"
+ raise OpenTox::NotFoundError.new "Dataset #{@id} not found." unless File.exists? @json_file
extension = File.extname(request.path_info)
unless extension.empty?
case extension
when ".html"
@accept = 'text/html'
+ when ".json"
+ @accept = 'application/json'
when ".yaml"
@accept = 'application/x-yaml'
when ".csv"
@@ -132,7 +142,7 @@ end
# Get a list of available datasets
# @return [text/uri-list] List of available datasets
get '/?' do
- uri_list = Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n"
+ uri_list = Dir["./#{@@datadir}/*json"].collect{|f| File.basename(f.sub(/.json/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n"
case @accept
when /html/
response['Content-Type'] = 'text/html'
@@ -147,43 +157,47 @@ end
# @param [Header] Accept one of `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel` (default application/rdf+xml)
# @return [application/rdf+xml, application-x-yaml, text/csv, application/ms-excel] Dataset representation
get '/:id' do
-
case @accept
when /rdf/ # redland sends text/rdf instead of application/rdf+xml
- file = "public/#{params[:id]}.rdfxml"
+ file = "#{@@datadir}/#{params[:id]}.rdfxml"
unless File.exists? file # lazy rdfxml generation
- dataset = YAML.load_file(@yaml_file)
+ dataset = OpenTox::Dataset.from_json File.read(@json_file)
File.open(file,"w+") { |f| f.puts dataset.to_rdfxml }
end
- response['Content-Type'] = 'application/rdf+xml'
- File.read(file)
+ send_file file, :type => 'application/rdf+xml'
+
+ when /json/
+ send_file @json_file, :type => 'application/x-yaml'
when /yaml/
- response['Content-Type'] = 'application/x-yaml'
- File.read(@yaml_file)
+ file = "#{@@datadir}/#{params[:id]}.yaml"
+ unless File.exists? file # lazy yaml generation
+ dataset = OpenTox::Dataset.from_json File.read(@json_file)
+ File.open(file,"w+") { |f| f.puts dataset.to_yaml }
+ end
+ send_file file, :type => 'application/x-yaml'
- when /html/
+ when /html/
response['Content-Type'] = 'text/html'
- OpenTox.text_to_html File.read(@yaml_file)
+ OpenTox.text_to_html JSON.pretty_generate(JSON.parse(File.read(@json_file)))
when "text/csv"
response['Content-Type'] = 'text/csv'
- YAML.load_file(@yaml_file).to_csv
+ OpenTox::Dataset.from_json(File.read(@json_file)).to_csv
when /ms-excel/
- file = "public/#{params[:id]}.xls"
- YAML.load_file(@yaml_file).to_xls.write(file) unless File.exists? file # lazy xls generation
- response['Content-Type'] = 'application/ms-excel'
- File.open(file).read
+ file = "#{@@datadir}/#{params[:id]}.xls"
+ OpenTox::Dataset.from_json(File.read(@json_file)).to_xls.write(file) unless File.exists? file # lazy xls generation
+ send_file file, :type => 'application/ms-excel'
when /sdfile/
response['Content-Type'] = 'chemical/x-mdl-sdfile'
- YAML.load_file(@yaml_file).to_sdf
+ OpenTox::Dataset.from_json(File.read(@json_file)).to_sdf
- when /uri-list/
- response['Content-Type'] = 'text/uri-list'
- YAML.load_file(@yaml_file).to_urilist
+# when /uri-list/
+# response['Content-Type'] = 'text/uri-list'
+# Yajl::Parser.parse(File.read(@json_file)).to_urilist
else
raise OpenTox::NotFoundError.new "Content-type #{@accept} not supported."
@@ -193,8 +207,7 @@ end
# Get metadata of the dataset
# @return [application/rdf+xml] Metadata OWL-DL
get '/:id/metadata' do
-
- metadata = YAML.load_file(@yaml_file).metadata
+ metadata = OpenTox::Dataset.from_json(File.read(@json_file)).metadata
case @accept
when /rdf/ # redland sends text/rdf instead of application/rdf+xml
@@ -216,9 +229,9 @@ get %r{/(\d+)/feature/(.*)$} do |id,feature|
@id = id
@uri = uri @id
- @yaml_file = "public/#{@id}.yaml"
+ @json_file = "#{@@datadir}/#{@id}.json"
feature_uri = url_for("/#{@id}/feature/#{URI.encode(feature)}",:full) # work around racks internal uri decoding
- metadata = YAML.load_file(@yaml_file).features[feature_uri]
+ metadata = OpenTox::Dataset.from_json(File.read(@json_file)).features[feature_uri]
case @accept
when /rdf/ # redland sends text/rdf instead of application/rdf+xml
@@ -229,6 +242,9 @@ get %r{/(\d+)/feature/(.*)$} do |id,feature|
when /yaml/
response['Content-Type'] = 'application/x-yaml'
metadata.to_yaml
+ when /json/
+ response['Content-Type'] = 'application/json'
+ Yajl::Encoder.encode(metadata)
end
end
@@ -238,7 +254,7 @@ end
# @return [application/rdf+xml, application-x-yaml, text/uri-list] Feature list
get '/:id/features' do
- features = YAML.load_file(@yaml_file).features
+ features = OpenTox::Dataset.from_json(File.read(@json_file)).features
case @accept
when /rdf/ # redland sends text/rdf instead of application/rdf+xml
@@ -249,6 +265,9 @@ get '/:id/features' do
when /yaml/
response['Content-Type'] = 'application/x-yaml'
features.to_yaml
+ when /json/
+ response['Content-Type'] = 'application/json'
+ Yajl::Encoder.encode(features)
when "text/uri-list"
response['Content-Type'] = 'text/uri-list'
features.keys.join("\n") + "\n"
@@ -259,7 +278,7 @@ end
# @return [text/uri-list] Feature list
get '/:id/compounds' do
response['Content-Type'] = 'text/uri-list'
- YAML.load_file(@yaml_file).compounds.join("\n") + "\n"
+ OpenTox::Dataset.from_json(File.read(@json_file)).compounds.join("\n") + "\n"
end
# Create a new dataset.
@@ -276,22 +295,22 @@ end
# @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel`
# @return [text/uri-list] Task URI or Dataset URI (empty datasets)
post '/?' do
-
+
response['Content-Type'] = 'text/uri-list'
# it could be that the read function works only once!, store in varible
input_data = request.env["rack.input"].read
@id = next_id
@uri = uri @id
- @yaml_file = "public/#{@id}.yaml"
+ @json_file = "#{@@datadir}/#{@id}.json"
if params.size == 0 and input_data.size==0
- File.open(@yaml_file,"w+"){|f| f.puts OpenTox::Dataset.new(@uri).to_yaml}
- OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @yaml_file
+ File.open(@json_file,"w+"){|f| f.puts OpenTox::Dataset.new(@uri).to_json}
+ OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @json_file
@uri
else
task = OpenTox::Task.create("Converting and saving dataset ", @uri) do
load_dataset @id, params, request.content_type, input_data
- OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @yaml_file
+ OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @json_file
@uri
end
raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled"
@@ -311,10 +330,9 @@ end
# @param [optional] file, for file uploads, Content-type should be multipart/form-data, please specify the file type `application/rdf+xml, application-x-yaml, text/csv, application/ms-excel`
# @return [text/uri-list] Task ID
post '/:id' do
- LOGGER.debug @uri
response['Content-Type'] = 'text/uri-list'
task = OpenTox::Task.create("Converting and saving dataset ", @uri) do
- FileUtils.rm Dir["public/#{@id}.*"]
+ FileUtils.rm Dir["#{@@datadir}/#{@id}.*"]
load_dataset @id, params, request.content_type, request.env["rack.input"].read
@uri
end
@@ -325,10 +343,10 @@ end
# Delete a dataset
# @return [text/plain] Status message
delete '/:id' do
- LOGGER.debug "deleting dataset with id "+@id.to_s
+ LOGGER.debug "deleting dataset with id #{@id}"
begin
- FileUtils.rm Dir["public/#{@id}.*"]
- if @subjectid and !File.exists? @yaml_file and @uri
+ FileUtils.rm Dir["#{@@datadir}/#{@id}.*"]
+ if @subjectid and !File.exists? @json_file and @uri
begin
res = OpenTox::Authorization.delete_policies_from_uri(@uri, @subjectid)
LOGGER.debug "Policy deleted for Dataset URI: #{@uri} with result: #{res}"
@@ -346,9 +364,10 @@ end
# Delete all datasets
# @return [text/plain] Status message
delete '/?' do
- FileUtils.rm Dir["public/*.rdfxml"]
- FileUtils.rm Dir["public/*.xls"]
- FileUtils.rm Dir["public/*.yaml"]
+ FileUtils.rm Dir["#{@@datadir}/*.rdfxml"]
+ FileUtils.rm Dir["#{@@datadir}/*.xls"]
+ FileUtils.rm Dir["#{@@datadir}/*.yaml"]
+ FileUtils.rm Dir["#{@@datadir}/*.json"]
response['Content-Type'] = 'text/plain'
"All datasets deleted."
end
diff --git a/data/.gitignore b/data/.gitignore
new file mode 100644
index 0000000..59e3edd
--- /dev/null
+++ b/data/.gitignore
@@ -0,0 +1,3 @@
+*.rdfxml
+*.xls
+*.yaml