module HTTParty::ClassMethods
def base_uri(uri=nil)
base_uri 'twitter.com'
include HTTParty
class Foo
Will normalize uri to include http, etc.
Allows setting a base uri to be used for each request.
def base_uri(uri=nil) return default_options[:base_uri] unless uri default_options[:base_uri] = HTTParty.normalize_base_uri(uri) end
def basic_auth(u, p)
basic_auth 'username', 'password'
include HTTParty
class Foo
Allows setting basic authentication username and password.
def basic_auth(u, p) default_options[:basic_auth] = {:username => u, :password => p} end
def cookies(h={})
def cookies(h={}) raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash) default_cookies.add_cookies(h) end
def debug_output(stream = $stderr)
debug_output $stderr
include HTTParty
class Foo
The output stream is passed on to Net::HTTP#set_debug_output.
Set an output stream for debugging, defaults to $stderr.
def debug_output(stream = $stderr) default_options[:debug_output] = stream end
def default_options #:nodoc:
def default_options #:nodoc: @default_options end
def default_params(h={})
default_params :api_key => 'secret', :another => 'foo'
include HTTParty
class Foo
Great for api keys and such.
Allows setting default parameters to be appended to each request.
def default_params(h={}) raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash) default_options[:default_params] ||= {} default_options[:default_params].merge!(h) end
def default_timeout(t)
default_timeout 10
include HTTParty
class Foo
Timeout is specified in seconds.
Allows setting a default timeout for all HTTP calls
def default_timeout(t) raise ArgumentError, 'Timeout must be an integer' unless t && t.is_a?(Integer) default_options[:timeout] = t end
def delete(path, options={})
def delete(path, options={}) perform_request Net::HTTP::Delete, path, options end
def digest_auth(u, p)
digest_auth 'username', 'password'
include HTTParty
class Foo
Allows setting digest authentication username and password.
def digest_auth(u, p) default_options[:digest_auth] = {:username => u, :password => p} end
def format(f = nil)
format :json
include HTTParty
class Foo
Must be one of the allowed formats ie: json, xml
Allows setting the format with which to parse.
def format(f = nil) if f.nil? default_options[:format] else parser(Parser) if parser.nil? default_options[:format] = f validate_format end end
def get(path, options={})
# ie: http://foo.com/resource.json?limit=10
# Simple get with full url and query parameters
Foo.get('http://foo.com/resource.json')
# Simple get with full url
end
include HTTParty
class Foo
Allows making a get request to a url.
def get(path, options={}) perform_request Net::HTTP::Get, path, options end
def head(path, options={})
def head(path, options={}) perform_request Net::HTTP::Head, path, options end
def headers(h={})
headers 'Accept' => 'text/html'
include HTTParty
class Foo
Allows setting HTTP headers to be used for each request.
def headers(h={}) raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash) default_options[:headers] ||= {} default_options[:headers].merge!(h) end
def http_proxy(addr=nil, port = nil)
http_proxy 'http://foo.com', 80
include HTTParty
class Foo
Allows setting http proxy information to be used
def http_proxy(addr=nil, port = nil) default_options[:http_proxyaddr] = addr default_options[:http_proxyport] = port end
def maintain_method_across_redirects(value = true)
def maintain_method_across_redirects(value = true) default_options[:maintain_method_across_redirects] = value end
def no_follow(value = false)
- See: HTTParty::ResponseError#response -
def no_follow(value = false) default_options[:no_follow] = value end
def options(path, options={})
def options(path, options={}) perform_request Net::HTTP::Options, path, options end
def parser(custom_parser = nil)
parser Proc.new {|data| ...}
include HTTParty
class Foo
Allows setting a custom parser for the response.
def parser(custom_parser = nil) if custom_parser.nil? default_options[:parser] else default_options[:parser] = custom_parser validate_format end end
def pem(pem_contents)
pem File.read('/home/user/my.pem')
include HTTParty
class Foo
Allows setting a PEM file to be used
def pem(pem_contents) default_options[:pem] = pem_contents end
def perform_request(http_method, path, options) #:nodoc:
def perform_request(http_method, path, options) #:nodoc: options = default_options.dup.merge(options) process_cookies(options) Request.new(http_method, path, options).perform end
def post(path, options={})
# which gets set as form data on the request.
# Simple post with full url using :query option,
Foo.post('http://foo.com/resources', :body => {:bar => 'baz'})
# Simple post with full url and setting the body
end
include HTTParty
class Foo
Allows making a post request to a url.
def post(path, options={}) perform_request Net::HTTP::Post, path, options end
def process_cookies(options) #:nodoc:
def process_cookies(options) #:nodoc: return unless options[:cookies] || default_cookies.any? options[:headers] ||= headers.dup options[:headers]["cookie"] = cookies.merge(options.delete(:cookies) || {}).to_cookie_string end
def put(path, options={})
def put(path, options={}) perform_request Net::HTTP::Put, path, options end
def validate_format
def validate_format if format && parser.respond_to?(:supports_format?) && !parser.supports_format?(format) raise UnsupportedFormat, "'#{format.inspect}' Must be one of: #{parser.supported_formats.map{|f| f.to_s}.sort.join(', ')}" end end