Skip to content
Browse files

Merge pull request #181 from flant/mechanize

Mechanize + Synchrony
  • Loading branch information...
2 parents 2747676 + dba2990 commit ad17ce925fe49c45e1b5f713910e24d4798ae29c @igrigorik committed Feb 9, 2014
Showing with 27 additions and 0 deletions.
  1. +27 −0 lib/em-synchrony/mechanize.rb
View
27 lib/em-synchrony/mechanize.rb
@@ -0,0 +1,27 @@
+require 'mechanize'
+
+module EventMachine
+ module Synchrony
+ class Mechanize < ::Mechanize
+ def initialize(*args, &blk)
+ super
+ @agent.instance_variable_get(:@http).singleton_class.send(:include, DeferedNetHttpPersistentRequest)
+ end
+
+ module DeferedNetHttpPersistentRequest
+ def self.included(base)
+ base.class_eval do
+ alias :request_without_defer :request
+ alias :request :request_with_defer
+ end
+ end
+
+ def request_with_defer(*args, &blk)
+ EM::Synchrony.defer do
+ request_without_defer(*args, &blk)
+ end
+ end
+ end
+ end
+ end
+end

0 comments on commit ad17ce9

Please sign in to comment.
Something went wrong with that request. Please try again.