Skip to content

Commit

Permalink
example of mounting an async Sinatra app under rails 3
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Jun 20, 2010
1 parent fb9ce25 commit 50c5e4f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions 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'
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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:

Expand Down
8 changes: 8 additions & 0 deletions 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("</br>")
tweets
end
end
4 changes: 4 additions & 0 deletions config/routes.rb
Expand Up @@ -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

@agibralter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@igrigorik
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@agibralter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@igrigorik
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you got it.

Please sign in to comment.