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.



1843
1844
1845
1846
# File 'lib/mockserver/models.rb', line 1843

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



1841
1842
1843
# File 'lib/mockserver/models.rb', line 1841

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:



1862
1863
1864
1865
1866
1867
1868
# File 'lib/mockserver/models.rb', line 1862

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)


1850
1851
1852
1853
# File 'lib/mockserver/models.rb', line 1850

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:



1874
1875
1876
1877
1878
# File 'lib/mockserver/models.rb', line 1874

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