Class: UrlParser::URI

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {}, &blk)
  @input      = uri
  @options    = set_options(options, &blk)
  @block      = blk ? blk : block_builder
  @uri        = UrlParser::Parser.call(@input, @options, &@block)
  @model      = UrlParser::Model.new(@uri)
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



54
55
56
# File 'lib/url_parser/uri.rb', line 54

def input
  @input
end

#optionsObject (readonly)

Returns the value of attribute options.



54
55
56
# File 'lib/url_parser/uri.rb', line 54

def options
  @options
end

#uriObject (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, options.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

Returns:

  • (Boolean)


121
122
123
# File 'lib/url_parser/uri.rb', line 121

def absolute?
  uri.absolute?
end

#canonicalObject

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/url_parser/uri.rb', line 76

def canonicalized?
  !!options[:canonicalize]
end

#cleanObject



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

Returns:

  • (Boolean)


113
114
115
# File 'lib/url_parser/uri.rb', line 113

def clean?
  cleaned? || self.to_s == clean
end

#cleaned?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
# File 'lib/url_parser/uri.rb', line 84

def cleaned?
  !!options[:clean] || (
    unescaped?      &&
    parsed?         &&
    unembedded?     &&
    canonicalized?  &&
    normalized?
  )
end

#ip_address?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/url_parser/uri.rb', line 145

def ip_address?
  ipv4? || ipv6?
end

#ipv4Object



129
130
131
# File 'lib/url_parser/uri.rb', line 129

def ipv4
  hostname.to_s[Resolv::IPv4::Regex]
end

#ipv4?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/url_parser/uri.rb', line 133

def ipv4?
  !!ipv4
end

#ipv6Object



137
138
139
# File 'lib/url_parser/uri.rb', line 137

def ipv6
  host.to_s[Resolv::IPv6::Regex]
end

#ipv6?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/url_parser/uri.rb', line 141

def ipv6?
  !!ipv6
end

#localhost?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/url_parser/uri.rb', line 125

def localhost?
  !!(hostname.to_s[LOCALHOST_REGEXP])
end

#naked?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/url_parser/uri.rb', line 149

def naked?
  !localhost? && www.nil?
end

#normalized?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/url_parser/uri.rb', line 80

def normalized?
  !!options[:normalize]
end

#parsed?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/url_parser/uri.rb', line 68

def parsed?
  true
end

#rawObject Also known as: to_s



153
154
155
# File 'lib/url_parser/uri.rb', line 153

def raw
  uri.to_s
end

#relative?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/url_parser/uri.rb', line 117

def relative?
  uri.relative?
end

#sha1Object Also known as: hash



158
159
160
# File 'lib/url_parser/uri.rb', line 158

def sha1
  Digest::SHA1.hexdigest(raw)
end

#unembedded?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/url_parser/uri.rb', line 72

def unembedded?
  !!options[:unembed]
end

#unescaped?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/url_parser/uri.rb', line 64

def unescaped?
  !!options[:unescape]
end

#valid?Boolean

Returns:

  • (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