Sha256: 99a410301641ef94393a2e51ce497b09d19474ba53aae48652437f5fee0db20c

Contents?: true

Size: 892 Bytes

Versions: 17

Compression:

Stored size: 892 Bytes

Contents

module Unleash
  module Strategy
    class RemoteAddress < Base
      PARAM = 'IPs'.freeze

      def name
        'remoteAddress'
      end

      # need: params['IPs'], context.remote_address
      def is_enabled?(params = {}, context = nil)
        return false unless params.is_a?(Hash) && params.has_key?(PARAM)
        return false unless params.fetch(PARAM, nil).is_a? String
        return false unless context.instance_of?(Unleash::Context)

        remote_address = ipaddr_or_nil_from_str(context.remote_address)

        params[PARAM]
          .split(',')
          .map(&:strip)
          .map{ |ipblock| ipaddr_or_nil_from_str(ipblock) }
          .compact
          .map{ |ipb| ipb.include? remote_address }
          .any?
      end

      private

      def ipaddr_or_nil_from_str(ip)
        IPAddr.new(ip)
      rescue StandardError
        nil
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
unleash-5.1.1 lib/unleash/strategy/remote_address.rb
unleash-5.1.0 lib/unleash/strategy/remote_address.rb
unleash-5.0.7 lib/unleash/strategy/remote_address.rb
unleash-5.0.6 lib/unleash/strategy/remote_address.rb
unleash-5.0.5 lib/unleash/strategy/remote_address.rb
unleash-5.0.4 lib/unleash/strategy/remote_address.rb
unleash-5.0.3 lib/unleash/strategy/remote_address.rb
unleash-5.0.2 lib/unleash/strategy/remote_address.rb
unleash-5.0.1 lib/unleash/strategy/remote_address.rb
unleash-5.0.0 lib/unleash/strategy/remote_address.rb
unleash-4.6.0 lib/unleash/strategy/remote_address.rb
unleash-4.5.0 lib/unleash/strategy/remote_address.rb
unleash-4.4.4 lib/unleash/strategy/remote_address.rb
unleash-4.4.3 lib/unleash/strategy/remote_address.rb
unleash-4.4.2 lib/unleash/strategy/remote_address.rb
unleash-4.4.1 lib/unleash/strategy/remote_address.rb
unleash-4.4.0 lib/unleash/strategy/remote_address.rb