Sha256: 41e13d0b609baf0d879d10d76d7006fa85f212321da140773e07d1964300c43f

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

class Toxiproxy
  class Toxic
    attr_reader :name, :proxy, :direction
    attr_reader :attrs

    def initialize(options)
      @proxy     = options[:proxy]
      @name      = options[:name]
      @direction = options[:direction]
      @attrs     = options[:attrs] || {}
    end

    def enabled?
      attrs['enabled']
    end

    def enable
      attrs['enabled'] = true
      save
    end

    def disable
      attrs['enabled'] = false
      save
    end

    def [](name)
      attrs[name]
    end

    def []=(name, value)
      attrs[name] = value
    end

    def save
      unless VALID_DIRECTIONS.include?(direction.to_sym)
        raise InvalidToxic, "Toxic direction must be one of: [#{VALID_DIRECTIONS.join(', ')}], got: #{direction}"
      end
      request = Net::HTTP::Post.new("/proxies/#{proxy.name}/#{direction}/toxics/#{name}")

      request.body = attrs.to_json

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

      @attrs = JSON.parse(response.body)

      self
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
toxiproxy-0.1.4 lib/toxiproxy/toxic.rb
toxiproxy-0.1.3 lib/toxiproxy/toxic.rb
toxiproxy-0.1.2 lib/toxiproxy/toxic.rb
toxiproxy-0.1.1 lib/toxiproxy/toxic.rb
toxiproxy-0.1.0 lib/toxiproxy/toxic.rb