Browse files

Revert "Merge ParseXml and ParseJson into common parser"

This reverts commit 5f90b90.
  • Loading branch information...
1 parent bdb5095 commit b7eed9e26dde87c33a84ea70634b3c73c97c05bb @sferik sferik committed Nov 8, 2010
View
4 README.md
@@ -14,7 +14,7 @@ Let's decode the response body with [MultiJson](http://github.com/intridea/multi
conn = Faraday::Connection.new(:url => 'http://api.twitter.com/1') do |builder|
builder.adapter Faraday.default_adapter
- builder.use Faraday::Response::Parse
+ builder.use Faraday::Response::ParseJson
end
resp = conn.get do |req|
@@ -30,7 +30,7 @@ Want to ditch the brackets and use dot notation? [Mashify](http://github.com/int
conn = Faraday::Connection.new(:url => 'http://api.twitter.com/1') do |builder|
builder.adapter Faraday.default_adapter
- builder.use Faraday::Response::Parse
+ builder.use Faraday::Response::ParseJson
builder.use Faraday::Response::Mashify
end
View
49 lib/faraday/parse.rb
@@ -1,49 +0,0 @@
-module Faraday
- class Response::Parse < Response::Middleware
- begin
- require 'multi_json'
- require 'multi_xml'
- rescue LoadError, NameError => error
- self.load_error = error
- end
-
- def self.register_on_complete(env)
- env[:response].on_complete do |response|
- response[:body] = begin
- case response[:response_headers].values_at('content-type', 'Content-Type').first
- when /\/json/
- parse_json(response[:body])
- when /\/.*xml/
- parse_xml(response[:body])
- else
- ''
- end
- end
- end
- end
-
- def initialize(app)
- super
- @parser = nil
- end
-
- private
-
- def self.parse_json(response_body)
- case response_body
- when ''
- nil
- when 'true'
- true
- when 'false'
- false
- else
- ::MultiJson.decode(response_body)
- end
- end
-
- def self.parse_xml(response_body)
- ::MultiXml.parse(response_body)
- end
- end
-end
View
31 lib/faraday/parse_json.rb
@@ -0,0 +1,31 @@
+module Faraday
+ class Response::ParseJson < Response::Middleware
+ begin
+ require 'multi_json'
+ rescue LoadError, NameError => error
+ self.load_error = error
+ end
+
+ def self.register_on_complete(env)
+ env[:response].on_complete do |response|
+ response[:body] = begin
+ case response[:body]
+ when ''
+ nil
+ when 'true'
+ true
+ when 'false'
+ false
+ else
+ ::MultiJson.decode(response[:body])
+ end
+ end
+ end
+ end
+
+ def initialize(app)
+ super
+ @parser = nil
+ end
+ end
+end
View
22 lib/faraday/parse_xml.rb
@@ -0,0 +1,22 @@
+module Faraday
+ class Response::ParseXml < Response::Middleware
+ begin
+ require 'multi_xml'
+ rescue LoadError, NameError => error
+ self.load_error = error
+ end
+
+ def self.register_on_complete(env)
+ env[:response].on_complete do |response|
+ response[:body] = begin
+ ::MultiXml.parse(response[:body])
+ end
+ end
+ end
+
+ def initialize(app)
+ super
+ @parser = nil
+ end
+ end
+end
View
2 test/mashify_test.rb
@@ -6,7 +6,7 @@ class MashifyTest < Test::Unit::TestCase
@stubs = Faraday::Adapter::Test::Stubs.new
@conn = Faraday::Connection.new do |builder|
builder.adapter :test, @stubs
- builder.use Faraday::Response::Parse
+ builder.use Faraday::Response::ParseJson
builder.use Faraday::Response::Mashify
end
end
View
48 test/parse_test.rb → test/parse_json_test.rb
@@ -1,13 +1,12 @@
require 'helper'
-class ParseTest < Test::Unit::TestCase
+class ParseJsonTest < Test::Unit::TestCase
context 'when used' do
setup do
@stubs = Faraday::Adapter::Test::Stubs.new
@conn = Faraday::Connection.new do |builder|
builder.adapter :test, @stubs
- require 'rexml/document'
- builder.use Faraday::Response::Parse
+ builder.use Faraday::Response::ParseJson
end
end
@@ -71,48 +70,5 @@ class ParseTest < Test::Unit::TestCase
assert_equal false, response.body
end
end
-
- context "when there is a XML body" do
- setup do
- @stubs.get('/me') {[200, {'content-type' => 'application/xml; charset=utf-8'}, '<user><name>Erik Michaels-Ober</name><username>sferik</username></user>']}
- end
-
- should 'parse the body as XML' do
- me = @conn.get("/me").body['user']
- assert me.is_a?(Hash)
- assert_equal 'Erik Michaels-Ober', me['name']
- assert_equal 'sferik', me['username']
- end
- end
-
- context "when there is a ATOM body" do
- setup do
- @stubs.get('/me') {[200, {'content-type' => 'application/atom+xml; charset=utf-8'}, '<user><name>Erik Michaels-Ober</name><username>sferik</username></user>']}
- end
-
- should 'parse the body as XML' do
- me = @conn.get("/me").body['user']
- assert me.is_a?(Hash)
- assert_equal 'Erik Michaels-Ober', me['name']
- assert_equal 'sferik', me['username']
- end
- end
-
- context "when the XML body is empty" do
- setup do
- @stubs.get('/me') {[200, {'content-type' => 'application/xml; charset=utf-8'}, ""]}
- end
-
- should 'still have the status code' do
- response = @conn.get("/me")
- assert_equal 200, response.status
- end
-
- should 'set body to nil' do
- response = @conn.get("/me")
- assert_equal Hash.new, response.body
- end
- end
-
end
end
View
56 test/parse_xml_test.rb
@@ -0,0 +1,56 @@
+require 'helper'
+
+class ParseXmlTest < Test::Unit::TestCase
+ context 'when used' do
+ setup do
+ @stubs = Faraday::Adapter::Test::Stubs.new
+ @conn = Faraday::Connection.new do |builder|
+ builder.adapter :test, @stubs
+ builder.use Faraday::Response::ParseXml
+ end
+ end
+
+ context "when there is a XML body" do
+ setup do
+ @stubs.get('/me') {[200, {'content-type' => 'application/xml; charset=utf-8'}, '<user><name>Erik Michaels-Ober</name><username>sferik</username></user>']}
+ end
+
+ should 'parse the body as XML' do
+ me = @conn.get("/me").body['user']
+ assert me.is_a?(Hash)
+ assert_equal 'Erik Michaels-Ober', me['name']
+ assert_equal 'sferik', me['username']
+ end
+ end
+
+ context "when there is a ATOM body" do
+ setup do
+ @stubs.get('/me') {[200, {'content-type' => 'application/atom+xml; charset=utf-8'}, '<user><name>Erik Michaels-Ober</name><username>sferik</username></user>']}
+ end
+
+ should 'parse the body as XML' do
+ me = @conn.get("/me").body['user']
+ assert me.is_a?(Hash)
+ assert_equal 'Erik Michaels-Ober', me['name']
+ assert_equal 'sferik', me['username']
+ end
+ end
+
+ context "when the XML body is empty" do
+ setup do
+ @stubs.get('/me') {[200, {'content-type' => 'application/xml; charset=utf-8'}, ""]}
+ end
+
+ should 'still have the status code' do
+ response = @conn.get("/me")
+ assert_equal 200, response.status
+ end
+
+ should 'set body to nil' do
+ response = @conn.get("/me")
+ assert_equal Hash.new, response.body
+ end
+ end
+
+ end
+end

0 comments on commit b7eed9e

Please sign in to comment.