Module: HTTParty::ClassMethods

Extended by:
AllowedFormatsDeprecation
Defined in:
lib/httparty.rb

Instance Method Summary collapse

Methods included from AllowedFormatsDeprecation

const_missing

Instance Method Details

#base_uri(uri = nil) ⇒ Object

Allows setting a base uri to be used for each request. Will normalize uri to include http, etc.

class Foo
  include HTTParty
  base_uri 'twitter.com'
end


57
58
59
60
# File 'lib/httparty.rb', line 57

def base_uri(uri=nil)
  return default_options[:base_uri] unless uri
  default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
end

#basic_auth(u, p) ⇒ Object

Allows setting basic authentication username and password.

class Foo
  include HTTParty
  basic_auth 'username', 'password'
end


68
69
70
# File 'lib/httparty.rb', line 68

def basic_auth(u, p)
  default_options[:basic_auth] = {:username => u, :password => p}
end

#cookies(h = {}) ⇒ Object

Raises:

  • (ArgumentError)


97
98
99
100
# File 'lib/httparty.rb', line 97

def cookies(h={})
  raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash)
  default_cookies.add_cookies(h)
end

#default_optionsObject

:nodoc:



192
193
194
# File 'lib/httparty.rb', line 192

def default_options #:nodoc:
  @default_options
end

#default_params(h = {}) ⇒ Object

Allows setting default parameters to be appended to each request. Great for api keys and such.

class Foo
  include HTTParty
  default_params :api_key => 'secret', :another => 'foo'
end

Raises:

  • (ArgumentError)


79
80
81
82
83
# File 'lib/httparty.rb', line 79

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

#delete(path, options = {}) ⇒ Object



180
181
182
# File 'lib/httparty.rb', line 180

def delete(path, options={})
  perform_request Net::HTTP::Delete, path, options
end

#format(f = nil) ⇒ Object

Allows setting the format with which to parse. Must be one of the allowed formats ie: json, xml

class Foo
  include HTTParty
  format :json
end


109
110
111
112
113
114
115
116
117
# File 'lib/httparty.rb', line 109

def format(f = nil)
  if f.nil?
    default_options[:format]
  else
    parser(Parser) if parser.nil?
    default_options[:format] = f
    validate_format
  end
end

#get(path, options = {}) ⇒ Object

Allows making a get request to a url.

class Foo
  include HTTParty
end

# Simple get with full url
Foo.get('http://foo.com/resource.json')

# Simple get with full url and query parameters
# ie: http://foo.com/resource.json?limit=10
Foo.get('http://foo.com/resource.json', :query => {:limit => 10})


156
157
158
# File 'lib/httparty.rb', line 156

def get(path, options={})
  perform_request Net::HTTP::Get, path, options
end

#head(path, options = {}) ⇒ Object



184
185
186
# File 'lib/httparty.rb', line 184

def head(path, options={})
  perform_request Net::HTTP::Head, path, options
end

#headers(h = {}) ⇒ Object

Allows setting a base uri to be used for each request.

class Foo
  include HTTParty
  headers 'Accept' => 'text/html'
end

Raises:

  • (ArgumentError)


91
92
93
94
95
# File 'lib/httparty.rb', line 91

def headers(h={})
  raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
  default_options[:headers] ||= {}
  default_options[:headers].merge!(h)
end

#http_proxy(addr = nil, port = nil) ⇒ Object

Allows setting http proxy information to be used

class Foo
  include HTTParty
  http_proxy 'http://foo.com', 80
end


45
46
47
48
# File 'lib/httparty.rb', line 45

def http_proxy(addr=nil, port = nil)
  default_options[:http_proxyaddr] = addr
  default_options[:http_proxyport] = port
end

#options(path, options = {}) ⇒ Object



188
189
190
# File 'lib/httparty.rb', line 188

def options(path, options={})
  perform_request Net::HTTP::Options, path, options
end

#parser(customer_parser = nil) ⇒ Object

Allows setting a custom parser for the response.

class Foo
  include HTTParty
  parser Proc.new {|data| ...}
end


135
136
137
138
139
140
141
142
# File 'lib/httparty.rb', line 135

def parser(customer_parser = nil)
  if customer_parser.nil?
    default_options[:parser]
  else
    default_options[:parser] = customer_parser
    validate_format
  end
end

#pem(pem_contents) ⇒ Object

Allows setting a PEM file to be used

class Foo
  include HTTParty
  pem File.read('/home/user/my.pem')
end


125
126
127
# File 'lib/httparty.rb', line 125

def pem(pem_contents)
  default_options[:pem] = pem_contents
end

#post(path, options = {}) ⇒ Object

Allows making a post request to a url.

class Foo
  include HTTParty
end

# Simple post with full url and setting the body
Foo.post('http://foo.com/resources', :body => {:bar => 'baz'})

# Simple post with full url using :query option,
# which gets set as form data on the request.
Foo.post('http://foo.com/resources', :query => {:bar => 'baz'})


172
173
174
# File 'lib/httparty.rb', line 172

def post(path, options={})
  perform_request Net::HTTP::Post, path, options
end

#put(path, options = {}) ⇒ Object



176
177
178
# File 'lib/httparty.rb', line 176

def put(path, options={})
  perform_request Net::HTTP::Put, path, options
end