Class: MockServer::ScenarioHandle

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

Overview

A handle to a single named stateful scenario on the server, wrapping the /mockserver/scenario/{name} control-plane endpoints.

Obtained via Client#scenario:

client.scenario('Deploy').set('Deploying', transition_after_ms: 5000, next_state: 'Deployed')
client.scenario('Deploy').trigger('Failed')
client.scenario('Deploy').state # => "Failed"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, name) ⇒ ScenarioHandle

Returns a new instance of ScenarioHandle.



2133
2134
2135
2136
# File 'lib/mockserver/models.rb', line 2133

def initialize(client, name)
  @client = client
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



2131
2132
2133
# File 'lib/mockserver/models.rb', line 2131

def name
  @name
end

Instance Method Details

#set(state, transition_after_ms: nil, next_state: nil) ⇒ ScenarioState

PUT to set this scenario's state, optionally scheduling a timed transition to next_state after transition_after_ms milliseconds.

Parameters:

  • state (String)

    the state to set

  • transition_after_ms (Integer, nil) (defaults to: nil)

    delay before auto-transitioning

  • next_state (String, nil) (defaults to: nil)

    the state to auto-transition to

Returns:



2152
2153
2154
2155
2156
2157
2158
# File 'lib/mockserver/models.rb', line 2152

def set(state, transition_after_ms: nil, next_state: nil)
  payload = { 'state' => state }
  payload['transitionAfterMs'] = transition_after_ms unless transition_after_ms.nil?
  payload['nextState'] = next_state unless next_state.nil?
  result = @client.scenario_request('PUT', scenario_path, JSON.generate(payload))
  ScenarioState.from_hash(result)
end

#stateString?

GET the current state of this scenario (+nil+ if not yet set).

Returns:

  • (String, nil)


2140
2141
2142
2143
# File 'lib/mockserver/models.rb', line 2140

def state
  result = @client.scenario_request('GET', scenario_path)
  result['currentState']
end

#trigger(new_state) ⇒ ScenarioState

PUT an external trigger advancing this scenario to new_state.

Parameters:

  • new_state (String)

Returns:



2164
2165
2166
2167
2168
# File 'lib/mockserver/models.rb', line 2164

def trigger(new_state)
  body = JSON.generate({ 'newState' => new_state })
  result = @client.scenario_request('PUT', "#{scenario_path}/trigger", body)
  ScenarioState.from_hash(result)
end