Add versioning support to your Crepe APIs.
In your Gemfile:
gem 'crepe-versioning'
crepe-versioning supports three different versioning strategies:
- In the path (the default)
- In the Accept header
- In a query parameter
require 'crepe'
require 'crepe/versioning'
# Demonstrates versioning your API in several ways:
#
# $ curl 0.0.0.0:9292/?v=v1
# {"message":"Version 1"}
# $ curl 0.0.0.0:9292/v2
# {"message":"Version 2"}
# $ curl -H 'Accept: application/vnd.crepe-3+json' 0.0.0.0:9292
# {"message":"Version 3"}
class Versioned < Crepe::API
# Equivalent to `version :v2 do ... end`
version :v2, with: :path do
get do
{ message: 'Version 2' }
end
end
# Experimental version 3
version :v3, with: :header, vendor: 'crepe' do
get do
{ message: 'Version 3' }
end
end
version :v1, with: :query, name: 'v' do
get do
{ message: 'Version 1' }
end
end
end
run Versioned
The first defined version will be the default version (:v2
, in the above case). You can also set namespace-wide configuration for your versions, including setting a default. For example:
require 'crepe'
require 'crepe/versioning'
# Demonstrates less repetitive version configuration:
#
# $ curl 0.0.0.0:9292
# {"message":"Version 2"}
# $ curl -H 'Accept: application/vnd.crepe-1+json' 0.0.0.0:9292
# {"message":"Version 1"}
class Versioned < Crepe::API
version with: :header, vendor: 'crepe', default: :v2
version :v1 do
get do
{ message: 'Version 1' }
end
end
version :v2 do
get do
{ message: 'Version 2' }
end
end
end
run Versioned
crepe-versioning is licensed under The MIT License.