Class: HTTP::Session

Inherits:
Object
  • Object
show all
Includes:
Configurable, Requestable
Defined in:
lib/http/session.rb,
lib/http/session/cache.rb,
lib/http/session/client.rb,
lib/http/session/context.rb,
lib/http/session/cookies.rb,
lib/http/session/options.rb,
lib/http/session/request.rb,
lib/http/session/version.rb,
lib/http/session/features.rb,
lib/http/session/response.rb,
lib/http/session/exceptions.rb,
lib/http/session/redirector.rb,
lib/http/session/cache/entry.rb,
lib/http/session/requestable.rb,
lib/http/session/cache/status.rb,
lib/http/session/configurable.rb,
lib/http/session/pool_manager.rb,
lib/http/session/client/perform.rb,
lib/http/session/connection_pool.rb,
lib/http/session/options/optionable.rb,
lib/http/session/cache/cache_control.rb,
lib/http/session/options/cache_option.rb,
lib/http/session/response/string_body.rb,
lib/http/session/features/auto_inflate.rb,
lib/http/session/context/follow_context.rb,
lib/http/session/options/cookies_option.rb,
lib/http/session/options/persistent_option.rb

Overview

Use session to manage cookies and cache across requests.

Defined Under Namespace

Modules: Configurable, Exceptions, Features, Requestable Classes: Cache, Client, ConnectionPool, Context, Cookies, Options, PoolManager, Redirector, Request, Response

Constant Summary collapse

VERSION =
"2.1.0"

Instance Method Summary collapse

Methods included from Requestable

#connect, #delete, #get, #head, #options, #patch, #post, #put, #trace

Methods included from Configurable

#accept, #auth, #basic_auth, #cookies, #encoding, #follow, #headers, #nodelay, #timeout, #use, #via

Constructor Details

#initialize(default_options = {}) ⇒ Session

Returns a new instance of Session.

Parameters:

  • default_options (Hash) (defaults to: {})

Options Hash (default_options):

  • :cookies (Boolean, Hash)

    session cookies option

  • :cache (Boolean, Hash)

    session cache option

  • :persistent (Boolean, Hash)

    session persistent option

  • :http (Hash)

    http client options



52
53
54
55
56
57
58
59
# File 'lib/http/session.rb', line 52

def initialize(default_options = {})
  super()

  @default_options = HTTP::Session::Options.new(default_options)
  @cookies_mgr = HTTP::Session::Cookies.new(@default_options.cookies)
  @cache_mgr = HTTP::Session::Cache.new(@default_options.cache)
  @pool_mgr = HTTP::Session::PoolManager.new(@default_options.persistent, self)
end

Instance Method Details

#request(verb, uri, opts = {}) ⇒ Response

Parameters:

  • verb
  • uri
  • opts (Hash) (defaults to: {})

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/http/session.rb', line 65

def request(verb, uri, opts = {})
  http_opts = @default_options.http.merge(opts)
  opts[:follow] = false

  res = perform(verb, uri, opts)
  return res unless http_opts.follow

  redirector = HTTP::Session::Redirector.new(http_opts.follow)
  redirector.perform(res) do |new_verb, new_uri, follow_ctx|
    new_opts = follow_ctx.same_origin? ? opts : {}
    ctx = HTTP::Session::Context.new(follow: follow_ctx)
    perform(new_verb, new_uri, new_opts, ctx)
  end
end