This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/api/version route; exposes API semantic version
- Loading branch information
Anthony Bargnesi
committed
Jun 6, 2016
1 parent
12af9ce
commit d023769
Showing
2 changed files
with
67 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |