Created
November 12, 2014 17:19
-
-
Save mchung/3f53b2fe2a3383437fd6 to your computer and use it in GitHub Desktop.
Sinatra: How to use "settings" to inject dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup the Service::Api | |
Service::Api.set(:environment, :production) | |
Service::Api.set(:root, Service.root) | |
Service::Api.set(:service, Service::Provider.new) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rackup | |
# encoding: utf-8 | |
$stdout.sync = true | |
require "boot" | |
run Rack::URLMap.new('/' => Service::Api) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra/base" | |
module Service | |
class Provider | |
def get_data | |
{:root => "hello, world"} | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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