Class: RequestFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/request_factory.rb

Class Method Summary collapse

Class Method Details

.build_data(request, interpolator) ⇒ Object



36
37
38
39
40
# File 'lib/request_factory.rb', line 36

def self.build_data(request, interpolator)
  return nil if request.data.nil?

  interpolator.interpolate(request.data)
end

.build_headers(environment, context, request, interpolator) ⇒ Object



24
25
26
27
28
29
# File 'lib/request_factory.rb', line 24

def self.build_headers(environment, context, request, interpolator)
  environment.headers
    .merge(context.headers)
    .merge(request.headers)
    .map {|key, value| [key, interpolator.interpolate(value)]}.to_h
end

.build_uri(environment, context, request, interpolator) ⇒ Object



31
32
33
34
# File 'lib/request_factory.rb', line 31

def self.build_uri(environment, context, request, interpolator)
  uri = environment.endpoint + request.path
  interpolator.interpolate(uri)
end

.create(config, state, request) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/request_factory.rb', line 2

def self.create(config, state, request)
  environment = config.get_environment(state.environment) || Environment.new
  context = config.get_context(state.context) || Context.new

  variables = merge_variables(environment, context, request, state.variables)
  interpolator = VariableInterpolator.new(variables)

  PreparedRequest.new({
    headers: build_headers(environment, context, request, interpolator),
    method: request.method.downcase.to_sym,
    uri: build_uri(environment, context, request, interpolator),
    data: build_data(request, interpolator)
  })
end

.merge_variables(environment, context, request, custom_variables) ⇒ Object



17
18
19
20
21
22
# File 'lib/request_factory.rb', line 17

def self.merge_variables(environment, context, request, custom_variables)
  (environment.variables || {})
    .merge(context.variables || {})
    .merge(request.variables || {})
    .merge(custom_variables || {})
end