Class: HTTPX::Session

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

Direct Known Subclasses

Faraday::Adapter::HTTPX::Session

Constant Summary

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, #timeout, #with

Methods included from Loggable

#log, #log_exception

Constructor Details

#initialize(options = {}, &blk) ⇒ Session

Returns a new instance of Session.



8
9
10
11
12
13
# File 'lib/httpx/session.rb', line 8

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

Class Attribute Details

.default_optionsObject (readonly)

Returns the value of attribute default_options.



213
214
215
# File 'lib/httpx/session.rb', line 213

def default_options
  @default_options
end

Class Method Details

.inherited(klass) ⇒ Object



215
216
217
218
219
# File 'lib/httpx/session.rb', line 215

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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/httpx/session.rb', line 221

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)
  unless @plugins.include?(pl)
    @plugins << pl
    pl.load_dependencies(self, &block) if pl.respond_to?(:load_dependencies)
    @default_options = @default_options.dup
    @default_options = pl.extra_options(@default_options, &block) if pl.respond_to?(:extra_options)
    @default_options = @default_options.merge(options) if options

    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)
    pl.configure(self, &block) if pl.respond_to?(:configure)

    @default_options.freeze
  end
  self
end

.plugins(pls) ⇒ Object



253
254
255
256
257
258
# File 'lib/httpx/session.rb', line 253

def plugins(pls)
  pls.each do |pl, *args|
    plugin(pl, *args)
  end
  self
end

Instance Method Details

#close(*args) ⇒ Object



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

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

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



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

def request(*args, **options)
  requests = build_requests(*args, options)
  responses = send_requests(*requests, options)
  return responses.first if responses.size == 1

  responses
end

#wrapObject



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

def wrap
  return unless block_given?

  begin
    prev_persistent = @persistent
    @persistent = true
    yield self
  ensure
    @persistent = prev_persistent
  end
end