Class: HTTPX::Session

Inherits:
Object
  • Object
show all
Includes:
Chainable, Loggable
Defined in:
lib/httpx/session.rb,
lib/httpx/session2.rb

Direct Known Subclasses

Faraday::Adapter::HTTPX::Session

Constant Summary collapse

EMPTY_HASH =
{}.freeze

Constants included from Loggable

Loggable::COLORS

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chainable

#accept, #headers, #plugin, #plugins, #timeout, #with

Methods included from Loggable

#log, #log_exception

Constructor Details

#initialize(options = EMPTY_HASH, &blk) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
# File 'lib/httpx/session.rb', line 10

def initialize(options = EMPTY_HASH, &blk)
  @options = self.class.default_options.merge(options)
  @responses = {}
  @persistent = @options.persistent
  wrap(&blk) if blk
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HTTPX::Chainable

Class Attribute Details

.default_optionsObject (readonly)

Returns the value of attribute default_options.



243
244
245
# File 'lib/httpx/session.rb', line 243

def default_options
  @default_options
end

Class Method Details

.inherited(klass) ⇒ Object



245
246
247
248
249
# File 'lib/httpx/session.rb', line 245

def inherited(klass)
  super
  klass.instance_variable_set(:@default_options, @default_options)
  klass.instance_variable_set(:@plugins, @plugins.dup)
end

.plugin(pl, options = nil, &block) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/httpx/session.rb', line 251

def plugin(pl, options = nil, &block)
  # raise Error, "Cannot add a plugin to a frozen config" if frozen?
  pl = Plugins.load_plugin(pl) if pl.is_a?(Symbol)
  if !@plugins.include?(pl)
    @plugins << pl
    pl.load_dependencies(self, &block) if pl.respond_to?(:load_dependencies)

    @default_options = @default_options.dup

    include(pl::InstanceMethods) if defined?(pl::InstanceMethods)
    extend(pl::ClassMethods) if defined?(pl::ClassMethods)

    opts = @default_options
    opts.request_class.__send__(:include, pl::RequestMethods) if defined?(pl::RequestMethods)
    opts.request_class.extend(pl::RequestClassMethods) if defined?(pl::RequestClassMethods)
    opts.response_class.__send__(:include, pl::ResponseMethods) if defined?(pl::ResponseMethods)
    opts.response_class.extend(pl::ResponseClassMethods) if defined?(pl::ResponseClassMethods)
    opts.headers_class.__send__(:include, pl::HeadersMethods) if defined?(pl::HeadersMethods)
    opts.headers_class.extend(pl::HeadersClassMethods) if defined?(pl::HeadersClassMethods)
    opts.request_body_class.__send__(:include, pl::RequestBodyMethods) if defined?(pl::RequestBodyMethods)
    opts.request_body_class.extend(pl::RequestBodyClassMethods) if defined?(pl::RequestBodyClassMethods)
    opts.response_body_class.__send__(:include, pl::ResponseBodyMethods) if defined?(pl::ResponseBodyMethods)
    opts.response_body_class.extend(pl::ResponseBodyClassMethods) if defined?(pl::ResponseBodyClassMethods)
    opts.connection_class.__send__(:include, pl::ConnectionMethods) if defined?(pl::ConnectionMethods)
    if defined?(pl::OptionsMethods)
      opts.options_class.__send__(:include, pl::OptionsMethods)

      (pl::OptionsMethods.instance_methods - Object.instance_methods).each do |meth|
        opts.options_class.method_added(meth)
      end
      @default_options = opts.options_class.new(opts)
    end

    @default_options = pl.extra_options(@default_options) if pl.respond_to?(:extra_options)
    @default_options = @default_options.merge(options) if options

    pl.configure(self, &block) if pl.respond_to?(:configure)

    @default_options.freeze
  elsif options
    # this can happen when two plugins are loaded, an one of them calls the other under the hood,
    # albeit changing some default.
    @default_options = pl.extra_options(@default_options) if pl.respond_to?(:extra_options)
    @default_options = @default_options.merge(options) if options

    @default_options.freeze
  end
  self
end

.plugins(pls) ⇒ Object

:nocov:



302
303
304
305
306
307
308
# File 'lib/httpx/session.rb', line 302

def plugins(pls)
  warn ":#{__method__} is deprecated, use :plugin instead"
  pls.each do |pl|
    plugin(pl)
  end
  self
end

Instance Method Details

#build_request(verb, uri, options = EMPTY_HASH) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/httpx/session.rb', line 42

def build_request(verb, uri, options = EMPTY_HASH)
  rklass = @options.request_class
  options = @options.merge(options) unless options.is_a?(Options)
  request = rklass.new(verb, uri, options.merge(persistent: @persistent))
  request.on(:response, &method(:on_response).curry(2)[request])
  request.on(:promise, &method(:on_promise))
  request
end

#close(*args) ⇒ Object



28
29
30
# File 'lib/httpx/session.rb', line 28

def close(*args)
  pool.close(*args)
end

#request(*args, **options) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/httpx/session.rb', line 32

def request(*args, **options)
  raise ArgumentError, "must perform at least one request" if args.empty?

  requests = args.first.is_a?(Request) ? args : build_requests(*args, options)
  responses = send_requests(*requests)
  return responses.first if responses.size == 1

  responses
end

#wrapObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/httpx/session.rb', line 17

def wrap
  begin
    prev_persistent = @persistent
    @persistent = true
    yield self
  ensure
    @persistent = prev_persistent
    close unless @persistent
  end
end