Skip to content

Commit

Permalink
including required hash extension
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 21, 2010
1 parent f4c9a07 commit ff5f89f
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 163 deletions.
255 changes: 124 additions & 131 deletions lib/restfulie/client/feature.rb
@@ -1,164 +1,157 @@
module Restfulie::Client
module Feature
autoload :EnhanceResponse, 'restfulie/client/feature/enhance_response'
end
end

module Restfulie::Client::Feature

module Base

class EnhanceResponse
def execute(flow, request, response)
resp = flow.continue(flow, request, response)
unless resp.kind_of? ::Restfulie::Client::HTTP::ResponseHolder
resp.extend(::Restfulie::Client::HTTP::ResponseHolder)
resp.response = response
end
resp
end
attr_reader :default_headers

def cookies
@cookies
end

module Base

attr_reader :default_headers

def cookies
@cookies
end

def verb
@method
end

def get
@method = :get
request_flow
end

#Set host
def at(url)
if self.host.nil?
self.host= url
else
self.host= self.host + url
end
self
end
def verb
@method
end

def get
@method = :get
request_flow
end

#Set host
def at(url)
if self.host.nil?
self.host= url
else
self.host= self.host + url
end
self
end

#Set Content-Type and Accept headers
def as(content_type)
headers['Content-Type'] = content_type
accepts(content_type)
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
#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
# 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
# Path (e.g. http://restfulie.com/posts => /posts)
def path
host.path
end

def host
@host
end
def host
@host
end

def host=(host)
if host.is_a?(::URI)
@host = host
else
@host = ::URI.parse(host)
end
def host=(host)
if host.is_a?(::URI)
@host = host
else
@host = ::URI.parse(host)
end
end

def default_headers
@default_headers ||= {}
end
def default_headers
@default_headers ||= {}
end

def headers
@headers ||= {}
end
def headers
@headers ||= {}
end

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

arguments = args.dup
headers = arguments.extract_options!
arguments = args.dup
headers = arguments.extract_options!

if [:post, :put].include?(method)
body = arguments.shift
end
if [:post, :put].include?(method)
body = arguments.shift
end

result << headers.collect { |key, value| "#{key}: #{value}" }.join("\n")
result << headers.collect { |key, value| "#{key}: #{value}" }.join("\n")

(result + [body ? (body.inspect + "\n") : nil]).compact.join("\n") << "\n"
end
(result + [body ? (body.inspect + "\n") : nil]).compact.join("\n") << "\n"
end

protected
protected

def headers=(h)
@headers = h
end
def headers=(h)
@headers = h
end

end

class BaseRequest

def execute(flow, request, response)
request!(request.verb, request.host, request.path, request, flow)
end

class BaseRequest

def execute(flow, request, response)
request!(request.verb, request.host, request.path, request, flow)
# Executes a request against your server and return a response instance.
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
def request!(method, host, path, request, flow, *args)
headers = request.default_headers.merge(args.extract_options!)
unless host.user.blank? && host.password.blank?
headers["Authorization"] = "Basic " + ["#{host.user}:#{host.password}"].pack("m").delete("\r\n")
end
headers.delete :recipe
headers['cookie'] = request.cookies if request.cookies
args << headers

::Restfulie::Common::Logger.logger.info(request.http_to_s(method, path, *args)) if ::Restfulie::Common::Logger.logger
begin
http_request = get_connection_provider(host)

cached = Restfulie::Client.cache_provider.get([host, path], http_request, method)
return cached if cached

response = http_request.send(method, path, *args)
rescue Exception => e
response = e
end

# Executes a request against your server and return a response instance.
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
def request!(method, host, path, request, flow, *args)
headers = request.default_headers.merge(args.extract_options!)
unless host.user.blank? && host.password.blank?
headers["Authorization"] = "Basic " + ["#{host.user}:#{host.password}"].pack("m").delete("\r\n")
end
headers.delete :recipe
headers['cookie'] = request.cookies if request.cookies
args << headers

