domain_validator_jp 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 703d0df0dce33e429dbf606d1ca23e6053e5251331260fe6fe0d392a7d96b299
4
- data.tar.gz: aa08dd03b420dea21484b5afebec87584a902534873c42df7d0f56bf45ca979f
3
+ metadata.gz: b994793dd271e71492fba726ebb405ef583f8b951f58806659b5528dcfdbd6b2
4
+ data.tar.gz: 2c657d7f2d250be49b418b6f1790292180670db5e15bdf0f6f2c922acd079b9d
5
5
  SHA512:
6
- metadata.gz: de4938c681c48a99ae0224b53865d0bf2faff7fdf796033dd446e223f0a6cd3c783d9fea37db3926b343c2c52780c96679c3eb38793e6397f1651c0aad64153a
7
- data.tar.gz: e219d06afef340342b61002e1c810b01ae44a4e19117e6a7e9e4dd8fa0ecdb15566b1e593934020ce457443da5bf57401f92caadd8dfb0875b0b7c29a6be63f8
6
+ metadata.gz: 6c03e1fd78be99e56a69e5a5df3e9a2b816807950637585dfc185736ed32d3d8c087029ad3d6a1f117afcf0831690ca22ef8dfbce1fa22e1524babe8a4c12a0a
7
+ data.tar.gz: 82e6f0df0cc35140adcbc278397dd00f28e2bd18dfc0df232665823e6b4e7bf5a9702e55cec6436fb8637690bac84d5578df5aa84f65e6f81c709f46d3cfcaf8
data/README.md CHANGED
@@ -9,11 +9,10 @@ domain name validator includes JP domain
9
9
  ## Usage
10
10
 
11
11
  ```rb
12
- DomainValidatorJp.new('example.jp').valid? # => true
13
- DomainValidatorJp.new('日本語.jp').valid? # => true
14
-
15
- DomainValidatorJp.new('①②③.jp').valid? # => false
16
- DomainValidatorJp.new('example.wrongtld').valid? # => false
12
+ DomainValidatorJp.valid?('example.jp') # => true
13
+ DomainValidatorJp.valid?('日本語.jp') # => true
14
+ DomainValidatorJp.valid?('①②③.jp') # => false
15
+ DomainValidatorJp.valid?('example.wrongtld') # => false
17
16
  ```
18
17
 
19
18
  ## Installation
@@ -1,102 +1,90 @@
1
1
  require "domain_validator_jp/version"
2
2
  require "public_suffix"
3
3
 
4
- class DomainValidatorJp
5
- attr_reader :domain
6
-
7
- def initialize(domain)
8
- @domain = domain
9
- end
10
-
11
- def valid?
12
- return false unless fqdn?
13
- return false if include_subdomain?
14
- return false unless valid_public_suffix?
15
- return false unless valid_sld_not_dotted?
16
- return false unless valid_sld_not_start_with_hyphen?
17
- return false unless valid_sld_not_end_with_hyphen?
18
-
19
- if sld_multibyte?
20
- return false unless valid_length_sld_multibyte?
21
- return false unless valid_jisx0208?
22
- else
23
- return false unless valid_sld_chars?
24
- return false unless valid_length_domain?
25
- return false unless valid_length_sld?
4
+ module DomainValidatorJp
5
+ class << self
6
+ def valid?(domain)
7
+ return false unless fqdn?(domain)
8
+ return false if include_subdomain?(domain)
9
+ return false unless valid_public_suffix?(domain)
10
+ return false unless valid_sld_not_dotted?(domain)
11
+ return false unless valid_sld_not_start_with_hyphen?(domain)
12
+ return false unless valid_sld_not_end_with_hyphen?(domain)
13
+
14
+ if sld_multibyte?(domain)
15
+ return false unless valid_length_sld_multibyte?(domain)
16
+ return false unless valid_jisx0208?(domain)
17
+ else
18
+ return false unless valid_sld_chars?(domain)
19
+ return false unless valid_length_domain?(domain)
20
+ return false unless valid_length_sld?(domain)
21
+ end
22
+
23
+ true
26
24
  end
27
25
 
