Skip to content

Commit

Permalink
Add Request#base_uri
Browse files Browse the repository at this point in the history
This allows you to set a base URI from a defaults block to apply to every request.
  • Loading branch information
bernerdschaefer committed Jun 24, 2011
1 parent daa2aa3 commit 572a72c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.markdown
Expand Up @@ -126,6 +126,23 @@ If you have Typhoeus settings you want to happen for every request, you can set
# ...
end

If all of your requests share a common base URI, you can set that in your
defaults block:

class GoogleJson < MonsterMash::Base
defaults do
base_uri "http://google.com"
end

get(:search) do
uri "/search" # expands to http://google.com/search
end

post(:authenticate) do
uri "https://auth.google.com" # ignores the base_uri
end
end

As well, if you set `params` or `headers` in the `defaults` block, any `params` or `headers` added later will be `merge`d into the hash.

class GoogleJson < MonsterMash::Base
Expand Down
12 changes: 10 additions & 2 deletions lib/monster_mash/request.rb
Expand Up @@ -61,9 +61,17 @@ def valid?

def uri(value = nil)
if value
@uri = value
@uri = base_uri ? URI.join(base_uri, value).to_s : value
end
@uri

@uri || base_uri
end

def base_uri(value = nil)
if value
@base_uri = value
end
@base_uri
end

def handler(&block)
Expand Down
33 changes: 33 additions & 0 deletions spec/monster_mash/request_spec.rb
Expand Up @@ -113,6 +113,39 @@ class ApplyDefaultsB < ApplyDefaultsA
@request.uri "http://google.com"
@request.uri.should == "http://google.com"
end

context "when a base uri is set" do
before do
@request.base_uri "http://google.com"
end

it "joins the two uris" do
@request.uri "/test"
@request.uri.should eq "http://google.com/test"
end

it "can be overridden" do
@request.uri "http://test.local"
@request.uri.should eq "http://test.local"
end

context "but no uri is specified" do
it "defaults to the base uri" do
@request.uri.should eq "http://google.com"
end
end
end
end

describe "#base_uri" do
before(:each) do
@request = MonsterMash::Request.new(:get)
end

it "should set the base uri, and return it" do
@request.base_uri "http://google.com"
@request.base_uri.should eq "http://google.com"
end
end

describe "method_missing methods" do
Expand Down

0 comments on commit 572a72c

Please sign in to comment.