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 !
:body and :query now both take either a hash or a query string [#4 
state:resolved]
jnunemaker (author)
Wed Jul 30 21:22:59 -0700 2008
commit  9b423a22c9ebcfefa93db3e668ead5e92dc0bfea
tree    c7f147b132670c1d8fa2ce7c66bf0d9dbcdfa77b
parent  3a8ad1da073c5ae61a2687a250cb17bc04f9c7c9
...
27
28
29
 
30
31
...
27
28
29
30
31
32
0
@@ -27,4 +27,5 @@ end
0
 twitter = Twitter.new(config['email'], config['password'])
0
 pp twitter.timeline
0
 # pp twitter.timeline(:friends, :query => {:since_id => 868482746})
0
+# pp twitter.timeline(:friends, :query => 'since_id=868482746')
0
 # pp twitter.post('this is a test')
0
\ No newline at end of file
...
90
91
92
93
94
 
 
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
 
 
 
 
 
 
 
 
 
 
 
 
 
112
113
114
115
 
116
117
118
...
90
91
92
 
 
93
94
95
96
97
98
 
99
100
101
 
 
 
 
 
 
 
 
 
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
118
119
120
121
0
@@ -90,29 +90,32 @@ module HTTParty
0
       
0
       # FIXME: this method is doing way to much and needs to be split up
0
       # options can be any or all of:
0
-      #   query       => hash of keys/values to be converted to query string
0
-      #   body        => string for raw post data
0
+      #   query       => hash of keys/values or a query string (foo=bar&baz=poo)
0
+      #   body        => hash of keys/values or a query string (foo=bar&baz=poo)
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
       def send_request(method, path, options={})
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, ':query must be a hash' if options[:query] && !options[:query].is_a?(Hash)
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
         # we always want path that begins with /
0
-        path         = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
0
-        @format    ||= format_from_path(path)
0
-        uri          = URI.parse("#{base_uri}#{path}")
0
-        current_qs   = uri.query ? "#{uri.query}&" : ''
0
-        uri.query    = current_qs + default_params.merge(options[:query] || {}).to_query
0
-        klass        = Net::HTTP.const_get method.to_s.downcase.capitalize
0
-        request      = klass.new(uri.request_uri)
0
-        request.body = options[:body] unless options[:body].blank?
0
-        basic_auth   = options.delete(:basic_auth) || @auth
0
+        path           = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
0
+        @format      ||= format_from_path(path)
0
+        uri            = URI.parse("#{base_uri}#{path}")
0
+        existing_query = uri.query ? "#{uri.query}&" : ''
0
+        uri.query      = if options[:query].blank?
0
+          existing_query
0
+        else
0
+          existing_query + (options[:query].is_a?(Hash) ? default_params.merge(options[:query]).to_query : options[:query])
0
+        end
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
         request.initialize_http_header headers.merge(options[:headers] || {})
0
         # note to self: self, do not put basic auth above headers because it removes basic auth
0
         request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
0
-        response     = http(uri).request(request)
0
+        response       = http(uri).request(request)
0
         parse_response(response.body)
0
       end
0
       
...
130
131
132
133
134
135
136
137
138
139
140
141
...
130
131
132
 
 
 
 
 
 
133
134
135
0
@@ -130,12 +130,6 @@ describe HTTParty do
0
       end.should raise_error(ArgumentError)
0
     end
0
     
0
-    it 'should require that :query is a hash if present' do
0
-      lambda do
0
-        Foo.send(:send_request, 'get', '/foo', :query => 'string')
0
-      end.should raise_error(ArgumentError)
0
-    end
0
-    
0
     it 'should require that :headers is a hash if present' do
0
       lambda do
0
         Foo.send(:send_request, 'get', '/foo', :headers => 'string')

Comments