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 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 delete(path, options={})
def delete(path, options={}) perform_request Net::HTTP::Delete, path, options end
def format(f)
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) raise UnsupportedFormat, "Must be one of: #{AllowedFormats.values.map { |v| v.to_s }.uniq.sort.join(', ')}" unless AllowedFormats.value?(f) default_options[:format] = f 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 headers(h={})
headers 'Accept' => 'text/html'
include HTTParty
class Foo
Allows setting a base uri 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 parser(customer_parser)
parser Proc.new {|data| ...}
include HTTParty
class Foo
Allows setting a custom parser for the response.
def parser(customer_parser) default_options[:parser] = customer_parser 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