# File lib/http/options.rb, line 46 def new(options = {}) return options if options.is_a?(self) super end
# 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
# File lib/http/options.rb, line 133 def [](option) send(option) rescue nil end
# File lib/http/options.rb, line 172 def dup dupped = super yield(dupped) if block_given? dupped end
# 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
# 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
# File lib/http/options.rb, line 105 def with_body(body) dup do |opts| opts.body = body end end
# 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
# File lib/http/options.rb, line 111 def with_follow(follow) dup do |opts| opts.follow = follow end end
# File lib/http/options.rb, line 99 def with_form(form) dup do |opts| opts.form = form end end
# 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
# File lib/http/options.rb, line 93 def with_params(params) dup do |opts| opts.params = params end end
Generated with the Darkfish Rdoc Generator 2.