Skip to main content

Ruby frameworks

Application frameworks are software libraries that provide a fundamental structure to support the development of applications for a specific environment.

Follow these guidelines based on your framework:

Configure with Rails

If you are using Rails, the Ruby agent functions as a Railtie so no additional configuration is required.

Configure with Sinatra

If you are using the Sinatra framework, you must configure your application to use the Ruby agent. A simple application configured to work with the Ruby agent looks like the following example:

require 'sinatra'
require 'contrast-agent'

class App < Sinatra::Base
  use Contrast::Agent::Middleware, true
end

Configure Sinatra with config.ru

If there is no class extending, Sinatra::Base or config.ru is the default way of racking up your application with the following configuration:

config.ru

# frozen_string_literal: true

# Require Sinatra early for Framework support to detect it.
require 'sinatra'

# example app.rb, could be any file implementing Sinatra 
# endpoints and logic.
#
# For instance:
#
#   # frozen_string_literal: true
#
#   require 'sinatra'
#
#    get '/frank-says' do
#      'Put this in your pipe & smoke it!'
#    end
#
require './app.rb'

# Contrast Agent needs to be required after sinatra.
require 'contrast-agent'

# Example for requiring gems:
require 'bundler/setup'
Bundler.require(:default)

# Add Contrast Agent middleware to the rack stack:
use Contrast::Agent::Middleware, true

# Run Sinatra application:
run Sinatra::Application

Start the application with:

bundle exec rackup

OR

bundle exec rackup config.ru

Configure with Grape

If you are using the Grape framework, you must configure your application to use the Ruby agent. A simple application configured to work with the Ruby agent looks like the following example:

require 'grape'
require 'contrast-agent'

class App < Grape::API
  use Contrast::Agent::Middleware, true
end

Configure Grape with config.ru

If there is no class extending, Grape::API or config.ru is the default way of racking up your application with the following configuration:

config.ru

# frozen_string_literal: true

require 'rack'
# Require Grape early for Framework support to detect it.
require 'Grape'

# example app.rb, could be any file implementing Grape 
# endpoints and logic.
#
# For instance:
#
# # frozen_string_literal: true
#
# require 'Grape'
#
# class App
#   def initialize
#      @filenames = ['', '.html', 'index.html', '/index.html']
#      @rack_static = ::Rack::Static.new(
#        lambda { [404, {}, []] },
#        root: File.expand_path('../public', __dir__),
#        urls: ['/']
#      )
#    end
#
#    def self.instance
#      @instance ||= Rack::Builder.new do
#        use Rack::Cors do
#          allow do
#            origins '*'
#            resource '*', headers: :any, methods: :get
#          end
#        end
#
#        run App.new
#      end.to_app
#    end

#   def call(env)
#     # Grape::API impleted in API module: 
#     API.call(env)
#      # handle response
#     ......
#    end
# end
#
require './app.rb'

# Contrast Agent needs to be required after Grape.
require 'contrast-agent'

# Example for requiring gems:
require 'bundler/setup'
Bundler.require(:default)

# Add Contrast Agent middleware to the rack stack:
use Contrast::Agent::Middleware, true

# Run Grape application:
run App.instance

Start the application with:

bundle exec rackup

OR

bundle exec rackup config.ru

See also