Sha256: b50dcdb536c845e3a105bf9d4af4885edb59f5fd6fa9591ba9e2320dd1ae34d2

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

class Toxiproxy
  class Toxic
    attr_reader :name, :type, :attributes, :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.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

1 entries across 1 versions & 1 rubygems

Version Path
toxiproxy-1.0.0 lib/toxiproxy/toxic.rb