Permalink
Browse files

simplify basic response middleware

  • Loading branch information...
1 parent 84e7c8d commit 1ad9b6b23522e08150bac73123021ee57f379b68 @technoweenie technoweenie committed Feb 27, 2011
@@ -27,7 +27,7 @@ def call(env)
request = EventMachine::HttpRequest.new(URI::parse(env[:url].to_s))
options = {:head => env[:request_headers]}
options[:ssl] = env[:ssl] if env[:ssl]
-
+
if env[:body]
if env[:body].respond_to? :read
options[:body] = env[:body].read
@@ -1,5 +1,16 @@
module Faraday
class Response
+ # Used for simple response middleware that parse the incoming body into
+ # a ruby object. Expects a #parse instance method.
+ class Middleware < Faraday::Middleware
+ def call(env)
+ env[:response].on_complete do |finished_env|
+ finished_env[:body] = parse(finished_env[:body])
+ end
+ @app.call(env)
+ end
+ end
+
extend AutoloadHelper
autoload_all 'faraday/response',
@@ -1,5 +1,5 @@
module Faraday
- class Response::ActiveSupportJson < Faraday::Middleware
+ class Response::ActiveSupportJson < Response::Middleware
begin
if !defined?(ActiveSupport::JSON)
require 'active_support'
@@ -9,18 +9,6 @@ class Response::ActiveSupportJson < Faraday::Middleware
self.load_error = e
end
- def initialize(app)
- super
- @parser = nil
- end
-
- def call(env)
- env[:response].on_complete do |finished_env|
- finished_env[:body] = parse(finished_env[:body])
- end
- @app.call(env)
- end
-
def parse(body)
ActiveSupport::JSON.decode(body)
rescue Object
@@ -1,23 +1,11 @@
module Faraday
- class Response::Yajl < Faraday::Middleware
+ class Response::Yajl < Response::Middleware
begin
require 'yajl'
rescue LoadError, NameError => e
self.load_error = e
end
- def initialize(app)
- super
- @parser = nil
- end
-
- def call(env)
- env[:response].on_complete do |finished_env|
- finished_env[:body] = parse(finished_env[:body])
- end
- @app.call(env)
- end
-
def parse(body)
Yajl::Parser.parse(body)
rescue Object

0 comments on commit 1ad9b6b

Please sign in to comment.