Class: Pact::Generator::ProviderState

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/pact/generator/provider_state.rb

Overview

ProviderState provides the provider state generator which will inject values provided by the provider state setup url.

Constant Summary collapse

VALUES_SEPARATOR =
','
START_EXPRESSION =
"\${"
END_EXPRESSION =
'}'

Instance Method Summary collapse

Methods included from Logging

included, #logger

Instance Method Details

#call(hash, params = nil, _example_value = nil) ⇒ Object



19
20
21
22
# File 'lib/pact/generator/provider_state.rb', line 19

def call(hash, params = nil, _example_value = nil)
  params ||= {}
  parse_expression hash['expression'], params
end

#can_generate?(hash) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/pact/generator/provider_state.rb', line 15

def can_generate?(hash)
  hash.key?('type') && hash['type'] == 'ProviderState'
end

#parse_expression(expression, params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pact/generator/provider_state.rb', line 24

def parse_expression(expression, params)
  return_string = []
  buffer = expression
  # initial value
  position = buffer.index(START_EXPRESSION)

  while position && position >= 0
    if position.positive?
      # add string
      return_string.push(buffer[0...position])
    end
    end_position = buffer.index(END_EXPRESSION, position)
    raise 'Missing closing brace in expression string' if !end_position || end_position.negative?

    variable = buffer[position + 2...end_position]

    logger.info "Could not subsitute provider state key #{variable}, have #{params}" unless params[variable]

    expression = params[variable] || ''
    return_string.push(expression)

    buffer = buffer[end_position + 1...-1]
    position = buffer.index(START_EXPRESSION)
  end

  return_string.join('')
end