Skip to content

Commit

Permalink
Merge branch '30-get-parameters'
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Jun 23, 2010
2 parents f73d400 + c41de76 commit 0b98b2c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/restfulie/client/http/request_builder.rb
@@ -1,3 +1,5 @@
require 'uri'

module Restfulie
module Client
module HTTP #:nodoc:
Expand Down Expand Up @@ -48,8 +50,8 @@ def path
host.path
end

def get
request(:get, path, headers)
def get(params = {})
request(:get, add_querystring(path, params), headers)
end

def head
Expand All @@ -71,9 +73,9 @@ def put(payload)
def delete
request(:delete, path, headers)
end

def get!
request!(:get, path, headers)
def get!(params = {})
request!(:get, add_querystring(path, params), headers)
end

def head!
Expand All @@ -97,6 +99,11 @@ def delete!
end

protected

def add_querystring(path, params)
params = params.map { |param, value| "#{param}=#{value}"}.join("&")
params.blank? ? path : URI.escape("#{path}?#{params}")
end

def headers=(h)
@headers = h
Expand Down
2 changes: 1 addition & 1 deletion lib/restfulie/server/action_controller/base.rb
Expand Up @@ -25,7 +25,7 @@ def call(env)
end

def include_restfulie?
defined?(Restfulie::Server::ActionController::Base) && controller_class_name.constantize.include?(Restfulie::Server::ActionController::Base)
defined?(Restfulie::Server::ActionController::Base) && self.include?(Restfulie::Server::ActionController::Base)
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/units/client/fake_server.rb
@@ -1,6 +1,7 @@
require 'rubygems'
require 'sinatra'
require 'rack/conneg'
require 'active_support'

require File.join(File.dirname(__FILE__),'..','lib','data_helper')

Expand Down Expand Up @@ -41,6 +42,10 @@
Rack::Response.new('OK', params[:error].to_i).finish
end

get "/request_with_querystring" do
params.to_json
end

get '/test_redirection' do
r = Rack::Response.new('', 201)
r.header["Location"] = "http://localhost:4567/redirected"
Expand Down
12 changes: 12 additions & 0 deletions spec/units/client/http/builder_spec.rb
@@ -0,0 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

context Restfulie::Client::HTTP::RequestBuilder do
context "chaining requests" do
it "should accept parameters in get requests" do
@result = Restfulie::Client::EntryPoint.at('http://localhost:4567/request_with_querystring').get!(:foo => "bar", :bar => "foo")
params = JSON.parse(@result)
params["foo"].should == "bar"
params["bar"].should == "foo"
end
end
end

0 comments on commit 0b98b2c

Please sign in to comment.