<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -59,7 +59,7 @@ module Voorhees
     
     def perform
       setup_request
-      parse_response(perform_actual_request)
+      build_response(perform_actual_request)
     end
         
     private
@@ -129,11 +129,8 @@ module Voorhees
         query_string_parts.size &gt; 0 ? query_string_parts.join('&amp;') : nil
       end
     
-      def parse_response(response)
-        Voorhees::Config[:response_class].new(JSON.parse(response.body), @caller_class, @hierarchy)
-      rescue JSON::ParserError
-        Voorhees.debug(&quot;Parsing JSON failed.\nFirst 500 chars of body:\n#{response.body[0...500]}&quot;)
-        raise Voorhees::ParseError
+      def build_response(response)
+        Voorhees::Config[:response_class].new(response.body, @caller_class, @hierarchy)
       end
     
       def validate</diff>
      <filename>lib/voorhees/request.rb</filename>
    </modified>
    <modified>
      <diff>@@ -25,7 +25,7 @@ module Voorhees
         (class &lt;&lt; self; self; end).instance_eval do
           define_method name do |*args|
             params = args[0]
-            json_request(klass) do |r|
+            json_request(:class =&gt; klass) do |r|
               r.parameters = params if params.is_a?(Hash)
               request_options.each do |option, value|
                 r.send(&quot;#{option}=&quot;, value)
@@ -35,10 +35,21 @@ module Voorhees
         end
       end
       
-      def json_request(klass=nil)
-        request = Voorhees::Request.new(klass || self)
+      def json_request(options={})
+        request = Voorhees::Request.new(options[:class] || self)
         yield request
-        request.perform.to_objects
+        response = request.perform
+        
+        case options[:returning]
+        when :raw
+          response.body
+        when :json
+          response.json
+        when :response
+          response
+        else
+          response.to_objects
+        end
       end
     end
     
@@ -48,8 +59,8 @@ module Voorhees
         @json_attributes ||= @raw_json.keys.collect{|x| x.underscore.to_sym}
       end
       
-      def json_request(klass=nil)
-        self.class.json_request(klass) do |r|
+      def json_request(options={})
+        self.class.json_request(options) do |r|
           yield r
         end
       end</diff>
      <filename>lib/voorhees/resource.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,25 +2,32 @@ module Voorhees
   
   class Response
     
-    attr_reader :json, :klass
+    attr_reader :body, :klass
     
-    def initialize(json, klass=nil, hierarchy=nil)
-      @json       = json
+    def initialize(body, klass=nil, hierarchy=nil)
+      @body       = body
       @hierarchy  = hierarchy
       @klass      = klass      
     end
     
+    def json
+      @json ||= JSON.parse(@body)
+    rescue JSON::ParserError
+      Voorhees.debug(&quot;Parsing JSON failed.\nFirst 500 chars of body:\n#{response.body[0...500]}&quot;)
+      raise Voorhees::ParseError
+    end
+    
     def to_objects
       return unless @klass
       
       raise Voorhees::NotResourceError.new unless @klass.respond_to?(:new_from_json)
       
-      if @json.is_a?(Array)
-        @json.collect do |item|
+      if json.is_a?(Array)
+        json.collect do |item|
           @klass.new_from_json(item, @hierarchy)
         end
       else
-        @klass.new_from_json(@json, @hierarchy)
+        @klass.new_from_json(json, @hierarchy)
       end
     end
     </diff>
      <filename>lib/voorhees/response.rb</filename>
    </modified>
    <modified>
      <diff>@@ -157,7 +157,7 @@ describe Voorhees::Request do
       body = '{&quot;something&quot;:&quot;result&quot;}'
       @http_response = Net::HTTPResponse::CODE_TO_OBJ[&quot;200&quot;].new(&quot;1.1&quot;, 200, body)
       @http_response.stub!(:body).and_return(body)
-      @json_response = JSON.parse(body)
+      @json_response = body
       
       @mock_http  = MockNetHttp.new
       @connection = @mock_http.connection
@@ -275,7 +275,7 @@ describe Voorhees::Request do
       @request.perform.should be_a(Voorhees::Response)
     end    
     
-    it &quot;should pass the parsed JSON, caller class and hierarcy to the response&quot; do
+    it &quot;should pass the json body, caller class and hierarcy to the response&quot; do
       @connection.stub!(:request).and_return(@http_response)
             
       Voorhees::Response.should_receive(:new).with(@json_response, @caller_class, @hierarchy)      
@@ -317,7 +317,7 @@ describe Voorhees::Request do
           it &quot;should return the response from the service&quot; do
             @connection.should_receive(:request).with(@mock_get).exactly(1).times.ordered.and_raise(Timeout::Error.new(nil))                
             @connection.should_receive(:request).with(@mock_get).exactly(1).times.ordered.and_return(@http_response)
-            @request.perform.json.should == @json_response
+            @request.perform.body.should == @json_response
           end
 
         end
@@ -407,7 +407,7 @@ describe Voorhees::Request do
           it &quot;should return the response from the service&quot; do
             @connection.should_receive(:request).with(@mock_get).exactly(1).times.ordered.and_raise(Errno::ECONNREFUSED)                
             @connection.should_receive(:request).with(@mock_get).exactly(1).times.ordered.and_return(@http_response)
-            @request.perform.json.should == @json_response
+            @request.perform.body.should == @json_response
           end
         end
 </diff>
      <filename>spec/request_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -36,13 +36,19 @@ describe User  do
     describe &quot;json_request&quot; do
       
       before :each do 
+        @json_string= &quot;{}&quot;
+        @json_hash  = {}
+        
         @request  = mock(:request,  :null_object =&gt; true)
         @response = mock(:response, :null_object =&gt; true)
         @objects  = [mock(:object)]
                 
         Voorhees::Request.stub!(:new).and_return(@request)
         @request.stub!(:perform).and_return(@response)
+        
         @response.stub!(:to_objects).and_return(@objects)
+        @response.stub!(:json).and_return(@json_hash)        
+        @response.stub!(:body).and_return(@json_string)            
       end
       
       def perform_request
@@ -57,7 +63,7 @@ describe User  do
       
       it &quot;should call Request.new with the specified class if a class is passed&quot; do
         Voorhees::Request.should_receive(:new).with(Message).and_return(@request)
-        User.json_request(Message) do |request|
+        User.json_request(:class =&gt; Message) do |request|
           # ...
         end
       end
@@ -80,9 +86,26 @@ describe User  do
         perform_request
       end
       
-      it &quot;should return the result of Response#to_objects&quot; do
+      it &quot;should return the result of Response#to_objects if :returning is not set&quot; do
         perform_request.should == @objects
       end
+      
+      it &quot;should return the JSON string if :returning is set to :raw&quot; do
+        User.json_request(:returning =&gt; :raw){}.should == @json_string
+      end
+      
+      it &quot;should return the JSON hash if :returning is set to :json&quot; do
+        User.json_request(:returning =&gt; :json){}.should == @json_hash
+      end
+      
+      it &quot;should return the objects string if :returning is set to :objects&quot; do
+        User.json_request(:returning =&gt; :objects){}.should == @objects
+      end
+      
+      it &quot;should return the response if :returning is set to :response&quot; do
+        User.json_request(:returning =&gt; :response){}.should == @response
+      end
+              
     end
     
     describe &quot;json_service&quot; do</diff>
      <filename>spec/resource_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -89,5 +89,5 @@ def build_response(fixture)
   @hierarchy = {
     :address =&gt; Address
   }
-  @response = Voorhees::Response.new(JSON.parse(body), @klass, @hierarchy)
+  @response = Voorhees::Response.new(body, @klass, @hierarchy)
 end</diff>
      <filename>spec/response_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>72be77b77c33fdd0551bc6e359c703d8113f604c</id>
    </parent>
  </parents>
  <author>
    <name>Richard Livsey</name>
    <email>richard@livsey.org</email>
  </author>
  <url>http://github.com/rlivsey/voorhees/commit/c0e75ca7091dbbc6f9c8b27eb90869e6fbda9031</url>
  <id>c0e75ca7091dbbc6f9c8b27eb90869e6fbda9031</id>
  <committed-date>2009-06-30T15:32:37-07:00</committed-date>
  <authored-date>2009-06-30T15:32:37-07:00</authored-date>
  <message>Allow returning different content types from json_request:

* moved JSON parsing to Voorhees::Response
* send raw response body to Voorhees::Response.new instead of parsed JSON
* return either string, parsed JSON, the response or objects from json_request based on :returning parameter
* change json_request to accept class type as :class parameter instead of 1st argument</message>
  <tree>cc00e0b0d599640d2e663f09a0ccef3cefdd6017</tree>
  <committer>
    <name>Richard Livsey</name>
    <email>richard@livsey.org</email>
  </committer>
</commit>
