diff --git a/lib/travis/api/app/endpoint.rb b/lib/travis/api/app/endpoint.rb index a00e0865..f350eda5 100644 --- a/lib/travis/api/app/endpoint.rb +++ b/lib/travis/api/app/endpoint.rb @@ -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 } diff --git a/lib/travis/api/app/helpers/json_renderer.rb b/lib/travis/api/app/helpers/json_renderer.rb index 33eacc24..b7c76b2a 100644 --- a/lib/travis/api/app/helpers/json_renderer.rb +++ b/lib/travis/api/app/helpers/json_renderer.rb @@ -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