1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

+ block passing in Resource#[]

This commit is contained in:
Niko Dittmann 2010-07-03 13:30:58 +02:00 committed by Julien Kirch
parent a6e3f85684
commit 65e4e7ae3b
4 changed files with 38 additions and 3 deletions

View file

@ -3,6 +3,7 @@
- add response body in Exception#inspect
- add support for RestClient.options
- fix tests for 1.9.2 (patch provided by Niko Dittmann)
- block passing in Resource#[] (patch provided by Niko Dittmann)
# 1.6.0

View file

@ -130,8 +130,13 @@ module RestClient
# comments = first_post['comments']
# comments.post 'Hello', :content_type => 'text/plain'
#
def [](suburl)
self.class.new(concat_urls(url, suburl), options)
def [](suburl, &new_block)
case
when block_given? then self.class.new(concat_urls(url, suburl), options, &new_block)
when block then self.class.new(concat_urls(url, suburl), options, &block)
else
self.class.new(concat_urls(url, suburl), options)
end
end
def concat_urls(url, suburl) # :nodoc:

View file

@ -1,4 +1,8 @@
Encoding.default_internal = Encoding.default_external = "ASCII-8BIT" if RUBY_VERSION == '1.9.1' or RUBY_VERSION == '1.9.2'
def is_ruby_19?
RUBY_VERSION == '1.9.1' or RUBY_VERSION == '1.9.2'
end
Encoding.default_internal = Encoding.default_external = "ASCII-8BIT" if is_ruby_19?
require 'rubygems'
require 'spec'

View file

@ -72,6 +72,31 @@ describe RestClient::Resource do
parent['posts'].password.should == 'password'
end
it "passes a given block to subresources" do
block = Proc.new{|r| r}
parent = RestClient::Resource.new('http://example.com', &block)
parent['posts'].block.should == block
end
it "the block should be overrideable" do
block1 = Proc.new{|r| r}
block2 = Proc.new{|r| r}
parent = RestClient::Resource.new('http://example.com', &block1)
# parent['posts', &block2].block.should == block2 # ruby 1.9 syntax
parent.send(:[], 'posts', &block2).block.should == block2
end
it "the block should be overrideable in ruby 1.9 syntax" do
block = Proc.new{|r| r}
parent = RestClient::Resource.new('http://example.com', &block)
r19_syntax = %q{
parent['posts', &->(r){r}].block.should_not == block
}
is_ruby_19?
eval(r19_syntax)
end
end
it "prints its url with to_s" do
RestClient::Resource.new('x').to_s.should == 'x'
end