require"base64"require"http/headers"moduleHTTPmoduleChainable# Request a get sans response body# @param uri# @option options [Hash]defhead(uri,options={})request:head,uri,optionsend# Get a resource# @param uri# @option options [Hash]defget(uri,options={})request:get,uri,optionsend# Post to a resource# @param uri# @option options [Hash]defpost(uri,options={})request:post,uri,optionsend# Put to a resource# @param uri# @option options [Hash]defput(uri,options={})request:put,uri,optionsend# Delete a resource# @param uri# @option options [Hash]defdelete(uri,options={})request:delete,uri,optionsend# Echo the request back to the client# @param uri# @option options [Hash]deftrace(uri,options={})request:trace,uri,optionsend# Return the methods supported on the given URI# @param uri# @option options [Hash]defoptions(uri,options={})request:options,uri,optionsend# Convert to a transparent TCP/IP tunnel# @param uri# @option options [Hash]defconnect(uri,options={})request:connect,uri,optionsend# Apply partial modifications to a resource# @param uri# @option options [Hash]defpatch(uri,options={})request:patch,uri,optionsend# Make an HTTP request with the given verb# @param uri# @option options [Hash]defrequest(verb,uri,options={})branch(options).requestverb,uriend# @overload(options = {})# Syntax sugar for `timeout(:per_operation, options)`# @overload(klass, options = {})# @param [#to_sym] klass# @param [Hash] options# @option options [Float] :read Read timeout# @option options [Float] :write Write timeout# @option options [Float] :connect Connect timeoutdeftimeout(klass,options={})klass,options=:per_operation,klassifklass.is_a?Hashklass=caseklass.to_symwhen:nullthenHTTP::Timeout::Nullwhen:globalthenHTTP::Timeout::Globalwhen:per_operationthenHTTP::Timeout::PerOperationelsefailArgumentError,"Unsupported Timeout class: #{klass}"end[:read,:write,:connect].eachdo|k|nextunlessoptions.key?koptions["#{k}_timeout".to_sym]=options.deletekendbranchdefault_options.merge(:timeout_class=>klass,:timeout_options=>options)end# @overload persistent(host)# Flags as persistent# @param [String] host# @raise [Request::Error] if Host is invalid# @return [HTTP::Client] Persistent client# @overload persistent(host, &block)# Executes given block with persistent client and automatically closes# connection at the end of execution.## @example## def keys(users)# HTTP.persistent("https://github.com") do |http|# users.map { |u| http.get("/#{u}.keys").to_s }# end# end## # same as## def keys(users)# http = HTTP.persistent "https://github.com"# users.map { |u| http.get("/#{u}.keys").to_s }# ensure# http.close if http# end### @yieldparam [HTTP::Client] client Persistent client# @return [Object] result of last expression in the blockdefpersistent(host)p_client=branchdefault_options.with_persistenthostreturnp_clientunlessblock_given?yieldp_clientensurep_client.closeend# Make a request through an HTTP proxy# @param [Array] proxy# @raise [Request::Error] if HTTP proxy is invaliddefvia(*proxy)proxy_hash={}proxy_hash[:proxy_address]=proxy[0]ifproxy[0].is_a?(String)proxy_hash[:proxy_port]=proxy[1]ifproxy[1].is_a?(Integer)proxy_hash[:proxy_username]=proxy[2]ifproxy[2].is_a?(String)proxy_hash[:proxy_password]=proxy[3]ifproxy[3].is_a?(String)if[2,4].include?(proxy_hash.keys.size)branchdefault_options.with_proxy(proxy_hash)elsefail(RequestError,"invalid HTTP proxy: #{proxy_hash}")endendalias_method:through,:via# Make client follow redirects.# @param opts# @return [HTTP::Client]# @see Redirector#initializedeffollow(opts={})branchdefault_options.with_followoptsend# @deprecated will be removed in 1.0.0# @see #followalias_method:with_follow,:follow# Make a request with the given headers# @param headersdefheaders(headers)branchdefault_options.with_headers(headers)end# @deprecated will be removed in 1.0.0# @see #headersalias_method:with,:headers# @deprecated will be removed in 1.0.0# @see #headersalias_method:with_headers,:headers# Make a request with the given cookiesdefcookies(cookies)branchdefault_options.with_cookies(cookies)end# Accept the given MIME type(s)# @param typedefaccept(type)headersHeaders::ACCEPT=>MimeType.normalize(type)end# Make a request with the given Authorization header# @param [#to_s] value Authorization header valuedefauth(value,opts=nil)# shim for deprecated auth(:basic, opts).# will be removed in 0.8.0returnbasic_auth(opts)if:basic==valueheadersHeaders::AUTHORIZATION=>value.to_send# Make a request with the given Basic authorization header# @see http://tools.ietf.org/html/rfc2617# @param [#fetch] opts# @option opts [#to_s] :user# @option opts [#to_s] :passdefbasic_auth(opts)user=opts.fetch:userpass=opts.fetch:passauth("Basic "<<Base64.strict_encode64("#{user}:#{pass}"))end# Get options for HTTP# @return [HTTP::Options]defdefault_options@default_options||=HTTP::Options.newend# Set options for HTTP# @param opts# @return [HTTP::Options]defdefault_options=(opts)@default_options=HTTP::Options.new(opts)end# @deprecated Will be removed in 1.0.0; Use `#default_options#headers`# Get headers of HTTP optionsdefdefault_headersdefault_options.headersend# Set headers of HTTP options# @deprecated Will be removed in 1.0.0; Use `#headers`# @param headersdefdefault_headers=(headers)@default_options=default_options.dupdo|opts|opts.headers=headersendendprivate# :nodoc:defbranch(options)HTTP::Client.new(options)endendend