File: hostname.rb

package info (click to toggle)
ruby-kdl 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ruby: 6,667; yacc: 72; sh: 5; makefile: 4
file content (36 lines) | stat: -rw-r--r-- 1,006 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require_relative './hostname/validator'

module KDL
  module Types
    class Hostname < Value
      def self.call(value, type = 'hostname')
        return nil unless value.is_a? ::KDL::Value::String

        validator = Validator.new(value.value)
        raise ArgumentError, "invalid hostname #{value}" unless validator.valid?

        new(value.value, type: type)
      end
    end
    MAPPING['hostname'] = Hostname

    class IDNHostname < Hostname
      attr_reader :unicode_value

      def initialize(value, unicode_value:, **kwargs)
        super(value, **kwargs)
        @unicode_value = unicode_value
      end

      def self.call(value, type = 'idn-hostname')
        return nil unless value.is_a? ::KDL::Value::String

        validator = Validator.new(value.value)
        raise ArgumentError, "invalid hostname #{value}" unless validator.valid?

        new(validator.ascii, type: type, unicode_value: validator.unicode)
      end
    end
    MAPPING['idn-hostname'] = IDNHostname
  end
end