Module: HTTPX::Chainable

Included in:
HTTPX, Session
Defined in:
lib/httpx/chainable.rb

Overview

Session mixin, implements most of the APIs that the users call. delegates to a default session when extended.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, **options, &blk) ⇒ Object (private)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/httpx/chainable.rb', line 56

def method_missing(meth, *args, **options, &blk)
  case meth
  when /\Awith_(.+)/

    option = Regexp.last_match(1)

    return super unless option

    with(option.to_sym => args.first || options)
  when /\Aon_(.+)/
    callback = Regexp.last_match(1)

    return super unless %w[
      connection_opened connection_closed
      request_error
      request_started request_body_chunk request_completed
      response_started response_body_chunk response_completed
    ].include?(callback)

    warn "DEPRECATION WARNING: calling `.#{meth}` on plain HTTPX sessions is deprecated. " \
         "Use `HTTPX.plugin(:callbacks).#{meth}` instead."

    plugin(:callbacks).__send__(meth, *args, **options, &blk)
  else
    super
  end
end

Instance Method Details

#accept(type) ⇒ Object



20
21
22
# File 'lib/httpx/chainable.rb', line 20

def accept(type)
  with(headers: { "accept" => String(type) })
end

#plugin(pl, options = nil, &blk) ⇒ Object

returns a new instance loaded with the pl plugin and options.



30
31
32
33
34
35
# File 'lib/httpx/chainable.rb', line 30

def plugin(pl, options = nil, &blk)
  klass = is_a?(S) ? self.class : Session
  klass = Class.new(klass)
  klass.instance_variable_set(:@default_options, klass.default_options.merge(default_options))
  klass.plugin(pl, options, &blk).new
end

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

delegates to the default session (see HTTPX::Session#request).



16
17
18
# File 'lib/httpx/chainable.rb', line 16

def request(*args, **options)
  branch(default_options).request(*args, **options)
end

#with(options, &blk) ⇒ Object

returns a new instance loaded with options.



38
39
40
# File 'lib/httpx/chainable.rb', line 38

def with(options, &blk)
  branch(default_options.merge(options), &blk)
end

#wrap(&blk) ⇒ Object

delegates to the default session (see HTTPX::Session#wrap).



25
26
27
# File 'lib/httpx/chainable.rb', line 25

def wrap(&blk)
  branch(default_options).wrap(&blk)
end