Module HTTParty::ClassMethods
In: lib/httparty.rb
lib/httparty.rb

Methods

base_uri   base_uri   basic_auth   basic_auth   cookies   cookies   default_params   default_params   delete   delete   format   format   get   get   headers   headers   http_proxy   http_proxy   post   post   put   put  

Public Instance methods

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

[Source]

    # File lib/httparty.rb, line 55
55:     def base_uri(uri=nil)
56:       return default_options[:base_uri] unless uri
57:       default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
58:     end

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

[Source]

    # File lib/httparty.rb, line 55
55:     def base_uri(uri=nil)
56:       return default_options[:base_uri] unless uri
57:       default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
58:     end

Allows setting basic authentication username and password.

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

[Source]

    # File lib/httparty.rb, line 66
66:     def basic_auth(u, p)
67:       default_options[:basic_auth] = {:username => u, :password => p}
68:     end

Allows setting basic authentication username and password.

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

[Source]

    # File lib/httparty.rb, line 66
66:     def basic_auth(u, p)
67:       default_options[:basic_auth] = {:username => u, :password => p}
68:     end

[Source]

    # File lib/httparty.rb, line 95
95:     def cookies(h={})
96:       raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash)
97:       default_cookies.add_cookies(h)
98:     end

[Source]

    # File lib/httparty.rb, line 95
95:     def cookies(h={})
96:       raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash)
97:       default_cookies.add_cookies(h)
98:     end

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

[Source]

    # File lib/httparty.rb, line 77
77:     def default_params(h={})
78:       raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
79:       default_options[:default_params] ||= {}
80:       default_options[:default_params].merge!(h)
81:     end

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

[Source]

    # File lib/httparty.rb, line 77
77:     def default_params(h={})
78:       raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
79:       default_options[:default_params] ||= {}
80:       default_options[:default_params].merge!(h)
81:     end

[Source]

     # File lib/httparty.rb, line 148
148:     def delete(path, options={})
149:       perform_request Net::HTTP::Delete, path, options
150:     end

[Source]

     # File lib/httparty.rb, line 148
148:     def delete(path, options={})
149:       perform_request Net::HTTP::Delete, path, options
150:     end

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

[Source]

     # File lib/httparty.rb, line 107
107:     def format(f)
108:       raise UnsupportedFormat, "Must be one of: #{AllowedFormats.values.map { |v| v.to_s }.uniq.sort.join(', ')}" unless AllowedFormats.value?(f)
109:       default_options[:format] = f
110:     end

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

[Source]

     # File lib/httparty.rb, line 107
107:     def format(f)
108:       raise UnsupportedFormat, "Must be one of: #{AllowedFormats.values.map { |v| v.to_s }.uniq.sort.join(', ')}" unless AllowedFormats.value?(f)
109:       default_options[:format] = f
110:     end

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})

[Source]

     # File lib/httparty.rb, line 124
124:     def get(path, options={})
125:       perform_request Net::HTTP::Get, path, options
126:     end

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})

[Source]

     # File lib/httparty.rb, line 124
124:     def get(path, options={})
125:       perform_request Net::HTTP::Get, path, options
126:     end

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

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

[Source]

    # File lib/httparty.rb, line 89
89:     def headers(h={})
90:       raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
91:       default_options[:headers] ||= {}
92:       default_options[:headers].merge!(h)
93:     end

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

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

[Source]

    # File lib/httparty.rb, line 89
89:     def headers(h={})
90:       raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
91:       default_options[:headers] ||= {}
92:       default_options[:headers].merge!(h)
93:     end

Allows setting http proxy information to be used

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

[Source]

    # File lib/httparty.rb, line 43
43:     def http_proxy(addr=nil, port = nil)
44:       default_options[:http_proxyaddr] = addr
45:       default_options[:http_proxyport] = port
46:     end

Allows setting http proxy information to be used

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

[Source]

    # File lib/httparty.rb, line 43
43:     def http_proxy(addr=nil, port = nil)
44:       default_options[:http_proxyaddr] = addr
45:       default_options[:http_proxyport] = port
46:     end

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'})

[Source]

     # File lib/httparty.rb, line 140
140:     def post(path, options={})
141:       perform_request Net::HTTP::Post, path, options
142:     end

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'})

[Source]

     # File lib/httparty.rb, line 140
140:     def post(path, options={})
141:       perform_request Net::HTTP::Post, path, options
142:     end

[Source]

     # File lib/httparty.rb, line 144
144:     def put(path, options={})
145:       perform_request Net::HTTP::Put, path, options
146:     end

[Source]

     # File lib/httparty.rb, line 144
144:     def put(path, options={})
145:       perform_request Net::HTTP::Put, path, options
146:     end

[Validate]