Skip to content

Commit

Permalink
delegator fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 19, 2010
1 parent 6bac841 commit b4181b2
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 57 deletions.
1 change: 0 additions & 1 deletion lib/restfulie/client/entry_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def self.at(uri)
recipe
follow_link
request_marshaller
headers_dsl
verb_request
}.at(uri)
end
Expand Down
45 changes: 0 additions & 45 deletions lib/restfulie/client/http/headers_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,6 @@ class HeadersDsl < MasterDelegator
def initialize(requester)
@requester = requester
end

#Set host
def at(url)
host = url
self
end

#Set Content-Type and Accept headers
def as(content_type)
headers['Content-Type'] = content_type
accepts(content_type)
end

#Set Accept headers
def accepts(content_type)
headers['Accept'] = content_type
self
end

# Merge internal header
#
# * <tt>headers (e.g. {'Cache-control' => 'no-cache'})</tt>
#
def with(headers)
headers.merge!(headers)
self
end

# Path (e.g. http://restfulie.com/posts => /posts)
def path
puts "GOING TO START ATTTTTTTTT #{url}"
puts
puts
puts
puts
puts
puts
host.path
end

protected

def headers=(h)
@headers = h
end
end
end
end
Expand Down
37 changes: 36 additions & 1 deletion lib/restfulie/client/http/request_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,41 @@ module HTTP #:nodoc:
#
class RequestAdapter

attr_reader :host
attr_accessor :cookies
attr_writer :default_headers

#Set host
def at(url)
self.host= url
self
end

#Set Content-Type and Accept headers
def as(content_type)
headers['Content-Type'] = content_type
accepts(content_type)
end

#Set Accept headers
def accepts(content_type)
headers['Accept'] = content_type
self
end

# Merge internal header
#
# * <tt>headers (e.g. {'Cache-control' => 'no-cache'})</tt>
#
def with(headers)
headers.merge!(headers)
self
end

# Path (e.g. http://restfulie.com/posts => /posts)
def path
host.path
end

def host
@host
end
Expand Down Expand Up @@ -115,6 +146,10 @@ def get_connection_provider

protected

def headers=(h)
@headers = h
end

def request_to_s(method, path, *args)
result = ["#{method.to_s.upcase} #{path}"]

Expand Down
6 changes: 3 additions & 3 deletions lib/restfulie/client/http/request_marshaller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.content_type_for(media_type)

def accepts(media_types)
@default_representation = @@representations[media_types]
super
delegate(:accepts, media_types)
end

def raw
Expand Down Expand Up @@ -55,7 +55,7 @@ def request!(method, path, *args)
args = add_representation_headers(method, path, unmarshaller, *args)
end

key, req, response = super(method, path, *args)
key, req, response = delegate(:request, method, path, *args)
Restfulie::Client.cache_provider.put(key, req, response)

parse_response(response)
Expand All @@ -74,7 +74,7 @@ def parse_response(response)
request.get!
elsif @raw
response
elsif !response.body.empty?
elsif (!response.body.nil?) && !response.body.empty?
representation = RequestMarshaller.content_type_for(response.headers['content-type']) || Restfulie::Common::Representation::Generic.new
representation.unmarshal(response.body).tap do |u|
u.extend(ResponseHolder)
Expand Down
19 changes: 16 additions & 3 deletions lib/restfulie/client/master_delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ def respond_to?(sym)

def method_missing(sym, *args, &block)
if original_respond_to?(sym)
super(sym, *args, &block)
else @requester.respond_to?(sym)
result = super(sym, *args, &block)
elsif @requester.respond_to?(sym)
result = @requester.send(sym, *args, &block)
result==@requester ? self : result
else
# let it go
return super(sym, *args, &block)
end
delegate_parse result
end

protected

def delegate(what, *args, &block)
delegate_parse @requester.send(what, *args, &block)
end

def delegate_parse(result)
(result == @requester) ? self : result
end

end
3 changes: 3 additions & 0 deletions tests/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ group :test, :development do
gem "sinatra"
gem "rack-conneg"
gem "rspec-rails", ">= 2.0.0.beta.19"
gem "autotest"
gem "autotest-fsevent"
gem "ruby-debug"
end

# Bundle gems for the local environment. Make sure to
Expand Down
14 changes: 14 additions & 0 deletions tests/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ GEM
activesupport (3.0.0)
arel (0.4.0)
activesupport (>= 3.0.0.beta)
autotest (4.4.1)
autotest-fsevent (0.2.3)
sys-uname
builder (2.1.2)
columnize (0.3.1)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
fakeweb (1.2.8)
i18n (0.4.1)
json_pure (1.4.6)
linecache (0.43)
mail (2.2.5)
activesupport (>= 2.3.6)
mime-types
Expand Down Expand Up @@ -83,9 +88,15 @@ GEM
rspec-rails (2.0.0.beta.19)
rspec (= 2.0.0.beta.19)
webrat (>= 0.7.2.beta.1)
ruby-debug (0.10.3)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.3.0)
ruby-debug-base (0.10.3)
linecache (>= 0.3)
sinatra (1.0)
rack (>= 1.0)
sqlite3-ruby (1.3.1)
sys-uname (0.8.4)
thor (0.14.0)
treetop (1.4.8)
polyglot (>= 0.3.1)
Expand All @@ -99,10 +110,13 @@ PLATFORMS
ruby

DEPENDENCIES
autotest
autotest-fsevent
fakeweb
rack-conneg
rails (= 3.0.0)
restfulie (= 0.9.3)
rspec-rails (>= 2.0.0.beta.19)
ruby-debug
sinatra
sqlite3-ruby
8 changes: 5 additions & 3 deletions tests/spec/requests/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

require 'ruby-debug'

context Restfulie::Client::Base do

before(:all) do
Expand Down Expand Up @@ -55,9 +57,9 @@
context Restfulie::Client::EntryPoint do

before(:all) do
b = Restfulie::Client::EntryPoint.at('http://localhost:4567/songs').accepts('application/atom+xml')
puts b
@songs_from_ep = Restfulie::Client::EntryPoint.at('http://localhost:4567/songs').accepts('application/atom+xml').get!
r = Restfulie::Client::EntryPoint.at('http://localhost:4567/songs')
r = r.accepts('application/atom+xml')
@songs_from_ep = r.get!
end

it 'should load songs' do
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/requests/http/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

before(:all) do
@host = "http://localhost:4567"
@client = ::Restfulie::Client::HTTP::RequestExecutor.new(@host)
@client = Restfulie.at(@host)
end

it "should get and respond 200 code" do
Expand Down

0 comments on commit b4181b2

Please sign in to comment.