Module: HTTParty::ClassMethods
- Extended by:
- AllowedFormatsDeprecation
- Defined in:
- lib/httparty.rb
Instance Method Summary collapse
-
#base_uri(uri = nil) ⇒ Object
Allows setting a base uri to be used for each request.
-
#basic_auth(u, p) ⇒ Object
Allows setting basic authentication username and password.
- #cookies(h = {}) ⇒ Object
-
#default_options ⇒ Object
:nodoc:.
-
#default_params(h = {}) ⇒ Object
Allows setting default parameters to be appended to each request.
- #delete(path, options = {}) ⇒ Object
-
#format(f = nil) ⇒ Object
Allows setting the format with which to parse.
-
#get(path, options = {}) ⇒ Object
Allows making a get request to a url.
- #head(path, options = {}) ⇒ Object
-
#headers(h = {}) ⇒ Object
Allows setting a base uri to be used for each request.
-
#http_proxy(addr = nil, port = nil) ⇒ Object
Allows setting http proxy information to be used.
- #options(path, options = {}) ⇒ Object
-
#parser(customer_parser = nil) ⇒ Object
Allows setting a custom parser for the response.
-
#pem(pem_contents) ⇒ Object
Allows setting a PEM file to be used.
-
#post(path, options = {}) ⇒ Object
Allows making a post request to a url.
- #put(path, options = {}) ⇒ Object
Methods included from AllowedFormatsDeprecation
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 [:base_uri] unless uri [: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) [:basic_auth] = {:username => u, :password => p} end |
#cookies(h = {}) ⇒ Object
97 98 99 100 |
# File 'lib/httparty.rb', line 97 def (h={}) raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash) .(h) end |
#default_options ⇒ Object
:nodoc:
192 193 194 |
# File 'lib/httparty.rb', line 192 def #: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
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_params] ||= {} [:default_params].merge!(h) end |
#delete(path, options = {}) ⇒ Object
180 181 182 |
# File 'lib/httparty.rb', line 180 def delete(path, ={}) perform_request Net::HTTP::Delete, path, 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? [:format] else parser(Parser) if parser.nil? [: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, ={}) perform_request Net::HTTP::Get, path, end |
#head(path, options = {}) ⇒ Object
184 185 186 |
# File 'lib/httparty.rb', line 184 def head(path, ={}) perform_request Net::HTTP::Head, path, end |
#headers(h = {}) ⇒ Object
Allows setting a base uri to be used for each request.
class Foo
include HTTParty
headers 'Accept' => 'text/html'
end
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) [:headers] ||= {} [: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) [:http_proxyaddr] = addr [:http_proxyport] = port end |
#options(path, options = {}) ⇒ Object
188 189 190 |
# File 'lib/httparty.rb', line 188 def (path, ={}) perform_request Net::HTTP::Options, path, 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? [:parser] else [: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) [: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, ={}) perform_request Net::HTTP::Post, path, end |
#put(path, options = {}) ⇒ Object
176 177 178 |
# File 'lib/httparty.rb', line 176 def put(path, ={}) perform_request Net::HTTP::Put, path, end |