Class: DHC::Request

Inherits:
Object
  • Object
show all
Includes:
UserAgentConcern
Defined in:
lib/dhc/request.rb,
lib/dhc/concerns/dhc/request/user_agent_concern.rb

Overview

The request is doing an http-request using typhoeus. It provides functionalities to access and alter request data and it communicates with interceptors.

Defined Under Namespace

Modules: UserAgentConcern

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, self_executing = true) ⇒ Request

Returns a new instance of Request.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dhc/request.rb', line 17

def initialize(options, self_executing = true)
  self.errors_ignored = (options.fetch(:ignore, []) || []).to_a.compact
  self.source = options&.dig(:source)
  self.options = format!(options.deep_dup || {})
  self.error_handler = options.delete :rescue
  use_configured_endpoint!
  generate_url_from_template!
  self.interceptors = DHC::Interceptors.new(self)
  interceptors.intercept(:before_init)
  self.raw = create_request
  interceptors.intercept(:before_request)
  if self_executing && !response
    run!
  elsif response
    on_complete(response)
  end
end

Instance Attribute Details

#error_handlerObject

Returns the value of attribute error_handler.



15
16
17
# File 'lib/dhc/request.rb', line 15

def error_handler
  @error_handler
end

#errors_ignoredObject

Returns the value of attribute errors_ignored.



15
16
17
# File 'lib/dhc/request.rb', line 15

def errors_ignored
  @errors_ignored
end

#formatObject

Returns the value of attribute format.



15
16
17
# File 'lib/dhc/request.rb', line 15

def format
  @format
end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/dhc/request.rb', line 15

def options
  @options
end

#rawObject

Returns the value of attribute raw.



15
16
17
# File 'lib/dhc/request.rb', line 15

def raw
  @raw
end

#responseObject

Returns the value of attribute response.



15
16
17
# File 'lib/dhc/request.rb', line 15

def response
  @response
end

#sourceObject

Returns the value of attribute source.



15
16
17
# File 'lib/dhc/request.rb', line 15

def source
  @source
end

Instance Method Details

#error_ignored?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dhc/request.rb', line 51

def error_ignored?
  ignore_error?
end

#headersObject



43
44
45
# File 'lib/dhc/request.rb', line 43

def headers
  raw.options.fetch(:headers, nil) || raw.options[:headers] = {}
end

#methodObject



39
40
41
# File 'lib/dhc/request.rb', line 39

def method
  (raw.options[:method] || options[:method] || :get).to_sym
end

#paramsObject



47
48
49
# File 'lib/dhc/request.rb', line 47

def params
  raw.options.fetch(:params, nil) || raw.options[:params] = {}
end

#run!Object



55
56
57
# File 'lib/dhc/request.rb', line 55

def run!
  raw.run
end

#urlObject



35
36
37
# File 'lib/dhc/request.rb', line 35

def url
  raw.base_url || options[:url]
end