Sha256: efeb7dc1b4345be376e6cc85e0122555e76191b04259c824df5942aaaa32fba0

Contents?: true

Size: 918 Bytes

Versions: 5

Compression:

Stored size: 918 Bytes

Contents

require 'ipaddr'
require 'forwardable'

module Middleman
  class PreviewServer
    class ServerIpAddress
      def self.new(ip_address)
        @parser = []
        @parser << ServerIpv6Address
        @parser << ServerIpv4Address

        @parser.find { |p| p.match? ip_address }.new(ip_address)
      end
    end

    class BasicServerIpAddress < SimpleDelegator
    end

    class ServerIpv4Address < BasicServerIpAddress
      def to_browser
        __getobj__.to_s
      end

      def self.match?(*)
        true
      end
    end

    class ServerIpv6Address < BasicServerIpAddress
      def to_s
        __getobj__.sub(/%.*$/, '')
      end

      def to_browser
        format('[%s]', to_s)
      end

      def self.match?(str)
        str = str.to_s.sub(/%.*$/, '')
        IPAddr.new(str).ipv6?
      rescue IPAddr::InvalidAddressError, IPAddr::AddressFamilyError
        false
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
middleman-core-4.5.1 lib/middleman-core/preview_server/server_ip_address.rb
middleman-core-4.5.0 lib/middleman-core/preview_server/server_ip_address.rb
middleman-core-4.4.3 lib/middleman-core/preview_server/server_ip_address.rb
middleman-core-4.4.2 lib/middleman-core/preview_server/server_ip_address.rb
middleman-core-4.4.0 lib/middleman-core/preview_server/server_ip_address.rb