naughty_or_nice 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99393247edb23d92e4d1ddebb5a3a62b71c8179c
4
- data.tar.gz: afc4922b88c3789f860fc95a8fcf26037d83bd7b
3
+ metadata.gz: ffbdc7af85eed5f4b34c437590c4c0d38eed1dd6
4
+ data.tar.gz: 5285a0ae47a4908efb6d6582b02a78c02cbe27a2
5
5
  SHA512:
6
- metadata.gz: e9c88f8fc3407466c321177f0a6074b797aba84dcb5be52f1b78e5a124e7ed51b21560ea03333e9a58cedd0f5bf2a79e7b8b55a9382d916cee854d171a7bb15e
7
- data.tar.gz: 3f9cde45e96febf46407d91efa40c3e3484c01f337c781bc11f6720ee5482cefa041a4c5cbb6c22f184ba01a168b333b2cd0bc604fab5a8233c5aacdb0df3f12
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 #=> "foo.bar.com"
58
- address.domain_parts.tld #=> "com"
59
- address.domain_parts.sld #=> "bar"
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
@@ -1,3 +1,3 @@
1
1
  module NaughtyOrNice
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -56,45 +56,29 @@ module NaughtyOrNice
56
56
 
57
57
  def initialize(domain)
58
58
  if domain.is_a?(PublicSuffix::Domain)
59
- @domain_parts = domain
60
- @text = domain.to_s
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
- # Parse the domain from the input string
66
+ # Return the public suffix domain object
67
67
  #
68
- # Can handle urls, domains, or emails
68
+ # Supports all domain strings (URLs, emails)
69
69
  #
70
- # Returns the domain string
70
+ # Returns the domain object or nil, but no errors, never an error
71
71
  def domain
72
- @domain ||= begin
73
- return nil if @text.empty?
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
- !!(domain_parts && domain_parts.valid?)
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
- # Helper function to return the public suffix domain object
108
- #
109
- # Supports all domain strings (URLs, emails)
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: 1.0.0
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-06-30 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: public_suffix