figure out the api version from HTTP_ACCEPT

This commit is contained in:
Sven Fuchs 2012-10-01 14:58:06 +02:00
parent 8845263244
commit dfc6a1273e
2 changed files with 10 additions and 2 deletions

View File

@ -9,7 +9,7 @@ class Travis::Api::App
set(:prefix) { "/" << name[/[^:]+$/].underscore }
set disable_root_endpoint: false
register :scoping
helpers :current_user # :services
helpers :current_user
before { content_type :json }
error(ActiveRecord::RecordNotFound, Sinatra::NotFound) { not_found }

View File

@ -6,6 +6,9 @@ class Travis::Api::App
# convert (in addition to the return values supported by Sinatra, of
# course). These values will be encoded in JSON.
module JsonRenderer
ACCEPT_VERSION = /vnd\.travis-ci\.(\d+)\+/
DEFAULT_VERSION = 'v1'
def respond_with(resource, options = {})
halt render_json(resource, options)
end
@ -18,7 +21,7 @@ class Travis::Api::App
private
def render_json(resource, options = {})
options[:version] ||= 'v2' # TODO: Content negotiation
options[:version] ||= api_version
options[:params] ||= params
builder = Travis::Api.builder(resource, options)
@ -27,6 +30,11 @@ class Travis::Api::App
resource
end
def api_version
accept = request.env['HTTP_ACCEPT'] || ''
accept =~ ACCEPT_VERSION && "v#{$1}" || DEFAULT_VERSION
end
end
end
end