public
Rubygem
Description: Makes http fun! Also, makes consuming restful web services dead easy.
Homepage:
Clone URL: git://github.com/jnunemaker/httparty.git
Click here to lend your support to: httparty and make a donation at www.pledgie.com !
Replacing string parameters with Net::HTTP classes

- send_request now uses the Net::HTTP classes directly
  rather than using const_get to grab them from 'get', etc.
ReinH (author)
Sat Nov 08 09:05:59 -0800 2008
commit  a2fd09256fb5b4558838c32b19f158024d065fb0
tree    e2cc34a50dab1898d2f5d4d236d5badee7d01951
parent  ae864580edb85b4e47aebf14afeff81855f202c8
...
17
18
19
 
20
21
22
...
66
67
68
69
 
70
71
72
73
74
 
75
76
77
78
79
 
80
81
82
83
84
 
85
86
87
...
99
100
101
102
 
103
104
105
106
107
 
108
109
110
...
117
118
119
120
121
122
123
...
131
132
133
134
 
135
136
137
...
166
167
168
169
170
 
...
17
18
19
20
21
22
23
...
67
68
69
 
70
71
72
73
74
 
75
76
77
78
79
 
80
81
82
83
84
 
85
86
87
88
...
100
101
102
 
103
104
105
106
107
 
108
109
110
111
...
118
119
120
 
121
122
123
...
131
132
133
 
134
135
136
137
...
166
167
168
 
169
170
0
@@ -17,6 +17,7 @@ module HTTParty
0
   end
0
   
0
   AllowedFormats = {:xml => 'text/xml', :json => 'application/json'}
0
+  SupportedHTTPMethods = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete]
0
   
0
   module ClassMethods    
0
     #
0
@@ -66,22 +67,22 @@ module HTTParty
0
     
0
     # TODO: spec out this
0
     def get(path, options={})
0
-      send_request 'get', path, options
0
+      send_request Net::HTTP::Get, path, options
0
     end
0
 
0
     # TODO: spec out this    
0
     def post(path, options={})
0
-      send_request 'post', path, options
0
+      send_request Net::HTTP::Post, path, options
0
     end
0
 
0
     # TODO: spec out this    
0
     def put(path, options={})
0
-      send_request 'put', path, options
0
+      send_request Net::HTTP::Put, path, options
0
     end
0
 
0
     # TODO: spec out this    
0
     def delete(path, options={})
0
-      send_request 'delete', path, options
0
+      send_request Net::HTTP::Delete, path, options
0
     end
0
     
0
     private
0
@@ -99,12 +100,12 @@ module HTTParty
0
       #   headers     => hash of headers to send request with
0
       #   basic_auth  => :username and :password to use as basic http authentication (overrides @auth class instance variable)
0
       # Raises exception Net::XXX (http error code) if an http error occured
0
-      def send_request(method, path, options={}) #:nodoc:
0
+      def send_request(klass, path, options={}) #:nodoc:
0
         options = {:limit => 5}.merge(options)
0
         options[:limit] = 0 if options.delete(:no_follow)
0
         
0
         raise HTTParty::RedirectionTooDeep, 'HTTP redirects too deep' if options[:limit].to_i <= 0
0
-        raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
0
+        raise ArgumentError, 'only get, post, put and delete methods are supported' unless SupportedHTTPMethods.include?(klass)
0
         raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
0
         raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
0
         
0
@@ -117,7 +118,6 @@ module HTTParty
0
           existing_query + (options[:query].is_a?(Hash) ? default_params.merge(options[:query]).to_query : options[:query])
0
         end
0
         
0
-        klass          = Net::HTTP.const_get method.to_s.downcase.capitalize
0
         request        = klass.new(uri.request_uri)
0
         request.body   = options[:body].is_a?(Hash) ? options[:body].to_query : options[:body] unless options[:body].blank?
0
         basic_auth     = options.delete(:basic_auth) || @auth
0
@@ -131,7 +131,7 @@ module HTTParty
0
           parse_response(response.body)
0
         when Net::HTTPRedirection
0
           options[:limit] -= 1
0
-          send_request(method, response['location'], options)
0
+          send_request(klass, response['location'], options)
0
         else
0
           response.instance_eval { class << self; attr_accessor :body_parsed; end }
0
           begin; response.body_parsed = parse_response(response.body); rescue; end
0
@@ -166,4 +166,4 @@ module HTTParty
0
         AllowedFormats.each { |k, v| return k if mimetype.include?(v) }
0
       end
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
139
140
141
142
 
143
144
145
 
146
147
148
...
186
187
188
189
 
190
191
192
...
218
219
220
221
222
 
...
139
140
141
 
142
143
144
 
145
146
147
148
...
186
187
188
 
189
190
191
192
...
218
219
220
 
221
222
0
@@ -139,10 +139,10 @@ describe HTTParty do
0
 
0
       Foo.headers.clear # clear out bogus settings from other specs
0
       Foo.format :xml
0
-      Foo.send(:send_request, 'get', '/bar').should be_nil
0
+      Foo.get('/bar').should be_nil
0
 
0
       response.stub!(:body).and_return("")
0
-      Foo.send(:send_request, 'get', 'bar').should be_nil
0
+      Foo.get('bar').should be_nil
0
     end
0
 
0
     describe "that respond with redirects" do
0
@@ -186,7 +186,7 @@ describe HTTParty do
0
         http.stub!(:request).and_return(redirect)
0
 
0
         lambda do
0
-          Foo.send(:send_request, 'get', '/foo')
0
+          Foo.get('/foo')
0
         end.should raise_error(HTTParty::RedirectionTooDeep)
0
       end
0
 
0
@@ -218,4 +218,4 @@ describe HTTParty do
0
       end
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+end

Comments