Permalink
Browse files

example of mounting an async Sinatra app under rails 3

  • Loading branch information...
1 parent fb9ce25 commit 50c5e4fd6701dfa2b3ecfc697ca53b40f8c57827 @igrigorik committed Jun 20, 2010
Showing with 14 additions and 0 deletions.
  1. +1 −0 Gemfile
  2. +1 −0 README.md
  3. +8 −0 app/metal/twitter.rb
  4. +4 −0 config/routes.rb
View
@@ -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'
View
@@ -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:
View
@@ -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("</br>")
+ tweets
+ end
+end
View
@@ -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

4 comments on commit 50c5e4f

Would there be any advantage to using https://github.com/raggi/async_sinatra here?

Owner

igrigorik replied Jan 29, 2011

Nah, not in this case. Since sinatra is this case is running within a thin server, the net effect is effectively the same.. minus the spaghetti code.

Ahh right, makes sense. It's not as if regular Sinatra is blocking anywhere in your example. And EM::HttpRequest is using 1.9 fibers to avoid callback-littered code, right?

Thanks!

Owner

igrigorik replied Jan 29, 2011

Yep, you got it.

Please sign in to comment.