naughty_or_nice 1.0.0 → 2.0.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 +4 -4
- data/README.md +17 -4
- data/lib/naughty_or_nice/version.rb +1 -1
- data/lib/naughty_or_nice.rb +38 -36
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ffbdc7af85eed5f4b34c437590c4c0d38eed1dd6
|
4
|
+
data.tar.gz: 5285a0ae47a4908efb6d6582b02a78c02cbe27a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a4f84828325d89c6fc187be7c489ea31f00a88ef4529aa03b3874a2acf1faad97650650c75821d145da0c5ad34dbd62f227a1922e08ed5ae3c5128eb703a172
|
7
|
+
data.tar.gz: 312f9b00bbeb629521f470ce4e7f5adaa1f6297794113b73424e1d6fbad2bc8c9be473d38c83958416facab22bf00b36206147c89bf2eb8ad149de3e9d31ff38
|
data/README.md
CHANGED
@@ -21,13 +21,26 @@ class Checker
|
|
21
21
|
DOMAINS = %w[foo.com bar.com foobar.com]
|
22
22
|
|
23
23
|
def valid?
|
24
|
-
DOMAINS.include? domain
|
24
|
+
DOMAINS.include? domain.to_s
|
25
25
|
end
|
26
26
|
end
|
27
27
|
```
|
28
28
|
|
29
29
|
That's it! Just overwrite the `valid?` method and Naughty or Nice takes care of the rest.
|
30
30
|
|
31
|
+
You can also get more complicated. Let's say you only wanted to allow `.gov` domains:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Checker
|
35
|
+
|
36
|
+
include NaughtyOrNice
|
37
|
+
|
38
|
+
def valid?
|
39
|
+
domain.tld == ".gov"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
31
44
|
## Using the included methods
|
32
45
|
|
33
46
|
There are a handful of magic methods that your class automatically gets. You can throw any domain-like string at your new `Checker` class, and figure out if it's on the list. Here's a few examples:
|
@@ -54,9 +67,9 @@ You can also you NaughtyOrNice to extract domain information for use elsewhere.
|
|
54
67
|
```ruby
|
55
68
|
address = Checker.new "baz@foo.bar.com"
|
56
69
|
address.valid? #=> true
|
57
|
-
address.domain
|
58
|
-
address.
|
59
|
-
address.
|
70
|
+
address.domain.to_s #=> "foo.bar.com"
|
71
|
+
address.domain.tld #=> "com"
|
72
|
+
address.domain.sld #=> "bar"
|
60
73
|
```
|
61
74
|
|
62
75
|
## See it in action
|
data/lib/naughty_or_nice.rb
CHANGED
@@ -56,45 +56,29 @@ module NaughtyOrNice
|
|
56
56
|
|
57
57
|
def initialize(domain)
|
58
58
|
if domain.is_a?(PublicSuffix::Domain)
|
59
|
-
@
|
60
|
-
@text
|
59
|
+
@domain = domain
|
60
|
+
@text = domain.to_s
|
61
61
|
else
|
62
62
|
@text = domain.to_s.downcase.strip
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
#
|
66
|
+
# Return the public suffix domain object
|
67
67
|
#
|
68
|
-
#
|
68
|
+
# Supports all domain strings (URLs, emails)
|
69
69
|
#
|
70
|
-
# Returns the domain
|
70
|
+
# Returns the domain object or nil, but no errors, never an error
|
71
71
|
def domain
|
72
|
-
@domain ||=
|
73
|
-
|
74
|
-
|
75
|
-
uri = Addressable::URI.parse(@text)
|
76
|
-
|
77
|
-
if uri.host # valid https?://* URI
|
78
|
-
uri.host
|
79
|
-
elsif email?
|
80
|
-
@text.match(/@([\w\.\-]+)\Z/i)[1]
|
81
|
-
else # url sans http://
|
82
|
-
uri = Addressable::URI.parse("http://#{@text}")
|
83
|
-
# properly parse http://foo edge cases
|
84
|
-
# see https://github.com/sporkmonger/addressable/issues/145
|
85
|
-
uri.host if uri.host =~ /\./
|
86
|
-
end
|
87
|
-
rescue Addressable::URI::InvalidURIError
|
88
|
-
nil
|
89
|
-
end
|
72
|
+
@domain ||= PublicSuffix.parse(domain_text)
|
73
|
+
rescue PublicSuffix::DomainInvalid, PublicSuffix::DomainNotAllowed
|
74
|
+
nil
|
90
75
|
end
|
91
|
-
alias_method :to_s, :domain
|
92
76
|
|
93
77
|
# Checks if the input string represents a valid domain
|
94
78
|
#
|
95
79
|
# Returns boolean true if a valid domain, otherwise false
|
96
80
|
def valid?
|
97
|
-
!!(
|
81
|
+
!!(domain && domain.valid?)
|
98
82
|
end
|
99
83
|
|
100
84
|
# Is the input text in the form of a valid email address?
|
@@ -104,20 +88,38 @@ module NaughtyOrNice
|
|
104
88
|
!!(@text =~ EMAIL_REGEX)
|
105
89
|
end
|
106
90
|
|
107
|
-
#
|
108
|
-
|
109
|
-
|
110
|
-
#
|
111
|
-
# Returns the domain object or nil, but no errors, never an error
|
112
|
-
def domain_parts
|
113
|
-
@domain_parts ||= begin
|
114
|
-
PublicSuffix.parse domain
|
115
|
-
rescue PublicSuffix::DomainInvalid, PublicSuffix::DomainNotAllowed
|
116
|
-
nil
|
117
|
-
end
|
91
|
+
# Return the parsed domain as a string
|
92
|
+
def to_s
|
93
|
+
domain.to_s if domain
|
118
94
|
end
|
119
95
|
|
120
96
|
def inspect
|
121
97
|
"#<#{self.class} domain=\"#{domain}\" valid=#{valid?}>"
|
122
98
|
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
# Parse the domain from the input string
|
103
|
+
#
|
104
|
+
# Can handle urls, domains, or emails
|
105
|
+
#
|
106
|
+
# Returns the domain string
|
107
|
+
def domain_text
|
108
|
+
return nil if @text.empty?
|
109
|
+
|
110
|
+
uri = Addressable::URI.parse(@text)
|
111
|
+
|
112
|
+
if uri.host # valid https?://* URI
|
113
|
+
uri.host
|
114
|
+
elsif email?
|
115
|
+
@text.match(/@([\w\.\-]+)\Z/i)[1]
|
116
|
+
else # url sans http://
|
117
|
+
uri = Addressable::URI.parse("http://#{@text}")
|
118
|
+
# properly parse http://foo edge cases
|
119
|
+
# see https://github.com/sporkmonger/addressable/issues/145
|
120
|
+
uri.host if uri.host =~ /\./
|
121
|
+
end
|
122
|
+
rescue Addressable::URI::InvalidURIError
|
123
|
+
nil
|
124
|
+
end
|
123
125
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: naughty_or_nice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Balter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: public_suffix
|