Skip to content
Browse files

interpolate get url fragments

  • Loading branch information...
1 parent 2a01da9 commit 3e9dc49251f08a3b349be831988302d88de85e71 @jamesgolick committed
Showing with 32 additions and 4 deletions.
  1. +20 −2 lib/trample/page.rb
  2. +12 −2 test/page_test.rb
View
22 lib/trample/page.rb
@@ -1,6 +1,6 @@
module Trample
class Page
- attr_reader :request_method, :url
+ attr_reader :request_method
def initialize(request_method, url, parameters = {})
@request_method = request_method
@@ -9,7 +9,7 @@ def initialize(request_method, url, parameters = {})
end
def parameters
- @parameters.is_a?(Proc) ? @parameters.call : @parameters
+ proc_params? ? @parameters.call : @parameters
end
def ==(other)
@@ -17,5 +17,23 @@ def ==(other)
other.request_method == request_method &&
other.url == url
end
+
+ def url
+ proc_params? ? interpolated_url : @url
+ end
+
+ protected
+ def proc_params?
+ @parameters.is_a?(Proc)
+ end
+
+ def interpolated_url
+ params = parameters # cache called proc
+ url = @url
+ url.scan(/\:\w+/).each do |m|
+ url.gsub!(m, params[m.gsub(/:/, '').to_sym].to_s)
+ end
+ url
+ end
end
end
View
14 test/page_test.rb
@@ -27,14 +27,24 @@ class PageTest < Test::Unit::TestCase
end
end
- context "Block-based request parameters" do
+ context "Block-based request parameters for POST requests" do
setup do
- @page = Trample::Page.new(:get, "http://google.com/", lambda { { :username => "joetheuser" } })
+ @page = Trample::Page.new(:post, "http://google.com/", lambda { { :username => "joetheuser" } })
end
should "be resolved when the page is asked for its parameters" do
assert_equal({:username => "joetheuser"}, @page.parameters)
end
end
+
+ context "Block based parameters for GET requests" do
+ setup do
+ @page = Trample::Page.new(:get, "http://mysite.com/somethings/:id", lambda { {:id => 5} })
+ end
+
+ should "interpolate those parameters with the url string" do
+ assert_equal "http://mysite.com/somethings/5", @page.url
+ end
+ end
end

0 comments on commit 3e9dc49

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