Skip to content

Commit

Permalink
Raises exception when http error occurs. Added http_proxy key for set…
Browse files Browse the repository at this point in the history
…ting proxy server and port. [jnunemaker#3 state:resolved] [jnunemaker#7 state:resolved]
  • Loading branch information
Franck ROUDET authored and jnunemaker committed Aug 22, 2008
1 parent e900355 commit b0f1cc2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
6 changes: 6 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion httparty.gemspec
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
26 changes: 24 additions & 2 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 /
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/httparty/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module HTTParty
module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
TINY = 2
TINY = 3

STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down

0 comments on commit b0f1cc2

Please sign in to comment.