Class: Reynard::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/reynard/context.rb

Overview

Exposes a public interface to build a request context.

Instance Method Summary collapse

Constructor Details

#initialize(specification:, inflector:, request_context: nil) ⇒ Context

Returns a new instance of Context.



11
12
13
14
15
# File 'lib/reynard/context.rb', line 11

def initialize(specification:, inflector:, request_context: nil)
  @specification = specification
  @inflector = inflector
  @request_context = request_context || build_request_context
end

Instance Method Details

#base_url(base_url) ⇒ Object



17
18
19
# File 'lib/reynard/context.rb', line 17

def base_url(base_url)
  copy(base_url:)
end

#body(data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reynard/context.rb', line 30

def body(data)
  return unless @request_context.operation

  serialized_body = @specification.build_body(@request_context.operation.node, data)
  return unless serialized_body

  copy(
    headers: @request_context.headers.merge(serialized_body.headers),
    body: serialized_body.to_s
  )
end

#executeObject



50
51
52
# File 'lib/reynard/context.rb', line 50

def execute
  build_response(build_request.perform)
end

#headers(headers) ⇒ Object



42
43
44
# File 'lib/reynard/context.rb', line 42

def headers(headers)
  copy(headers: @request_context.headers.merge(headers))
end

#logger(logger) ⇒ Object



46
47
48
# File 'lib/reynard/context.rb', line 46

def logger(logger)
  copy(logger:)
end

#operation(operation_name) ⇒ Object



21
22
23
# File 'lib/reynard/context.rb', line 21

def operation(operation_name)
  copy(operation: @specification.operation(operation_name))
end

#params(params) ⇒ Object



25
26
27
28
# File 'lib/reynard/context.rb', line 25

def params(params)
  params = params.transform_keys(&:to_s)
  copy(params: @specification.build_grouped_params(@request_context.operation.node, params))
end