Skip to content

Commit

Permalink
added convenience methods to request to fire off single requests sync…
Browse files Browse the repository at this point in the history
…hronously
  • Loading branch information
pauldix committed Oct 27, 2009
1 parent 3b37387 commit ceeadd2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/typhoeus/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,28 @@ def handled_response
def cache_key
Digest::SHA1.hexdigest(url)
end

def self.run(url, params)
r = new(url, params)
Typhoeus::Hydra.hydra.queue r
Typhoeus::Hydra.hydra.run
r.response
end

def self.get(url, params)
run(url, params.merge(:method => :get))
end

def self.post(url, params)
run(url, params.merge(:method => :post))
end

def self.put(url, params)
run(url, params.merge(:method => :put))
end

def self.delete(url, params)
run(url, params.merge(:method => :delete))
end
end
end
27 changes: 27 additions & 0 deletions spec/typhoeus/request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe "request" do
describe "quick request methods" do
it "can run a GET synchronously" do
response = Typhoeus::Request.get("http://localhost:3000", :params => {:q => "hi"}, :headers => {:foo => "bar"})
response.code.should == 200
JSON.parse(response.body)["REQUEST_METHOD"].should == "GET"
end

it "can run a POST synchronously" do
response = Typhoeus::Request.post("http://localhost:3000", :params => {:q => "hi"}, :headers => {:foo => "bar"})
response.code.should == 200
JSON.parse(response.body)["REQUEST_METHOD"].should == "POST"
end

it "can run a PUT synchronously" do
response = Typhoeus::Request.put("http://localhost:3000", :params => {:q => "hi"}, :headers => {:foo => "bar"})
response.code.should == 200
JSON.parse(response.body)["REQUEST_METHOD"].should == "PUT"
end

it "can run a DELETE synchronously" do
response = Typhoeus::Request.delete("http://localhost:3000", :params => {:q => "hi"}, :headers => {:foo => "bar"})
response.code.should == 200
JSON.parse(response.body)["REQUEST_METHOD"].should == "DELETE"
end
end

it "takes url as the first argument" do
Typhoeus::Request.new("http://localhost:3000").url.should == "http://localhost:3000"
end
Expand Down Expand Up @@ -104,4 +130,5 @@
it "should take a retry option"
it "should count the number of times a request has failed"
end

end

0 comments on commit ceeadd2

Please sign in to comment.