Sha256: 9e84b10ba6b4acf6f0c7001f9530334f8e4fb590d1233b36940ac84ad6738448

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

class Toxiproxy
  class Toxic
    attr_reader :name, :type, :stream, :proxy
    attr_accessor :attributes, :toxicity

    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

    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

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
toxiproxy-2.0.2 lib/toxiproxy/toxic.rb
toxiproxy-2.0.1 lib/toxiproxy/toxic.rb
toxiproxy-2.0.0 lib/toxiproxy/toxic.rb
toxiproxy-1.0.3 lib/toxiproxy/toxic.rb