Permalink
Browse files

Http calls optionally async with EM

  • Loading branch information...
1 parent 96e96ef commit 2c1ee5a9afafbb3de5afd37193543edb891dfd34 @mikelewis committed Dec 18, 2011
Showing with 67 additions and 0 deletions.
  1. +23 −0 lib/tire/http/clients/em-http-request.rb
  2. +40 −0 test/unit/http_client_test.rb
  3. +4 −0 tire.gemspec
@@ -0,0 +1,23 @@
+require 'em-synchrony'
+require "em-synchrony/em-http"
+
+module Tire
+ module HTTP
+ module Client
+ class EMHttpRequest
+ %w{get post put delete head}.each do |type|
+ class_eval <<-STR
+ def self.#{type}(url, data=nil)
+ opts = {}
+ if data
+ opts[:body] = data
+ end
+ r = EventMachine::HttpRequest.new(url).#{type}(opts)
+ Response.new r.response, r.response_header.status
+ end
+ STR
+ end
+ end
+ end
+ end
+end
@@ -1,5 +1,6 @@
require 'test_helper'
require 'tire/http/clients/curb'
+require 'tire/http/clients/em-http-request'
module Tire
module HTTP
@@ -52,6 +53,45 @@ class ClientTest < Test::Unit::TestCase
end
+ context "EM Http Request" do
+ setup do
+ Configuration.client Client::EMHttpRequest
+ end
+
+ teardown do
+ Configuration.client Client::RestClient
+ end
+
+
+ should "respond to HTTP methods" do
+ assert_respond_to Client::EMHttpRequest, :get
+ assert_respond_to Client::EMHttpRequest, :post
+ assert_respond_to Client::EMHttpRequest, :put
+ assert_respond_to Client::EMHttpRequest, :delete
+ assert_respond_to Client::EMHttpRequest, :head
+ end
+
+ should "use POST method if request body passed" do
+ response = nil
+
+ EM.synchrony do
+ response = Configuration.client.post "http://localhost:3000", '{ "query_string" : { "query" : "apple" }}'
+ EM.stop
+ end
+ assert_equal response.class, Response
+ end
+
+ should "use GET method if request body is nil" do
+ response = nil
+ EM.synchrony do
+ response = Configuration.client.get "http://localhost:9200/articles/article/1"
+ EM.stop
+ end
+ assert_equal response.class, Response
+ end
+
+ end
+
end
end
View
@@ -43,6 +43,10 @@ Gem::Specification.new do |s|
s.add_development_dependency "sqlite3"
s.add_development_dependency "supermodel"
s.add_development_dependency "curb"
+ s.add_development_dependency "eventmachine"
+ s.add_development_dependency "em-synchrony"
+ s.add_development_dependency "em-http-request"
+
# These gems are not needed for CI at <http://travis-ci.org/#!/karmi/tire>
#

0 comments on commit 2c1ee5a

Please sign in to comment.