diff options
author | rautenberg <rautenberg@in-silico.ch> | 2016-09-01 11:12:18 +0200 |
---|---|---|
committer | rautenberg <rautenberg@in-silico.ch> | 2016-09-01 11:12:18 +0200 |
commit | 24e0b815b716af488d6b5fb2f66c30e59cdb265b (patch) | |
tree | ad92d7aaef43fd99e8125841026920b13524db81 | |
parent | f96a6cdfed2b41a3262239a125fc8302ec548914 (diff) |
add route for substance
-rw-r--r-- | api/api.json | 89 | ||||
-rw-r--r-- | lib/substance.rb | 25 |
2 files changed, 114 insertions, 0 deletions
diff --git a/api/api.json b/api/api.json index f64959c..0ad3be8 100644 --- a/api/api.json +++ b/api/api.json @@ -284,6 +284,91 @@ } } }, + "/substance": { + "get": { + "tags": [ + "substance" + ], + "description": "Get a list of all substances", + "parameters": [ + { + "name": "accept", + "in": "header", + "description": "requested Content-Type", + "required": true, + "type": "string", + "enum": [ + "text/uri-list", + "application/json" + ] + } + ], + "produces": [ + "text/uri-list", + "application/json" + ], + "responses": { + "200": { + "$ref": "#/responses/200" + }, + "400": { + "$ref": "#/responses/400" + }, + "401": { + "$ref": "#/responses/401" + }, + "404": { + "$ref": "#/responses/404" + }, + "500": { + "$ref": "#/responses/500" + } + } + } + }, + "/substance/{id}": { + "get": { + "tags": [ + "substance" + ], + "description": "Get substance representation", + "parameters": [ + { + "name": "Content-Type", + "in": "header", + "description": "body Content-Type", + "required": true, + "type": "string", + "enum": [ + "application/json" + ] + }, + { + "$ref": "#/parameters/id" + } + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "$ref": "#/responses/200" + }, + "400": { + "$ref": "#/responses/400" + }, + "401": { + "$ref": "#/responses/401" + }, + "403": { + "$ref": "#/responses/403" + }, + "404": { + "$ref": "#/responses/404" + } + } + } + }, "/validation": { "get": { "tags": [ @@ -718,6 +803,10 @@ { "name": "validation", "description": "Validation" + }, + { + "name": "substance", + "description": "Substance" } ] }
\ No newline at end of file diff --git a/lib/substance.rb b/lib/substance.rb new file mode 100644 index 0000000..1116e65 --- /dev/null +++ b/lib/substance.rb @@ -0,0 +1,25 @@ +# Get all substances +get "/substance/?" do + substances = Substance.all + case @accept + when "text/uri-list" + uri_list = substances.collect{|substance| uri("/substance/#{substance.id}")} + return uri_list.join("\n") + "\n" + when "application/json" + substances = JSON.parse substances.to_json + substances.each_index do |idx| + substances[idx][:URI] = uri("/substance/#{substances[idx]["_id"]["$oid"]}") + end + return substances.to_json + else + bad_request_error "Mime type #{@accept} is not supported." + end +end + +# Get a substance +get "/substance/:id/?" do + substance = Substance.find :id => params[:id] + resource_not_found_error "Substance with id: #{params[:id]} not found." unless substance + substance[:URI] = uri("/substance/#{substance.id}") + return substance.to_json +end
\ No newline at end of file |