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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4bc4d6ad831692d1251048f8b21820bb0efb10ed5b3cce641441b31afb5308b4
4
- data.tar.gz: 67a96b33dc3544847af271c8bd837dbc592031bff5dac126022a147c2281460c
3
+ metadata.gz: 2b919b52fe3fda7edf9738141e66e2acbcb2458d276800cb62b48c4c34d3b914
4
+ data.tar.gz: be02cf195da3a6a9da51136140f1487e96c026b22f1a300825d15733eb493acf
5
5
  SHA512:
6
- metadata.gz: 02bca764446a3391461695cfeeaef9c6e7920308bc78768b062ae676005d3610b09733133cb10c34cd5e29dc35169f770b4789f418fd554cba762a6d5a19022a
7
- data.tar.gz: eeaaa8356b306feba33e08e54c8da2926f7e052ebac5d6920f0a6f26c0dacd3bfbb0d4f863fa694377d87441564a2c1eecff764d756ce4efde0569fabf573ee2
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DomainExtractor
4
- VERSION = '0.1.8'
4
+ VERSION = '0.1.9'
5
5
  end
@@ -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
- # Raises DomainExtractor::InvalidURLError when the URL fails validation.
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
- it 'raises InvalidURLError for malformed URLs' do
146
- expect { described_class.parse('http://') }.to raise_error(
147
- DomainExtractor::InvalidURLError,
148
- 'Invalid URL Value'
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 'raises InvalidURLError for invalid domains' do
153
- expect { described_class.parse('not_a_url') }.to raise_error(
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
- it 'raises InvalidURLError for IP addresses' do
160
- expect { described_class.parse('192.168.1.1') }.to raise_error(
163
+ expect { result.domain! }.to raise_error(
161
164
  DomainExtractor::InvalidURLError,
162
- 'Invalid URL Value'
165
+ 'domain not found or invalid'
163
166
  )
164
- end
165
167
 
166
- it 'raises InvalidURLError for IPv6 addresses' do
167
- expect { described_class.parse('[2001:db8::1]') }.to raise_error(
168
+ expect { result.host! }.to raise_error(
168
169
  DomainExtractor::InvalidURLError,
169
- 'Invalid URL Value'
170
+ 'host not found or invalid'
170
171
  )
171
172
  end
172
173
 
173
- it 'raises InvalidURLError for empty string' do
174
- expect { described_class.parse('') }.to raise_error(DomainExtractor::InvalidURLError, 'Invalid URL Value')
175
- end
176
-
177
- it 'raises InvalidURLError for nil' do
178
- expect { described_class.parse(nil) }.to raise_error(DomainExtractor::InvalidURLError, 'Invalid URL Value')
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
@@ -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::ParsedURL.new(nil) }
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::ParsedURL.new(nil)
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::ParsedURL.new(nil)
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
- # But parse() raises error, so we need to construct directly
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domain_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenSite AI