Class: Loadimpact::UserScenario

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

Overview

Public: Various methods useful for managing a user's UserScenarios. All methods are instance methods and should be called on the UserScenario object.

Constant Summary collapse

STATUS_FIELD =

Public: Hash status field descriptions and values of UserScenario validation.

{
  :queued => 0,
  :initializing => 1,
  :running => 2,
  :finished => 3,
  :failed => 4
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ UserScenario

Public: Initialize a UserScenario.

client - A Client object. options - The Hash options used to initialize the UserScenario:

:id - The Integer id of the UserScenario (required).
:name - The String name of the UserScenario (required).
:load_script - The String Lua load script of the UserScenario (required).


23
24
25
26
27
28
29
# File 'lib/loadimpact/user_scenario.rb', line 23

def initialize(client, options = {})
  @client = client
  @id = options["id"]
  @validation_id = nil
  @name = options["name"]
  @load_script = options["load_script"]
end

Instance Attribute Details

#idObject (readonly)

Public: Returns the Integer id of the UserScenario.



14
15
16
# File 'lib/loadimpact/user_scenario.rb', line 14

def id
  @id
end

Instance Method Details

#clone(new_name) ⇒ Object

Public: Clone UserScenario and give it a new name.

new_name - The String new name of cloned UserScenario.

Examples

cloned_user_scenario = user_scenario.clone("My cloned user scenario")
# => #<Loadimpact::UserScenario>

Returns UserScenario object if successfully cloned. Raises LoadimpactError if invalid HTTP request. Raises DuplicateUserScenarioError if UserScenario name already exists.



98
99
100
101
102
103
104
105
106
107
# File 'lib/loadimpact/user_scenario.rb', line 98

def clone(new_name)
  res = @client.post("/user-scenarios/#{@id}/clone", body: { name: new_name }.to_json)
  if res.code == 409
    raise DuplicateUserScenarioError.new
  elsif res.key?("message")
    raise LoadimpactError.new(res["message"])
  end
  hsh = JSON.parse(res.body)
  UserScenario.new(@client, hsh)
end

#deleteObject

Public: Delete UserScenario.

Examples

user_scenario.delete
# => 204

Returns HTTP status code 204 if successfully deleted. Raises LoadimpactError if invalid HTTP request.

Raises:



80
81
82
83
84
# File 'lib/loadimpact/user_scenario.rb', line 80

def delete
  res = @client.delete("/user-scenarios/#{@id}")
  raise LoadimpactError.new(res["message"]) if res.key?("message")
  res.code
end

#infoObject

Public: Retrieve UserScenario information.

Examples

user_scenario.info
# => {"id"=>12345, "name"=>"My user scenario", # omitted }

Returns Hash of UserScenario information. Raises LoadimpactError if invalid HTTP request.

Raises:



40
41
42
43
44
# File 'lib/loadimpact/user_scenario.rb', line 40

def info
  res = @client.get("/user-scenarios/#{@id}")
  raise LoadimpactError.new(res["message"]) if res.key?("message")
  JSON.parse(res.body)
end

#update(options = {}) ⇒ Object

Public: Update UserScenario.

options - The Hash options used to update the UserScenario:

:name - The String name of the UserScenario (optional).
:load_script - The String Lua load script of the UserScenario (optional).

Examples

user_scenario.update(name: "My updated user scenario")
# => 200

Returns HTTP status code 200 if successfully updated. Raises LoadimpactError if invalid HTTP request. Raises InvalidDataStoreError if invalid data store.



60
61
62
63
64
65
66
67
68
69
# File 'lib/loadimpact/user_scenario.rb', line 60

def update(options = {})
  user_scenario_builder(options)
  res = @client.put("/user-scenarios/#{@id}", body: payload)
  if res.code == 422
    raise InvalidDataStoreError.new
  elsif res.key?("message")
    raise LoadimpactError.new(res["message"])
  end
  res.code
end

#valid?Boolean

Public: Validate UserScenario.

Examples

user_scenario.valid?
# => true

Returns true if UserScenario is valid. Returns false if UserScenario is invalid. Raises LoadimpactError if invalid HTTP request. Raises UserScenarioValidationLimitError if hit limit of validations simultaneously running (3).

Returns:

  • (Boolean)

Raises:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/loadimpact/user_scenario.rb', line 120

def valid?
  if @validation_id.nil?
    res = @client.post("/user-scenario-validations", body: { user_scenario_id: @id.to_s }.to_json)
    if res.code == 429
      raise UserScenarioValidationLimitError.new
    elsif res.key?("message")
      raise LoadimpactError.new(res["message"])
    end
    hsh = JSON.parse(res.body)
    @validation_id = hsh["id"]
  end
  res = @client.get("/user-scenario-validations/#{@validation_id}")
  raise LoadimpactError.new(res["message"]) if res.key?("message")
  res["status"] == STATUS_FIELD[:finished] ? true : false
end