Skip to content

Instantly share code, notes, and snippets.

@jch
Created December 1, 2011 05:00
Show Gist options
  • Save jch/1413808 to your computer and use it in GitHub Desktop.
Save jch/1413808 to your computer and use it in GitHub Desktop.
Grape Path and Header Versioning

Grape Path and Header Versioning

Illustrates how path and header versioning can be used side by side for different versions. See this blog post for the original discussion

To run example:

bundle
ruby api.rb
require 'bundler'
Bundler.require :default
require 'grape'
require 'rack-test'
class API < Grape::API
version 'v2', using: :header, vendor: 'example'
get '/hello' do
"I am version 2"
end
version 'v1', using: :path
get '/hello' do
"I am version 1"
end
end
include Rack::Test::Methods
def app
API
end
get '/v1/hello'
puts "with path version v1: " + last_response.body
puts
get '/hello'
puts "with no version specified in header, defaults to latest available version: " + last_response.body
puts
get '/hello', {}, {'HTTP_ACCEPT' => 'application/vnd.example-v2+txt'}
puts "with header version v2: " + last_response.body
puts
source 'http://rubygems.org'
gem 'grape', :git => '[email protected]:intridea/grape.git'
gem 'rack'
gem 'rack-test'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment