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 !
Raises exception when http error occurs. Added http_proxy key for setting proxy 
server and port. [#3 state:resolved] [#7 state:resolved]
francxk (author)
Mon Aug 18 12:48:54 -0700 2008
jnunemaker (committer)
Fri Aug 22 13:32:42 -0700 2008
commit  b0f1cc2a8d6ab02f2fc7485ad3c25b87319443e4
tree    3f2fb955dfb2d8fa9cb82c2fabeaa7d1fb66b127
parent  e9003555f044685832a84d013f77a79485445c17
...
 
 
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
8
9
0
@@ -1,3 +1,9 @@
0
+== 0.1.3 2008-08-XX
0
+
0
+* 1 major enhancement:
0
+  * Added : http_proxy key for setting proxy server and port
0
+  * Added : raises exception when http error occurs
0
+
0
 == 0.1.2 2008-08-09
0
 
0
 * 1 major enhancement:
...
1
2
3
 
4
5
6
...
1
2
 
3
4
5
6
0
@@ -1,6 +1,6 @@
0
 Gem::Specification.new do |s|
0
   s.name = %q{httparty}
0
-  s.version = "0.1.2"
0
+  s.version = "0.1.3"
0
 
0
   s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
0
   s.authors = ["John Nunemaker"]
...
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
24
25
26
...
80
81
82
83
 
84
85
86
...
94
95
96
 
97
98
99
...
116
117
118
119
 
 
 
 
 
 
 
 
 
 
120
121
122
...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
92
93
94
 
95
96
97
98
...
106
107
108
109
110
111
112
...
129
130
131
 
132
133
134
135
136
137
138
139
140
141
142
143
144
0
@@ -21,6 +21,18 @@ module HTTParty
0
   AllowedFormats = %w[xml json]
0
   
0
   module ClassMethods    
0
+    #
0
+    # Set an http proxy
0
+    #
0
+    #  class Twitter
0
+    #    include HTTParty
0
+    #    http_proxy http://myProxy, 1080
0
+    # ....
0
+    def http_proxy(addr=nil, port = nil)
0
+     @http_proxyaddr = addr
0
+     @http_proxyport = port
0
+    end
0
+
0
     def base_uri(base_uri=nil)
0
       return @base_uri unless base_uri
0
       # don't want this to ever end with /
0
@@ -80,7 +92,7 @@ module HTTParty
0
     private
0
       def http(uri) #:nodoc:
0
         if @http.blank?
0
-          @http = Net::HTTP.new(uri.host, uri.port)
0
+          @http = Net::HTTP.new(uri.host, uri.port, @http_proxyaddr, @http_proxyport)
0
           @http.use_ssl = (uri.port == 443)
0
           # so we can avoid ssl warnings
0
           @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
0
@@ -94,6 +106,7 @@ module HTTParty
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
+      # Raises exception Net::XXX (http error code) if an http error occured
0
       def send_request(method, path, options={}) #:nodoc:
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, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
0
@@ -116,7 +129,16 @@ module HTTParty
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
-        parse_response(response.body)
0
+
0
+        case response
0
+        when Net::HTTPSuccess     then
0
+          parse_response(response.body)
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
+          response.error! # raises  exception corresponding to http error Net::XXX
0
+        end
0
+
0
       end
0
       
0
       def parse_response(body) #:nodoc:
...
2
3
4
5
 
6
7
8
...
2
3
4
 
5
6
7
8
0
@@ -2,7 +2,7 @@ module HTTParty
0
   module VERSION #:nodoc:
0
     MAJOR = 0
0
     MINOR = 1
0
-    TINY  = 2
0
+    TINY  = 3
0
 
0
     STRING = [MAJOR, MINOR, TINY].join('.')
0
   end

Comments