Skip to content

Instantly share code, notes, and snippets.

@Crisfole
Last active December 3, 2015 19:04
Show Gist options
  • Save Crisfole/18461b6b42c144ed02c1 to your computer and use it in GitHub Desktop.
Save Crisfole/18461b6b42c144ed02c1 to your computer and use it in GitHub Desktop.
Cuba.is Demo

Demo of Rollbar Middleware in Cuba

require './hello'
run Cuba
source 'https://rubygems.org'
gem 'cuba'
gem 'rollbar'
GEM
remote: https://rubygems.org/
specs:
cuba (3.4.0)
rack
multi_json (1.11.2)
rack (1.6.4)
rollbar (2.6.1)
multi_json
PLATFORMS
ruby
DEPENDENCIES
cuba
rollbar
BUNDLED WITH
1.10.6
require "cuba"
require "cuba/safe"
require "./rollbar_middleware"
Rollbar.configure do |config|
config.access_token = 'POST_SERVER_ITEM_ACCESS_TOKEN'
config.disable_monkey_patch = true # Seems like this isn't working for Cuba, so we'll do it manually
end
Cuba.use Rack::Session::Cookie, :secret => "__a_very_long_string__"
Cuba.use RollbarMiddleware
Cuba.plugin Cuba::Safe
Cuba.define do
on get do
on "hello" do
res.write "Hello world!"
end
on "error" do
raise Exception.new("Ops, you bwoke it")
end
on root do
res.redirect "/hello"
end
end
end
require 'rollbar'
require 'rollbar/exception_reporter'
require 'rollbar/request_data_extractor'
class RollbarMiddleware
include Rollbar::ExceptionReporter
include Rollbar::RequestDataExtractor
def initialize(app)
@app = app
end
def call(env)
Rollbar.reset_notifier! # If anything was changed by a previous request
Rollbar.scoped(fetch_scope(env)) do
begin
response = @app.call(env)
err = framework_error(env, response)
report_exception_to_rollbar(env, err) if err
return response
rescue Exception => e
report_exception_to_rollbar(env, e)
raise
end
end
end
def fetch_scope(env)
{
:request => proc { extract_request_data_from_rack(env) },
:person => nil # Change this to change how you report affected user data.
# Person must have a unique :id property
# Person may have optional :usename, and :email properties
}
end
def framework_error(env, response)
false # If Cuba treats exceptions differently (doesn't re-raise them, for instance,
# instead sticking it in the environemnt or response) return the error here
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment