Skip to content

Instantly share code, notes, and snippets.

@mchung
Created November 12, 2014 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchung/3f53b2fe2a3383437fd6 to your computer and use it in GitHub Desktop.
Save mchung/3f53b2fe2a3383437fd6 to your computer and use it in GitHub Desktop.
Sinatra: How to use "settings" to inject dependencies
# Setup the Service::Api
Service::Api.set(:environment, :production)
Service::Api.set(:root, Service.root)
Service::Api.set(:service, Service::Provider.new)
#!/usr/bin/env rackup
# encoding: utf-8
$stdout.sync = true
require "boot"
run Rack::URLMap.new('/' => Service::Api)
require "sinatra/base"
module Service
class Provider
def get_data
{:root => "hello, world"}
end
end
end
require "sinatra/base"
module Service
class Api < Sinatra::Base
before do
content_type :json
end
get "/" do
settings.service.get_data.to_json
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment