Sha256: 80c434d79461907cd0cd69eb1d85aaa04f223044554bb6cd45e6dbfd42d290ed

Contents?: true

Size: 1004 Bytes

Versions: 1

Compression:

Stored size: 1004 Bytes

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, 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

1 entries across 1 versions & 1 rubygems

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