Class: DomainExtractor::ParsedURL
- Inherits:
-
Object
- Object
- DomainExtractor::ParsedURL
- Defined in:
- lib/domain_extractor/parsed_url.rb
Overview
ParsedURL wraps the parsing result and provides convenient accessor methods with support for bang (!) and question mark (?) variants.
Examples:
parsed = DomainExtractor.parse('https://api.example.com')
parsed.host # => 'api.example.com'
parsed.subdomain # => 'api'
parsed.subdomain? # => true
parsed.www_subdomain? # => false
parsed = DomainExtractor.parse('invalid')
parsed.host # => nil
parsed.host? # => false
parsed.host! # raises InvalidURLError
Constant Summary collapse
- RESULT_KEYS =
List of valid result keys that should have method accessors
i[subdomain domain tld root_domain host path query_params].freeze
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Expose the underlying hash for backward compatibility.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(result) ⇒ ParsedURL
constructor
A new instance of ParsedURL.
-
#inspect ⇒ Object
Provide hash-like inspection.
-
#method_missing(method_name, *args) ⇒ Object
Dynamically handle method calls for all result keys Supports three variants: - method_name: returns value or nil - method_name!: returns value or raises InvalidURLError - method_name?: returns boolean (true if value exists and not nil/empty).
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#to_h ⇒ Object
(also: #to_hash)
Allow to_h conversion for hash compatibility.
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Check if the parsed result is valid (not nil/empty).
-
#www_subdomain? ⇒ Boolean
Special helper: check if subdomain is specifically ‘www’.
Constructor Details
#initialize(result) ⇒ ParsedURL
25 26 27 28 |
# File 'lib/domain_extractor/parsed_url.rb', line 25 def initialize(result) @result = result || {} freeze end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Dynamically handle method calls for all result keys Supports three variants:
-
method_name: returns value or nil
-
method_name!: returns value or raises InvalidURLError
-
method_name?: returns boolean (true if value exists and not nil/empty)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/domain_extractor/parsed_url.rb', line 51 def method_missing(method_name, *args, &) method_str = method_name.to_s # Handle bang methods (method_name!) return handle_bang_method(method_str) if method_str.end_with?('!') # Handle question mark methods (method_name?) return handle_question_method(method_str) if method_str.end_with?('?') # Handle regular methods (method_name) key = method_name.to_sym return @result[key] if RESULT_KEYS.include?(key) super end |
Instance Attribute Details
#result ⇒ Object (readonly)
Expose the underlying hash for backward compatibility
20 21 22 |
# File 'lib/domain_extractor/parsed_url.rb', line 20 def result @result end |
Instance Method Details
#[](key) ⇒ Object
32 33 34 |
# File 'lib/domain_extractor/parsed_url.rb', line 32 def [](key) @result[key] end |
#inspect ⇒ Object
Provide hash-like inspection
86 87 88 |
# File 'lib/domain_extractor/parsed_url.rb', line 86 def inspect "#<DomainExtractor::ParsedURL #{@result.inspect}>" end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/domain_extractor/parsed_url.rb', line 67 def respond_to_missing?(method_name, include_private = false) method_str = method_name.to_s # Check for www_subdomain? special case return true if method_name == :www_subdomain? # Check if it's a bang or question mark variant if method_str.end_with?('!') || method_str.end_with?('?') key = method_str[0...-1].to_sym return true if RESULT_KEYS.include?(key) end # Check if it's a regular method return true if RESULT_KEYS.include?(method_name.to_sym) super end |
#to_h ⇒ Object Also known as: to_hash
Allow to_h conversion for hash compatibility
95 96 97 |
# File 'lib/domain_extractor/parsed_url.rb', line 95 def to_h @result.dup end |
#to_s ⇒ Object
90 91 92 |
# File 'lib/domain_extractor/parsed_url.rb', line 90 def to_s @result.to_s end |
#valid? ⇒ Boolean
Check if the parsed result is valid (not nil/empty)
37 38 39 |
# File 'lib/domain_extractor/parsed_url.rb', line 37 def valid? !@result.empty? end |
#www_subdomain? ⇒ Boolean
Special helper: check if subdomain is specifically ‘www’
42 43 44 |
# File 'lib/domain_extractor/parsed_url.rb', line 42 def www_subdomain? @result[:subdomain] == 'www' end |