File: email.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 (47 lines) | stat: -rw-r--r-- 1,175 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
37
38
39
40
41
42
43
44
45
46
47
require_relative './email/parser'

module KDL
  module Types
    class Email < Value
      attr_reader :local, :domain

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

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

        local, domain = Parser.new(value.value).parse

        new(value.value, type: type, local: local, domain: domain)
      end

    end
    MAPPING['email'] = Email

    class IDNEmail < Email
      attr_reader :unicode_domain

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

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

        local, domain, unicode_domain = Email::Parser.new(value.value, idn: true).parse

        new("#{local}@#{domain}", type: type, local: local, domain: domain, unicode_domain: unicode_domain)
      end

      def unicode_value
        "#{local}@#{unicode_domain}"
      end
    end
    MAPPING['idn-email'] = IDNEmail
  end
end