This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Updated Gist model to use asynchronous HTTP requests. Updated applica…

…tion to use Rack::FiberPool
  • Loading branch information...
aarongough committed Jun 12, 2010
1 parent 6a436f0 commit d740bea4182b9787349be945a345218bfb8873e4
Showing with 16 additions and 10 deletions.
  1. +3 −1 Gemfile
  2. +10 −9 app/models/gist.rb
  3. +1 −0 config.ru
  4. +1 −0 config/environments/development.rb
  5. +1 −0 config/environments/production.rb
View
@@ -1,9 +1,11 @@
source 'http://rubygems.org'
gem 'rails', '3.0.0.beta4'
gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'rack-fiber_pool', :require => 'rack/fiber_pool'
gem 'heroku'
gem 'rush'
gem 'em-synchrony'
gem 'em-http-request', :require => 'em-http'
group :test do
gem 'webmock'
View
@@ -1,5 +1,4 @@
require 'net/http'
require 'uri'
require 'em-synchrony/em-http'
class Gist
@@ -12,14 +11,16 @@ def self.gist_embed_url_for_id(id)
end
def self.gist_embed_code(id)
begin
github_response = Net::HTTP.get_response(URI.parse(self.gist_embed_url_for_id(id)))
rescue SocketError, Errno::ENETDOWN
return notice("There was a network error while trying to contact the GitHub Gist service.")
if(EventMachine.reactor_running?)
github_response = EventMachine::HttpRequest.new(self.gist_embed_url_for_id(id)).get
else
EventMachine.synchrony do
github_response = EventMachine::HttpRequest.new(self.gist_embed_url_for_id(id)).get
EventMachine.stop
end
end
case github_response
when Net::HTTPSuccess then github_response.body
when Net::HTTPNotModified then github_response.body
if(github_response.response)
github_response.response
else
notice("This Gist is no longer available, please check your URL")
end
View
@@ -1,4 +1,5 @@
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
use Rack::FiberPool
run AjaxGist::Application
@@ -16,4 +16,5 @@
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
config.threadsafe!
end
@@ -43,4 +43,5 @@
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
config.threadsafe!
end

0 comments on commit d740bea

Please sign in to comment.