Created
October 25, 2012 20:34
-
-
Save apeckham/3955222 to your computer and use it in GitHub Desktop.
simple sinatra-synchrony/postgres example
This file contains 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 'json' | |
class App < Sinatra::Base | |
register Sinatra::Synchrony | |
use Rack::JSONP | |
before { content_type 'application/json' } | |
get '/' do | |
"hello async".to_json | |
end | |
get '/delay/:n' do |n| | |
EM::Synchrony.sleep n.to_i | |
"delayed for #{n} seconds".to_json | |
end | |
get '/db/:n' do |n| | |
DB.query("SELECT NOW(), pg_sleep(#{n.to_i})") do |result| | |
result.first.to_json | |
end | |
end | |
end |
This file contains 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
Bundler.require | |
require './app' | |
DB = EM::Synchrony::ConnectionPool.new(size: 10) do | |
uri = URI.parse(ENV['DATABASE_URL']) | |
PG::EM::Client.new(host: uri.host, port: uri.port, user: uri.user, password: uri.password, dbname: uri.path[1..-1]) | |
end | |
run App |
This file contains 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
source :rubygems | |
gem 'thin' | |
gem 'sinatra-synchrony' | |
gem 'em-pg-client', require: %w'pg/em em-synchrony/pg' | |
gem 'rack-contrib' | |
gem 'rspec' | |
gem 'rack-test' |
This file contains 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
GEM | |
remote: http://rubygems.org/ | |
specs: | |
addressable (2.3.2) | |
cookiejar (0.3.0) | |
daemons (1.1.9) | |
diff-lcs (1.1.3) | |
em-http-request (1.0.3) | |
addressable (>= 2.2.3) | |
cookiejar | |
em-socksify | |
eventmachine (>= 1.0.0.beta.4) | |
http_parser.rb (>= 0.5.3) | |
em-pg-client (0.2.1) | |
eventmachine (>= 0.12.10) | |
pg (>= 0.13.2) | |
em-resolv-replace (1.1.3) | |
em-socksify (0.2.1) | |
eventmachine (>= 1.0.0.beta.4) | |
em-synchrony (1.0.2) | |
eventmachine (>= 1.0.0.beta.1) | |
eventmachine (1.0.0) | |
http_parser.rb (0.5.3) | |
pg (0.14.1) | |
rack (1.4.1) | |
rack-contrib (1.1.0) | |
rack (>= 0.9.1) | |
rack-fiber_pool (0.9.2) | |
rack-protection (1.2.0) | |
rack | |
rack-test (0.6.2) | |
rack (>= 1.0) | |
rspec (2.11.0) | |
rspec-core (~> 2.11.0) | |
rspec-expectations (~> 2.11.0) | |
rspec-mocks (~> 2.11.0) | |
rspec-core (2.11.1) | |
rspec-expectations (2.11.3) | |
diff-lcs (~> 1.1.3) | |
rspec-mocks (2.11.3) | |
sinatra (1.3.3) | |
rack (~> 1.3, >= 1.3.6) | |
rack-protection (~> 1.2) | |
tilt (~> 1.3, >= 1.3.3) | |
sinatra-synchrony (0.4.1) | |
em-http-request (~> 1.0) | |
em-resolv-replace (~> 1.1) | |
em-synchrony (~> 1.0.1) | |
eventmachine (~> 1.0.0) | |
rack-fiber_pool (~> 0.9) | |
sinatra (~> 1.0) | |
thin (1.5.0) | |
daemons (>= 1.0.9) | |
eventmachine (>= 0.12.6) | |
rack (>= 1.0.0) | |
tilt (1.3.3) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
em-pg-client | |
rack-contrib | |
rack-test | |
rspec | |
sinatra-synchrony | |
thin |
This file contains 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
Bundler.require | |
require './app' | |
App.set :environment, :test | |
Sinatra::Synchrony.patch_tests! | |
describe App do | |
include Rack::Test::Methods | |
def app | |
App | |
end | |
it 'says hello' do | |
get '/' | |
last_response.ok?.should be_true | |
last_response.body.should == '"hello async"' | |
end | |
it 'says hello' do | |
get '/delay/1' | |
last_response.ok?.should be_true | |
last_response.body.should == '"delayed for 1 seconds"' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment