Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Raises exception when http error occurs. Added http_proxy key for set…

…ting proxy server and port. [#3 state:resolved] [#7 state:resolved]
  • Loading branch information...
commit b0f1cc2a8d6ab02f2fc7485ad3c25b87319443e4 1 parent e900355
Franck ROUDET authored committed
View
6 History.txt
@@ -1,3 +1,9 @@
+== 0.1.3 2008-08-XX
+
+* 1 major enhancement:
+ * Added : http_proxy key for setting proxy server and port
+ * Added : raises exception when http error occurs
+
== 0.1.2 2008-08-09
* 1 major enhancement:
View
2  httparty.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = %q{httparty}
- s.version = "0.1.2"
+ s.version = "0.1.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["John Nunemaker"]
View
26 lib/httparty.rb
@@ -21,6 +21,18 @@ class UnsupportedFormat < StandardError; end
AllowedFormats = %w[xml json]
module ClassMethods
+ #
+ # Set an http proxy
+ #
+ # class Twitter
+ # include HTTParty
+ # http_proxy http://myProxy, 1080
+ # ....
+ def http_proxy(addr=nil, port = nil)
+ @http_proxyaddr = addr
+ @http_proxyport = port
+ end
+
def base_uri(base_uri=nil)
return @base_uri unless base_uri
# don't want this to ever end with /
@@ -80,7 +92,7 @@ def delete(path, options={})
private
def http(uri) #:nodoc:
if @http.blank?
- @http = Net::HTTP.new(uri.host, uri.port)
+ @http = Net::HTTP.new(uri.host, uri.port, @http_proxyaddr, @http_proxyport)
@http.use_ssl = (uri.port == 443)
# so we can avoid ssl warnings
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@@ -94,6 +106,7 @@ def http(uri) #:nodoc:
# body => hash of keys/values or a query string (foo=bar&baz=poo)
# headers => hash of headers to send request with
# basic_auth => :username and :password to use as basic http authentication (overrides @auth class instance variable)
+ # Raises exception Net::XXX (http error code) if an http error occured
def send_request(method, path, options={}) #:nodoc:
raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
@@ -116,7 +129,16 @@ def send_request(method, path, options={}) #:nodoc:
# note to self: self, do not put basic auth above headers because it removes basic auth
request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
response = http(uri).request(request)
- parse_response(response.body)
+
+ case response
+ when Net::HTTPSuccess then
+ parse_response(response.body)
+ else
+ response.instance_eval { class << self; attr_accessor :body_parsed; end }
+ begin; response.body_parsed = parse_response(response.body); rescue; end
+ response.error! # raises exception corresponding to http error Net::XXX
+ end
+
end
def parse_response(body) #:nodoc:
View
2  lib/httparty/version.rb
@@ -2,7 +2,7 @@ module HTTParty
module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
- TINY = 2
+ TINY = 3
STRING = [MAJOR, MINOR, TINY].join('.')
end
Please sign in to comment.
Something went wrong with that request. Please try again.