Class: UrlParser::URI
- Inherits:
-
Object
- Object
- UrlParser::URI
- Extended by:
- Forwardable
- Defined in:
- lib/url_parser/uri.rb
Constant Summary collapse
- LOCALHOST_REGEXP =
/(\A|\.)localhost\z/
- COMPONENTS =
[ :scheme, # Top level URI naming structure / protocol. :username, # Username portion of the userinfo. :user, # Alias for #username. :password, # Password portion of the userinfo. :userinfo, # URI username and password for authentication. :hostname, # Fully qualified domain name or IP address. :naked_hostname, # Hostname without any ww? prefix. :port, # Port number. :host, # Hostname and port. :www, # The ww? portion of the subdomain. :tld, # Returns the top level domain portion, aka the extension. :top_level_domain, # Alias for #tld. :extension, # Alias for #tld. :sld, # Returns the second level domain portion, aka the domain part. :second_level_domain, # Alias for #sld. :domain_name, # Alias for #sld. :trd, # Returns the third level domain portion, aka the subdomain part. :third_level_domain, # Alias for #trd. :subdomains, # Alias for #trd. :naked_trd, # Any non-ww? subdomains. :naked_subdomain, # Alias for #naked_trd. :domain, # The domain name with the tld. :subdomain, # All subdomains, include ww?. :origin, # Scheme and host. :authority, # Userinfo and host. :site, # Scheme, userinfo, and host. :path, # Directory and segment. :segment, # Last portion of the path. :directory, # Any directories following the site within the URI. :filename, # Segment if a file extension is present. :suffix, # The file extension of the filename. :query, # Params and values as a string. :query_values, # A hash of params and values. :fragment, # Fragment identifier. :resource, # Path, query, and fragment. :location # Directory and resource - everything after the site. ]
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #+(uri) ⇒ Object (also: #join)
- #==(uri) ⇒ Object
- #=~(uri) ⇒ Object
- #absolute? ⇒ Boolean
-
#canonical ⇒ Object
Cleans and converts into a naked hostname.
- #canonicalized? ⇒ Boolean
- #clean ⇒ Object
- #clean? ⇒ Boolean
- #cleaned? ⇒ Boolean
-
#initialize(uri, options = {}, &blk) ⇒ URI
constructor
A new instance of URI.
- #ip_address? ⇒ Boolean
- #ipv4 ⇒ Object
- #ipv4? ⇒ Boolean
- #ipv6 ⇒ Object
- #ipv6? ⇒ Boolean
- #localhost? ⇒ Boolean
- #naked? ⇒ Boolean
- #normalized? ⇒ Boolean
- #parsed? ⇒ Boolean
- #raw ⇒ Object (also: #to_s)
- #relative? ⇒ Boolean
- #sha1 ⇒ Object (also: #hash)
- #unembedded? ⇒ Boolean
- #unescaped? ⇒ Boolean
- #valid? ⇒ Boolean
Constructor Details
#initialize(uri, options = {}, &blk) ⇒ URI
Returns a new instance of URI.
56 57 58 59 60 61 62 |
# File 'lib/url_parser/uri.rb', line 56 def initialize(uri, = {}, &blk) @input = uri @options = (, &blk) @block = blk ? blk : block_builder @uri = UrlParser::Parser.call(@input, @options, &@block) @model = UrlParser::Model.new(@uri) end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
54 55 56 |
# File 'lib/url_parser/uri.rb', line 54 def input @input end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
54 55 56 |
# File 'lib/url_parser/uri.rb', line 54 def @options end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
54 55 56 |
# File 'lib/url_parser/uri.rb', line 54 def uri @uri end |
Instance Method Details
#+(uri) ⇒ Object Also known as: join
171 172 173 |
# File 'lib/url_parser/uri.rb', line 171 def +(uri) self.class.new(uri.to_s, .merge({ base_uri: self.to_s}), &@block) end |
#==(uri) ⇒ Object
163 164 165 |
# File 'lib/url_parser/uri.rb', line 163 def ==(uri) clean == self.class.new(uri, clean: true).clean end |
#=~(uri) ⇒ Object
167 168 169 |
# File 'lib/url_parser/uri.rb', line 167 def =~(uri) canonical == self.class.new(uri, clean: true).canonical end |
#absolute? ⇒ Boolean
121 122 123 |
# File 'lib/url_parser/uri.rb', line 121 def absolute? uri.absolute? end |
#canonical ⇒ Object
Cleans and converts into a naked hostname
104 105 106 107 108 109 110 111 |
# File 'lib/url_parser/uri.rb', line 104 def canonical opts = { raw: true } curi = naked_hostname + location UrlParser::Parser.call(curi, opts) do |uri| uri.clean! end.sub(/\A[a-z]+:\/\//i, '//') end |
#canonicalized? ⇒ Boolean
76 77 78 |
# File 'lib/url_parser/uri.rb', line 76 def canonicalized? !![:canonicalize] end |
#clean ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/url_parser/uri.rb', line 94 def clean if cleaned? raw else UrlParser::Parser.call(@input, raw: true) { |uri| uri.clean! } end end |
#clean? ⇒ Boolean
113 114 115 |
# File 'lib/url_parser/uri.rb', line 113 def clean? cleaned? || self.to_s == clean end |
#cleaned? ⇒ Boolean
84 85 86 87 88 89 90 91 92 |
# File 'lib/url_parser/uri.rb', line 84 def cleaned? !![:clean] || ( unescaped? && parsed? && && canonicalized? && normalized? ) end |
#ip_address? ⇒ Boolean
145 146 147 |
# File 'lib/url_parser/uri.rb', line 145 def ip_address? ipv4? || ipv6? end |
#ipv4 ⇒ Object
129 130 131 |
# File 'lib/url_parser/uri.rb', line 129 def ipv4 hostname.to_s[Resolv::IPv4::Regex] end |
#ipv4? ⇒ Boolean
133 134 135 |
# File 'lib/url_parser/uri.rb', line 133 def ipv4? !!ipv4 end |
#ipv6 ⇒ Object
137 138 139 |
# File 'lib/url_parser/uri.rb', line 137 def ipv6 host.to_s[Resolv::IPv6::Regex] end |
#ipv6? ⇒ Boolean
141 142 143 |
# File 'lib/url_parser/uri.rb', line 141 def ipv6? !!ipv6 end |
#localhost? ⇒ Boolean
125 126 127 |
# File 'lib/url_parser/uri.rb', line 125 def localhost? !!(hostname.to_s[LOCALHOST_REGEXP]) end |
#naked? ⇒ Boolean
149 150 151 |
# File 'lib/url_parser/uri.rb', line 149 def naked? !localhost? && www.nil? end |
#normalized? ⇒ Boolean
80 81 82 |
# File 'lib/url_parser/uri.rb', line 80 def normalized? !![:normalize] end |
#parsed? ⇒ Boolean
68 69 70 |
# File 'lib/url_parser/uri.rb', line 68 def parsed? true end |
#raw ⇒ Object Also known as: to_s
153 154 155 |
# File 'lib/url_parser/uri.rb', line 153 def raw uri.to_s end |
#relative? ⇒ Boolean
117 118 119 |
# File 'lib/url_parser/uri.rb', line 117 def relative? uri.relative? end |
#sha1 ⇒ Object Also known as: hash
158 159 160 |
# File 'lib/url_parser/uri.rb', line 158 def sha1 Digest::SHA1.hexdigest(raw) end |
#unembedded? ⇒ Boolean
72 73 74 |
# File 'lib/url_parser/uri.rb', line 72 def !![:unembed] end |
#unescaped? ⇒ Boolean
64 65 66 |
# File 'lib/url_parser/uri.rb', line 64 def unescaped? !![:unescape] end |
#valid? ⇒ Boolean
176 177 178 179 180 |
# File 'lib/url_parser/uri.rb', line 176 def valid? return false if input.nil? || relative? return true if ip_address? || localhost? parsed_domain.valid? end |