Class: Toxiproxy::Toxic

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Toxic

Returns a new instance of Toxic.



6
7
8
9
10
11
12
13
14
# File 'lib/toxiproxy/toxic.rb', line 6

def initialize(attrs)
  raise "Toxic type is required" unless attrs[:type]
  @type = attrs[:type]
  @stream = attrs[:stream] || 'downstream'
  @name = attrs[:name] || "#{@type}_#{@stream}"
  @proxy = attrs[:proxy]
  @toxicity = attrs[:toxicity] || 1.0
  @attributes = attrs[:attributes] || {}
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/toxiproxy/toxic.rb', line 4

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/toxiproxy/toxic.rb', line 3

def name
  @name
end

#proxyObject (readonly)

Returns the value of attribute proxy.



3
4
5
# File 'lib/toxiproxy/toxic.rb', line 3

def proxy
  @proxy
end

#streamObject (readonly)

Returns the value of attribute stream.



3
4
5
# File 'lib/toxiproxy/toxic.rb', line 3

def stream
  @stream
end

#toxicityObject

Returns the value of attribute toxicity.



4
5
6
# File 'lib/toxiproxy/toxic.rb', line 4

def toxicity
  @toxicity
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/toxiproxy/toxic.rb', line 3

def type
  @type
end

Instance Method Details

#as_jsonObject



39
40
41
42
43
44
45
46
47
# File 'lib/toxiproxy/toxic.rb', line 39

def as_json
  {
    name: name,
    type: type,
    stream: stream,
    toxicity: toxicity,
    attributes: attributes,
  }.to_json
end

#destroyObject



32
33
34
35
36
37
# File 'lib/toxiproxy/toxic.rb', line 32

def destroy
  request = Net::HTTP::Delete.new("/proxies/#{proxy.name}/toxics/#{name}")
  response = Toxiproxy.http_request(request)
  Toxiproxy.assert_response(response)
  self
end

#saveObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/toxiproxy/toxic.rb', line 16

def save
  request = Net::HTTP::Post.new("/proxies/#{proxy.name}/toxics")
  request["Content-Type"] = "application/json"

  request.body = as_json

  response = Toxiproxy.http_request(request)
  Toxiproxy.assert_response(response)

  json = JSON.parse(response.body)
  @attributes = json['attributes']
  @toxicity = json['toxicity']

  self
end