1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00
httparty/lib/httparty.rb

170 lines
6.1 KiB
Ruby
Raw Normal View History

2008-07-27 11:52:18 -04:00
require 'net/http'
require 'net/https'
require 'uri'
require 'ostruct'
2008-07-27 11:52:18 -04:00
require 'rubygems'
require 'active_support'
directory = File.dirname(__FILE__)
$:.unshift(directory) unless $:.include?(directory) || $:.include?(File.expand_path(directory))
module HTTParty
class UnsupportedFormat < StandardError; end
class RedirectionTooDeep < StandardError; end
2008-07-27 11:52:18 -04:00
def self.included(base)
base.extend ClassMethods
end
AllowedFormats = {:xml => 'text/xml', :json => 'application/json'}
SupportedHTTPMethods = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete]
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
2008-07-27 11:52:18 -04:00
def base_uri(base_uri=nil)
return @base_uri unless base_uri
@base_uri = normalize_base_uri(base_uri)
end
# Warning: This is not thread safe most likely and
# only works if you use one set of credentials. I
# leave it because it is convenient on some occasions.
def basic_auth(u, p)
@auth = {:username => u, :password => p}
2008-07-27 11:52:18 -04:00
end
# Updates the default query string parameters
# that should be appended to each request.
def default_params(h={})
raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
@default_params ||= {}
return @default_params if h.blank?
@default_params.merge!(h)
end
def headers(h={})
raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
@headers ||= {}
return @headers if h.blank?
@headers.merge!(h)
end
def format(f)
raise UnsupportedFormat, "Must be one of: #{AllowedFormats.keys.join(', ')}" unless AllowedFormats.key?(f)
@format = f
end
# TODO: spec out this
def get(path, options={})
send_request Net::HTTP::Get, path, options
end
# TODO: spec out this
def post(path, options={})
send_request Net::HTTP::Post, path, options
end
# TODO: spec out this
def put(path, options={})
send_request Net::HTTP::Put, path, options
end
# TODO: spec out this
def delete(path, options={})
send_request Net::HTTP::Delete, path, options
end
2008-07-27 11:52:18 -04:00
private
2008-07-31 00:30:52 -04:00
def http(uri) #:nodoc:
http = Net::HTTP.new(uri.host, uri.port, @http_proxyaddr, @http_proxyport)
http.use_ssl = (uri.port == 443)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http
end
# FIXME: this method is doing way to much and needs to be split up
# options can be any or all of:
# query => hash of keys/values or a query string (foo=bar&baz=poo)
# 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(klass, path, options={}) #:nodoc:
options = {:limit => 5}.merge(options)
options[:limit] = 0 if options.delete(:no_follow)
raise HTTParty::RedirectionTooDeep, 'HTTP redirects too deep' if options[:limit].to_i <= 0
raise ArgumentError, 'only get, post, put and delete methods are supported' unless SupportedHTTPMethods.include?(klass)
2008-07-28 13:32:35 -04:00
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
path = URI.parse(path)
uri = path.relative? ? URI.parse("#{base_uri}#{path}") : path
existing_query = uri.query ? "#{uri.query}&" : ''
uri.query = if options[:query].blank?
existing_query + default_params.to_query
else
existing_query + (options[:query].is_a?(Hash) ? default_params.merge(options[:query]).to_query : options[:query])
end
request = klass.new(uri.request_uri)
request.body = options[:body].is_a?(Hash) ? options[:body].to_query : options[:body] unless options[:body].blank?
basic_auth = options.delete(:basic_auth) || @auth
request.initialize_http_header headers.merge(options[:headers] || {})
request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
response = http(uri).request(request)
@format ||= format_from_mimetype(response['content-type'])
case response
when Net::HTTPSuccess
parse_response(response.body)
when Net::HTTPRedirection
options[:limit] -= 1
send_request(klass, response['location'], options)
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
2008-07-31 00:30:52 -04:00
def parse_response(body) #:nodoc:
return nil if body.nil? or body.empty?
case @format
when :xml
Hash.from_xml(body)
when :json
ActiveSupport::JSON.decode(body)
else
body
end
end
2008-07-27 11:52:18 -04:00
# Makes it so uri is sure to parse stuff like google.com with the http
2008-11-08 11:18:25 -05:00
def normalize_base_uri(url) #:nodoc:
use_ssl = (url =~ /^https/) || url.include?(':443')
url.chop! if url.ends_with?('/')
url.gsub!(/^https?:\/\//i, '')
"http#{'s' if use_ssl}://#{url}"
2008-07-27 11:52:18 -04:00
end
# Uses the HTTP Content-Type header to determine the format of the response
# It compares the MIME type returned to the types stored in the AllowedFormats hash
def format_from_mimetype(mimetype) #:nodoc:
AllowedFormats.each { |k, v| return k if mimetype.include?(v) }
end
2008-07-27 11:52:18 -04:00
end
end