Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
/api/version route; exposes API semantic version
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Bargnesi committed Jun 6, 2016
1 parent 12af9ce commit d023769
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/openbel/api/app.rb
Expand Up @@ -15,12 +15,13 @@
require_relative 'routes/base'
require_relative 'routes/root'
require_relative 'routes/annotations'
require_relative 'routes/nanopubs'
require_relative 'routes/authenticate'
require_relative 'routes/datasets'
require_relative 'routes/expressions'
require_relative 'routes/language'
require_relative 'routes/namespaces'
require_relative 'routes/authenticate'
require_relative 'routes/nanopubs'
require_relative 'routes/version'
require_relative 'middleware/auth'

module OpenBEL
Expand Down Expand Up @@ -72,6 +73,7 @@ class Server < Sinatra::Application
use OpenBEL::Routes::Expressions
use OpenBEL::Routes::Language
use OpenBEL::Routes::Namespaces
use OpenBEL::Routes::Version

# routes requiring authentication
if OpenBEL::Settings[:auth][:enabled]
Expand Down
63 changes: 63 additions & 0 deletions app/openbel/api/routes/version.rb
@@ -0,0 +1,63 @@
require 'openbel/api/version'

module OpenBEL
module Routes
# Version defines and implements the _/api/version_ route that exposes
# the semantic version of the OpenBEL API.
class Version < Base

JSON = Rack::Mime.mime_type('.json')
HAL = 'application/hal+json'
TEXT = Rack::Mime.mime_type('.txt')
ACCEPTED_TYPES = {
'hal' => HAL,
'json' => JSON,
'text' => TEXT
}
DEFAULT_TYPE = TEXT

helpers do
def requested_media_type
if params && params[:format]
ACCEPTED_TYPES[params[:format]]
else
request.accept.flat_map { |accept_entry|
ACCEPTED_TYPES.values.find { |type| type == accept_entry.entry }
}.compact.first
end
end
end

options '/api/version' do
response.headers['Allow'] = 'OPTIONS,GET'
status 200
end

get '/api/version' do
accept_type = requested_media_type || DEFAULT_TYPE

case accept_type
when TEXT
response.headers['Content-Type'] = 'text/plain'
OpenBEL::Version.to_s
when HAL, JSON
response.headers['Content-Type'] = 'application/hal+json'
MultiJson.dump({
:version => {
:string => OpenBEL::Version.to_s,
:semantic_version => {
:major => OpenBEL::Version::MAJOR,
:minor => OpenBEL::Version::MINOR,
:patch => OpenBEL::Version::PATCH
}
}
})
else
halt 406
end
end
end
end
end
# vim: ts=2 sw=2:
# encoding: utf-8

0 comments on commit d023769

Please sign in to comment.