From 50c5e4fd6701dfa2b3ecfc697ca53b40f8c57827 Mon Sep 17 00:00:00 2001 From: Ilya Grigorik Date: Sat, 19 Jun 2010 22:29:23 -0400 Subject: [PATCH] example of mounting an async Sinatra app under rails 3 --- Gemfile | 1 + README.md | 1 + app/metal/twitter.rb | 8 ++++++++ config/routes.rb | 4 ++++ 4 files changed, 14 insertions(+) create mode 100644 app/metal/twitter.rb diff --git a/Gemfile b/Gemfile index aeeae3e..2fcdf4a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ source 'http://rubygems.org' gem 'rails', '3.0.0.beta4' +gem 'sinatra' # async wrappers gem 'rack-fiber_pool', :require => 'rack/fiber_pool' diff --git a/README.md b/README.md index 4e1da73..d96858c 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Simple async demo stack with Rails 3 + EventMachine and Fibers. * Hit localhost:3000/widgets to do a 1s async mysql query * Hit localhost:3000/widgets/http to make an HTTP call back to /widgets - recursive! :-) + * Hit localhost:3000/twitter to load a mounted async Sinatra app (reports latests rails 3 tweets) Requirements: diff --git a/app/metal/twitter.rb b/app/metal/twitter.rb new file mode 100644 index 0000000..090a224 --- /dev/null +++ b/app/metal/twitter.rb @@ -0,0 +1,8 @@ +class Twitter < Sinatra::Base + get '/twitter' do + http = EM::HttpRequest.new("http://search.twitter.com/search?q=rails+3&format=json").get + tweets = ActiveSupport::JSON.decode(http.response) + tweets = tweets['results'].collect {|t| t['text'] }.join("
") + tweets + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index be35c0e..7a1d829 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,5 +54,9 @@ # This is a legacy wild controller route that's not recommended for RESTful applications. # Note: This route will make all actions in every controller accessible via GET requests. + + # mount async sinatra app (in metal folder) + match '/twitter', :to => Twitter + match ':controller(/:action(/:id(.:format)))' end