domain_extractor 0.1.8 → 0.1.9
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 +26 -0
- data/lib/domain_extractor/version.rb +1 -1
- data/lib/domain_extractor.rb +12 -1
- data/spec/domain_extractor_spec.rb +26 -24
- data/spec/parsed_url_spec.rb +49 -6
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2b919b52fe3fda7edf9738141e66e2acbcb2458d276800cb62b48c4c34d3b914
|
|
4
|
+
data.tar.gz: be02cf195da3a6a9da51136140f1487e96c026b22f1a300825d15733eb493acf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 981d228483ba55b85834c38df3146a47018be6b7456053add7356a74f15d33cd27038d50682e5488f14ff9dfd09ef463bee1bbd6f7c486d9e9698dd305543738
|
|
7
|
+
data.tar.gz: e320e6d216196664de8f73b18be95746863b60fba99e496dc166ebd2f051afdf475d56f7d7a30dc6910e66d380e5b03b7d472730698905ed5b83e87aba3eb4e5
|
data/README.md
CHANGED
|
@@ -65,6 +65,10 @@ end
|
|
|
65
65
|
result.subdomain # => 'www'
|
|
66
66
|
result.domain # => 'example'
|
|
67
67
|
result.host # => 'www.example.co.uk'
|
|
68
|
+
|
|
69
|
+
# Opt into strict parsing when needed
|
|
70
|
+
DomainExtractor.parse!('notaurl')
|
|
71
|
+
# => raises DomainExtractor::InvalidURLError: Invalid URL Value
|
|
68
72
|
```
|
|
69
73
|
|
|
70
74
|
## ParsedURL API - Intuitive Method Access
|
|
@@ -150,6 +154,28 @@ DomainExtractor.parse('https://api.dashtrack.com').subdomain? # => true
|
|
|
150
154
|
# Check for www subdomain specifically
|
|
151
155
|
DomainExtractor.parse('https://www.dashtrack.com').www_subdomain? # => true
|
|
152
156
|
DomainExtractor.parse('https://api.dashtrack.com').www_subdomain? # => false
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### Handling Unknown or Invalid Data
|
|
161
|
+
|
|
162
|
+
```ruby
|
|
163
|
+
# Default accessors fail silently with nil
|
|
164
|
+
DomainExtractor.parse(nil).domain # => nil
|
|
165
|
+
DomainExtractor.parse('').host # => nil
|
|
166
|
+
DomainExtractor.parse('asdfasdfds').domain # => nil
|
|
167
|
+
|
|
168
|
+
# Boolean checks never raise
|
|
169
|
+
DomainExtractor.parse(nil).subdomain? # => false
|
|
170
|
+
DomainExtractor.parse('').domain? # => false
|
|
171
|
+
DomainExtractor.parse('https://dashtrack.com').subdomain? # => false
|
|
172
|
+
|
|
173
|
+
# Bang methods raise when a component is missing
|
|
174
|
+
DomainExtractor.parse('').host! # => raises DomainExtractor::InvalidURLError
|
|
175
|
+
DomainExtractor.parse('asdfasdfds').domain! # => raises DomainExtractor::InvalidURLError
|
|
176
|
+
|
|
177
|
+
# Strict parsing helper mirrors legacy behaviour
|
|
178
|
+
DomainExtractor.parse!('asdfasdfds') # => raises DomainExtractor::InvalidURLError
|
|
153
179
|
```
|
|
154
180
|
|
|
155
181
|
#### Safe Batch Processing
|
data/lib/domain_extractor.rb
CHANGED
|
@@ -15,10 +15,21 @@ module DomainExtractor
|
|
|
15
15
|
class << self
|
|
16
16
|
# Parse an individual URL and extract domain attributes.
|
|
17
17
|
# Returns a ParsedURL object that supports hash-style access and method calls.
|
|
18
|
-
#
|
|
18
|
+
# For invalid inputs the returned ParsedURL will be marked invalid and all
|
|
19
|
+
# accessors (without bang) will evaluate to nil/false.
|
|
19
20
|
# @param url [String, #to_s]
|
|
20
21
|
# @return [ParsedURL]
|
|
21
22
|
def parse(url)
|
|
23
|
+
Parser.call(url)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Parse an individual URL and raise when extraction fails.
|
|
27
|
+
# This mirrors the legacy behaviour of .parse while giving callers an
|
|
28
|
+
# explicit opt-in to strict validation.
|
|
29
|
+
# @param url [String, #to_s]
|
|
30
|
+
# @return [ParsedURL]
|
|
31
|
+
# @raise [InvalidURLError]
|
|
32
|
+
def parse!(url)
|
|
22
33
|
result = Parser.call(url)
|
|
23
34
|
raise InvalidURLError unless result.valid?
|
|
24
35
|
|
|
@@ -142,40 +142,42 @@ RSpec.describe DomainExtractor do
|
|
|
142
142
|
end
|
|
143
143
|
|
|
144
144
|
context 'with invalid URLs' do
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
145
|
+
let(:invalid_inputs) { ['http://', 'not_a_url', '192.168.1.1', '[2001:db8::1]', '', nil] }
|
|
146
|
+
|
|
147
|
+
it 'returns an invalid ParsedURL that safely yields nil values' do
|
|
148
|
+
invalid_inputs.each do |input|
|
|
149
|
+
result = described_class.parse(input)
|
|
150
|
+
|
|
151
|
+
expect(result).to be_a(DomainExtractor::ParsedURL)
|
|
152
|
+
expect(result.valid?).to be(false)
|
|
153
|
+
expect(result.domain).to be_nil
|
|
154
|
+
expect(result.domain?).to be(false)
|
|
155
|
+
expect(result.host).to be_nil
|
|
156
|
+
expect(result.host?).to be(false)
|
|
157
|
+
end
|
|
150
158
|
end
|
|
151
159
|
|
|
152
|
-
it '
|
|
153
|
-
|
|
154
|
-
DomainExtractor::InvalidURLError,
|
|
155
|
-
'Invalid URL Value'
|
|
156
|
-
)
|
|
157
|
-
end
|
|
160
|
+
it 'allows bang accessors to raise explicit errors' do
|
|
161
|
+
result = described_class.parse('not_a_url')
|
|
158
162
|
|
|
159
|
-
|
|
160
|
-
expect { described_class.parse('192.168.1.1') }.to raise_error(
|
|
163
|
+
expect { result.domain! }.to raise_error(
|
|
161
164
|
DomainExtractor::InvalidURLError,
|
|
162
|
-
'
|
|
165
|
+
'domain not found or invalid'
|
|
163
166
|
)
|
|
164
|
-
end
|
|
165
167
|
|
|
166
|
-
|
|
167
|
-
expect { described_class.parse('[2001:db8::1]') }.to raise_error(
|
|
168
|
+
expect { result.host! }.to raise_error(
|
|
168
169
|
DomainExtractor::InvalidURLError,
|
|
169
|
-
'
|
|
170
|
+
'host not found or invalid'
|
|
170
171
|
)
|
|
171
172
|
end
|
|
172
173
|
|
|
173
|
-
it '
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
174
|
+
it 'provides strict parsing via parse!' do
|
|
175
|
+
invalid_inputs.each do |input|
|
|
176
|
+
expect { described_class.parse!(input) }.to raise_error(
|
|
177
|
+
DomainExtractor::InvalidURLError,
|
|
178
|
+
'Invalid URL Value'
|
|
179
|
+
)
|
|
180
|
+
end
|
|
179
181
|
end
|
|
180
182
|
end
|
|
181
183
|
end
|
data/spec/parsed_url_spec.rb
CHANGED
|
@@ -127,8 +127,8 @@ RSpec.describe DomainExtractor::ParsedURL do
|
|
|
127
127
|
end
|
|
128
128
|
end
|
|
129
129
|
|
|
130
|
-
context 'with invalid URL' do
|
|
131
|
-
let(:parsed) { DomainExtractor
|
|
130
|
+
context 'with invalid URL input' do
|
|
131
|
+
let(:parsed) { DomainExtractor.parse('invalid_url_value') }
|
|
132
132
|
|
|
133
133
|
describe 'default accessor methods' do
|
|
134
134
|
it 'returns nil for subdomain' do
|
|
@@ -189,6 +189,50 @@ RSpec.describe DomainExtractor::ParsedURL do
|
|
|
189
189
|
end
|
|
190
190
|
end
|
|
191
191
|
end
|
|
192
|
+
|
|
193
|
+
context 'with nil input' do
|
|
194
|
+
let(:parsed) { DomainExtractor.parse(nil) }
|
|
195
|
+
|
|
196
|
+
it 'returns nil for default accessors' do
|
|
197
|
+
expect(parsed.domain).to be_nil
|
|
198
|
+
expect(parsed.host).to be_nil
|
|
199
|
+
expect(parsed.subdomain).to be_nil
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
it 'returns false for question accessors' do
|
|
203
|
+
expect(parsed.domain?).to be false
|
|
204
|
+
expect(parsed.host?).to be false
|
|
205
|
+
expect(parsed.subdomain?).to be false
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it 'raises for bang accessors' do
|
|
209
|
+
expect { parsed.domain! }.to raise_error(
|
|
210
|
+
DomainExtractor::InvalidURLError,
|
|
211
|
+
'domain not found or invalid'
|
|
212
|
+
)
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
context 'with empty string input' do
|
|
217
|
+
let(:parsed) { DomainExtractor.parse('') }
|
|
218
|
+
|
|
219
|
+
it 'returns nil for default accessors' do
|
|
220
|
+
expect(parsed.domain).to be_nil
|
|
221
|
+
expect(parsed.host).to be_nil
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
it 'returns false for question accessors' do
|
|
225
|
+
expect(parsed.domain?).to be false
|
|
226
|
+
expect(parsed.host?).to be false
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
it 'raises for bang accessors' do
|
|
230
|
+
expect { parsed.host! }.to raise_error(
|
|
231
|
+
DomainExtractor::InvalidURLError,
|
|
232
|
+
'host not found or invalid'
|
|
233
|
+
)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
192
236
|
end
|
|
193
237
|
|
|
194
238
|
describe '#www_subdomain?' do
|
|
@@ -208,7 +252,7 @@ RSpec.describe DomainExtractor::ParsedURL do
|
|
|
208
252
|
end
|
|
209
253
|
|
|
210
254
|
it 'returns false for invalid URL' do
|
|
211
|
-
parsed = DomainExtractor
|
|
255
|
+
parsed = DomainExtractor.parse('invalid_url_value')
|
|
212
256
|
expect(parsed.www_subdomain?).to be false
|
|
213
257
|
end
|
|
214
258
|
end
|
|
@@ -220,7 +264,7 @@ RSpec.describe DomainExtractor::ParsedURL do
|
|
|
220
264
|
end
|
|
221
265
|
|
|
222
266
|
it 'returns false for invalid URL' do
|
|
223
|
-
parsed = DomainExtractor
|
|
267
|
+
parsed = DomainExtractor.parse('invalid_url_value')
|
|
224
268
|
expect(parsed.valid?).to be false
|
|
225
269
|
end
|
|
226
270
|
|
|
@@ -299,8 +343,7 @@ RSpec.describe DomainExtractor::ParsedURL do
|
|
|
299
343
|
|
|
300
344
|
it 'handles example: domain returns nil for invalid URL' do
|
|
301
345
|
# Parser returns ParsedURL with empty result for invalid URLs
|
|
302
|
-
|
|
303
|
-
parsed = DomainExtractor::ParsedURL.new(nil)
|
|
346
|
+
parsed = DomainExtractor.parse('invalid_url_value')
|
|
304
347
|
expect(parsed.domain).to be_nil
|
|
305
348
|
end
|
|
306
349
|
end
|