Parent

HTTP::Options

Attributes

default_socket_class[RW]
default_ssl_socket_class[RW]
body[RW]

Explicit request body of the request

callbacks[RW]

Before callbacks

follow[RW]

Follow redirects

form[RW]

Form data to embed in the request

headers[RW]

HTTP headers to include in the request

params[RW]

Query string params to add to the url

proxy[RW]

HTTP proxy to route request

response[RW]

How to format the response [:object, :body, :parse_body]

socket_class[RW]

Socket classes

ssl_context[RW]

SSL context

ssl_socket_class[RW]

Socket classes

Public Class Methods

new(options = {}) click to toggle source
# File lib/http/options.rb, line 46
def new(options = {})
  return options if options.is_a?(self)
  super
end
new(options = {}) click to toggle source
# File lib/http/options.rb, line 52
def initialize(options = {})
  @response  = options[:response]  || :auto
  @headers   = options[:headers]   || {}
  @proxy     = options[:proxy]     || {}
  @callbacks = options[:callbacks] || {:request => [], :response => []}
  @body      = options[:body]
  @params      = options[:params]
  @form      = options[:form]
  @follow    = options[:follow]

  @socket_class     = options[:socket_class]     || self.class.default_socket_class
  @ssl_socket_class = options[:ssl_socket_class] || self.class.default_ssl_socket_class
  @ssl_context      = options[:ssl_context]

  @headers["User-Agent"] ||= "RubyHTTPGem/#{HTTP::VERSION}"
end

Public Instance Methods

[](option) click to toggle source
# File lib/http/options.rb, line 133
def [](option)
  send(option) rescue nil
end
dup() click to toggle source
# File lib/http/options.rb, line 172
def dup
  dupped = super
  yield(dupped) if block_given?
  dupped
end
merge(other) click to toggle source
# File lib/http/options.rb, line 137
def merge(other)
  h1, h2 = to_hash, other.to_hash
  merged = h1.merge(h2) do |k,v1,v2|
    case k
    when :headers
      v1.merge(v2)
    when :callbacks
      v1.merge(v2){|event,l,r| (l+r).uniq}
    else
      v2
    end
  end

  self.class.new(merged)
end
to_hash() click to toggle source
# File lib/http/options.rb, line 153
def to_hash
  # FIXME: hardcoding these fields blows! We should have a declarative
  # way of specifying all the options fields, and ensure they *all*
  # get serialized here, rather than manually having to add them each time
  {
    :response         => response,
    :headers          => headers,
    :proxy            => proxy,
    :params           => params,
    :form             => form,
    :body             => body,
    :callbacks        => callbacks,
    :follow           => follow,
    :socket_class     => socket_class,
    :ssl_socket_class => ssl_socket_class,
    :ssl_context      => ssl_context
 }
end
with_body(body) click to toggle source
# File lib/http/options.rb, line 105
def with_body(body)
  dup do |opts|
    opts.body = body
  end
end
with_callback(event, callback) click to toggle source
# File lib/http/options.rb, line 117
def with_callback(event, callback)
  unless callback.respond_to?(:call)
    argument_error! "invalid callback: #{callback}"
  end
  unless callback.respond_to?(:arity) and callback.arity == 1
    argument_error! "callback must accept only one argument"
  end
  unless [:request, :response].include?(event)
    argument_error! "invalid callback event: #{event}"
  end
  dup do |opts|
    opts.callbacks = callbacks.dup
    opts.callbacks[event] = (callbacks[event].dup << callback)
  end
end
with_follow(follow) click to toggle source
# File lib/http/options.rb, line 111
def with_follow(follow)
  dup do |opts|
    opts.follow = follow
  end
end
with_form(form) click to toggle source
# File lib/http/options.rb, line 99
def with_form(form)
  dup do |opts|
    opts.form = form
  end
end
with_headers(headers) click to toggle source
# File lib/http/options.rb, line 78
def with_headers(headers)
  unless headers.respond_to?(:to_hash)
    argument_error! "invalid headers: #{headers}"
  end
  dup do |opts|
    opts.headers = self.headers.merge(headers.to_hash)
  end
end
with_params(params) click to toggle source
# File lib/http/options.rb, line 93
def with_params(params)
  dup do |opts|
    opts.params = params
  end
end
with_proxy(proxy_hash) click to toggle source
# File lib/http/options.rb, line 87
def with_proxy(proxy_hash)
  dup do |opts|
    opts.proxy = proxy_hash
  end
end
with_response(response) click to toggle source
# File lib/http/options.rb, line 69
def with_response(response)
  unless [:auto, :object, :body, :parsed_body].include?(response)
    argument_error! "invalid response type: #{response}"
  end
  dup do |opts|
    opts.response = response
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.