::Restfulie::Common::Logger.logger.info(request.http_to_s(method, path, *args)) if ::Restfulie::Common::Logger.logger
begin
http_request = get_connection_provider(host)

cached = Restfulie::Client.cache_provider.get([host, path], http_request, method)
return cached if cached

response = http_request.send(method, path, *args)
rescue Exception => e
response = e
end

flow.continue(request, response)

# Restfulie::Client::Response::EnhanceResponse.new(response_handler).parse(host, path, http_request, self, response, method)
flow.continue(request, response)

# Restfulie::Client::Response::EnhanceResponse.new(response_handler).parse(host, path, http_request, self, response, method)

end
end

# Executes a request against your server and return a response instance without {Error}
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
def request(method = nil, path = nil, *args)
@response_handler = Restfulie::Client::Response::IgnoreError.new(@response_handler)
request!(method, path, *args)
end
# Executes a request against your server and return a response instance without {Error}
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
def request(method = nil, path = nil, *args)
@response_handler = Restfulie::Client::Response::IgnoreError.new(@response_handler)
request!(method, path, *args)
end

def get_connection_provider(host)
@connection ||= ::Net::HTTP.new(host.host, host.port)
end

def get_connection_provider(host)
@connection ||= ::Net::HTTP.new(host.host, host.port)
end

end

end
12 changes: 12 additions & 0 deletions lib/restfulie/client/feature/enhance_response.rb
@@ -0,0 +1,12 @@
module Restfulie::Client::Feature
class EnhanceResponse
def execute(flow, request, response)
resp = flow.continue(flow, request, response)
unless resp.kind_of? ::Restfulie::Client::HTTP::ResponseHolder
resp.extend(::Restfulie::Client::HTTP::ResponseHolder)
resp.response = response
end
resp
end
end
end
6 changes: 6 additions & 0 deletions lib/restfulie/client/http/response_holder.rb
Expand Up @@ -3,6 +3,12 @@ module Client
module HTTP
module ResponseHolder
attr_accessor :response

def resource
type = response.respond_to?(:headers) ? response.headers['content-type'] : response['content-type']
representation = Restfulie::Client::HTTP::RequestMarshaller.content_type_for(type) || Restfulie::Common::Representation::Generic.new
representation.unmarshal(response.body)
end
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/restfulie/client/response.rb
@@ -1,12 +1,10 @@
module Restfulie
module Client
module Response#:nodoc:
autoload :EnhanceResponse, 'restfulie/client/response/enhance_response'
autoload :IgnoreError, 'restfulie/client/response/ignore_error'
autoload :CatchAndThrow, 'restfulie/client/response/catch_and_throw'
autoload :CacheHandler, 'restfulie/client/response/cache_handler'
autoload :CreatedRedirect, 'restfulie/client/response/created_redirect'
autoload :UnmarshallHandler, 'restfulie/client/response/unmarshall_handler'
end
end
end
Empty file.
29 changes: 0 additions & 29 deletions lib/restfulie/client/response/unmarshall_handler.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/restfulie/common/converter/atom/base.rb
@@ -1,3 +1,5 @@
require 'active_support/core_ext/hash/conversions'

module Restfulie
module Common
module Converter
Expand Down
2 changes: 2 additions & 0 deletions lib/restfulie/common/converter/xml/base.rb
@@ -1,3 +1,5 @@
require 'active_support/core_ext/hash/conversions'

module Restfulie
module Common
module Converter
Expand Down
Binary file added restfulie-0.9.3.gem
Binary file not shown.
2 changes: 1 addition & 1 deletion spec/integration/twitter_spec.rb
Expand Up @@ -11,7 +11,7 @@

it "should work with twitter" do
twitter = Restfulie.at(TWITTER_ENTRY_POINT).get
twitter.statuses[0].user.screen_name.should == "fionnaps"
twitter.resource.statuses[0].user.screen_name.should == "fionnaps"
end

after do
Expand Down

0 comments on commit ff5f89f

Please sign in to comment.