Rack::AppVersion Middleware
Rack::AppVersion is a middleware that sets the version of an app (Rack compatible web applications) via response header.
Setup
1. In your Gemfile
gem 'rack-app_version'2. In config/application.rb of your Rails application, put the code below.
module YourApp
class Application < Rails::Application
# ...
config.middleware.use Rack::AppVersion
end
end3. In Rakefile of your Rails application, put the code below.
module Rack
class AppVersion
def self.generate_version
# Implement logic of getting application version here.
end
end
end
require 'rack/app_version/rake_task'
Rack::AppVersion.load_tasksDoing bundle exec rake -T | grep app_version will give you the following which you can use:
rake app_version:generate # generate app version and write it in .app_version file
rake app_version:init # generate .app_version file that will contain application version4. Use have_app_version matcher to ensure everything is setup (optional).
This rspec matcher can be used by the application to ensure that everything is properly configured.
it 'has app version in response headers' do
expect(env).to have_app_version
endCommon Gotchas
To determine where to put the Rack::AppVersion middleware in the Rack stack, run the following command:
bundle exec rake middlewareIn many cases, the Rack stack will be different running in production environment. Run the following command to see the middleware stack in production:
RAILS_ENV=production bundle exec rake middlewareSee The Rails Guide to Rack for more details on rack middlewares or watch the railscast.