28
- true
29
- end
30
-
31
- def fqdn?
32
- splitted = domain.split('.')
33
- splitted.length >= 2 && splitted.all?{|label| !label.empty? }
34
- end
35
-
36
- def parsed
37
- @parsed ||= PublicSuffix.parse(domain)
38
- end
39
-
40
- def sld
41
- parsed.sld
42
- end
26
+ def fqdn?(domain)
27
+ splitted = domain.split('.')
28
+ splitted.length >= 2 && splitted.all?{|label| !label.empty? }
29
+ end
43
30
 
44
- def tld
45
- parsed.tld
46
- end
31
+ def parsed(domain)
32
+ PublicSuffix.parse(domain)
33
+ end
47
34
 
48
- # common
35
+ # common
49
36
 
50
- def include_subdomain?
51
- !parsed.subdomain.nil?
52
- end
37
+ def include_subdomain?(domain)
38
+ !parsed(domain).subdomain.nil?
39
+ end
53
40
 
54
- def valid_public_suffix?
55
- PublicSuffix.valid?(domain, default_rule: nil, ignore_private: true)
56
- end
41
+ def valid_public_suffix?(domain)
42
+ PublicSuffix.valid?(domain, default_rule: nil, ignore_private: true)
43
+ end
57
44
 
58
- def valid_sld_not_dotted?
59
- sld !~ /\./
60
- end
45
+ def valid_sld_not_dotted?(domain)
46
+ parsed(domain).sld !~ /\./
47
+ end
61
48
 
62
- def valid_sld_not_start_with_hyphen?
63
- sld !~ /^\-/
64
- end
49
+ def valid_sld_not_start_with_hyphen?(domain)
50
+ parsed(domain).sld !~ /^\-/
51
+ end
65
52
 
66
- def valid_sld_not_end_with_hyphen?
67
- sld !~ /\-$/
68
- end
53
+ def valid_sld_not_end_with_hyphen?(domain)
54
+ parsed(domain).sld !~ /\-$/
55
+ end
69
56
 
70
- # only multibyte
57
+ # only multibyte
71
58
 
72
- def sld_multibyte?
73
- sld !~ /^[0-9A-Za-z\-]+$/
74
- end
59
+ def sld_multibyte?(domain)
60
+ parsed(domain).sld !~ /^[0-9A-Za-z\-]+$/
61
+ end
75
62
 
76
- def valid_length_sld_multibyte?
77
- 1 <= sld.length && sld.length <= 15
78
- end
63
+ def valid_length_sld_multibyte?(domain)
64
+ 1 <= parsed(domain).sld.length && parsed(domain).sld.length <= 15
65
+ end
79
66
 
80
- # only multibyte
67
+ # only multibyte
81
68
 
82
- def valid_sld_chars?
83
- sld =~ /^[0-9A-Za-z\-]+$/
84
- end
69
+ def valid_sld_chars?(domain)
70
+ parsed(domain).sld =~ /^[0-9A-Za-z\-]+$/
71
+ end
85
72
 
86
- def valid_length_domain?
87
- domain.length <= 253
88
- end
73
+ def valid_length_domain?(domain)
74
+ domain.length <= 253
75
+ end
89
76
 
90
- def valid_length_sld?
91
- sld.length <= 63
92
- end
77
+ def valid_length_sld?(domain)
78
+ parsed(domain).sld.length <= 63
79
+ end
93
80
 
94
- def valid_jisx0208?
95
- valid = /^#{utf8_to_sjis("[\-0-9A-Za-zぁ-んァ-ヶ亜-腕弌-熙]")}+$/
96
- valid.match(utf8_to_sjis(sld)) if utf8_to_sjis(sld)
97
- end
81
+ def valid_jisx0208?(domain)
82
+ valid = /^#{utf8_to_sjis("[\-0-9A-Za-zぁ-んァ-ヶ亜-腕弌-熙]")}+$/
83
+ valid.match(utf8_to_sjis(parsed(domain).sld)) if utf8_to_sjis(parsed(domain).sld)
84
+ end
98
85
 
99
- def utf8_to_sjis(str)
100
- str.encode('Windows-31J') rescue nil
86
+ def utf8_to_sjis(str)
87
+ str.encode('Windows-31J') rescue nil
88
+ end
101
89
  end
102
90
  end
@@ -1,3 +1,3 @@
1
- class DomainValidatorJp
2
- VERSION = "0.1.4"
1
+ module DomainValidatorJp
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domain_validator_jp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kimromi