Class: UrlParser::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/url_parser/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, domain = nil) ⇒ Model

Returns a new instance of Model.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/url_parser/model.rb', line 6

def initialize(uri, domain = nil)
  unless uri.is_a?(Addressable::URI)
    raise RequiresAddressableURI,
      "#{uri} must be an Addressable::URI"
  end

  unless domain.is_a?(UrlParser::Domain)
    raise RequiresUrlParserDomain,
      "#{domain} must be a UrlParser::Domain"
  end if domain

  @parsed_uri     = uri
  @parsed_domain  = domain || UrlParser::Domain.new(uri.hostname)
end

Instance Attribute Details

#parsed_domainObject (readonly)

Returns the value of attribute parsed_domain.



4
5
6
# File 'lib/url_parser/model.rb', line 4

def parsed_domain
  @parsed_domain
end

#parsed_uriObject (readonly)

Returns the value of attribute parsed_uri.



4
5
6
# File 'lib/url_parser/model.rb', line 4

def parsed_uri
  @parsed_uri
end

Instance Method Details

#authorityObject

Userinfo and host.



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

def authority
  parsed_uri.authority
end

#directoryObject

Any directories following the site within the URI.



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/url_parser/model.rb', line 157

def directory
  unless path.nil? || path.empty?
    parts = path.split('/')
    if parts.empty?
      '/'
    else
      parts.pop unless segment.to_s.empty?
      parts.unshift('') unless parts.first.to_s.empty?
      parts.compact.join('/')
    end
  end
end

#domainObject

The domain name with the tld.



114
115
116
# File 'lib/url_parser/model.rb', line 114

def domain
  parsed_domain.domain
end

#filenameObject

Segment if a file extension is present.



172
173
174
# File 'lib/url_parser/model.rb', line 172

def filename
  segment.to_s[/.+\..+/]
end

#fragmentObject

Fragment identifier.



200
201
202
# File 'lib/url_parser/model.rb', line 200

def fragment
  parsed_uri.fragment
end

#hostObject

Hostname and port.



70
71
72
73
# File 'lib/url_parser/model.rb', line 70

def host
  result = [ hostname, port ].compact.join(':')
  result.empty? ? nil : result
end

#hostnameObject

Fully qualified domain name or IP address.



48
49
50
# File 'lib/url_parser/model.rb', line 48

def hostname
  parsed_uri.host
end

#locationObject

Directory and resource - everything after the site.



213
214
215
216
217
218
219
220
# File 'lib/url_parser/model.rb', line 213

def location
  if directory == '/'
    directory + resource.to_s
  else
    result = [ directory, resource ].compact.join('/')
    result.empty? ? nil : result
  end
end

#naked_hostnameObject

Fully qualified domain name or IP address without ww? prefix.



54
55
56
57
58
59
60
# File 'lib/url_parser/model.rb', line 54

def naked_hostname
  if www
    hostname.sub(/\A#{www}./, '')
  else
    hostname
  end
end

#naked_trdObject Also known as: naked_subdomain

Any non-ww? subdomains.



107
108
109
# File 'lib/url_parser/model.rb', line 107

def naked_trd
  (trd && www) ? trd[/(?<=^#{www}\.).+/] : trd
end

#originObject

Scheme and host.



126
127
128
129
# File 'lib/url_parser/model.rb', line 126

def origin
  original_origin = parsed_uri.origin
  original_origin == "null" ? nil : original_origin
end

#passwordObject

Password portion of the userinfo.



36
37
38
# File 'lib/url_parser/model.rb', line 36

def password
  parsed_uri.password
end

#pathObject

Directory and segment.



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

def path
  parsed_uri.path
end

#portObject

Port number.



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

def port
  parsed_uri.port
end

#queryObject

Params and values as a string.



188
189
190
# File 'lib/url_parser/model.rb', line 188

def query
  parsed_uri.query
end

#query_valuesObject

A hash of params and values.



194
195
196
# File 'lib/url_parser/model.rb', line 194

def query_values
  parsed_uri.query_values.to_h
end

#resourceObject

Path, query, and fragment.



206
207
208
209
# File 'lib/url_parser/model.rb', line 206

def resource
  name = [ segment, query_string, fragment_string ].compact.join
  name.empty? ? nil : name
end

#schemeObject

Top level URI naming structure / protocol.



23
24
25
# File 'lib/url_parser/model.rb', line 23

def scheme
  parsed_uri.scheme
end

#segmentObject

Last portion of the path.



151
152
153
# File 'lib/url_parser/model.rb', line 151

def segment
  (path =~ /\/\z/ ? nil : path.split('/').last) if path
end

#siteObject

Scheme, userinfo, and host.



139
140
141
# File 'lib/url_parser/model.rb', line 139

def site
  parsed_uri.site
end

#sldObject Also known as: second_level_domain, domain_name

Returns the second level domain portion, aka the domain part.



91
92
93
# File 'lib/url_parser/model.rb', line 91

def sld
  parsed_domain.sld
end

#subdomainObject

All subdomains, include ww?.



120
121
122
# File 'lib/url_parser/model.rb', line 120

def subdomain
  parsed_domain.subdomain
end

#suffixObject

The file extension of the filename.



178
179
180
181
182
183
184
# File 'lib/url_parser/model.rb', line 178

def suffix
  if path
    ext = File.extname(path)
    ext[0] = '' if ext[0] == '.'
    ext.empty? ? nil : ext
  end
end

#tldObject Also known as: top_level_domain, extension

Returns the top level domain portion, aka the extension.



83
84
85
# File 'lib/url_parser/model.rb', line 83

def tld
  parsed_domain.tld
end

#trdObject Also known as: third_level_domain, subdomains

Returns the third level domain portion, aka the subdomain part.



99
100
101
# File 'lib/url_parser/model.rb', line 99

def trd
  parsed_domain.trd
end

#userinfoObject

URI username and password for authentication.



42
43
44
# File 'lib/url_parser/model.rb', line 42

def userinfo
  parsed_uri.userinfo
end

#usernameObject Also known as: user

Username portion of the userinfo.



29
30
31
# File 'lib/url_parser/model.rb', line 29

def username
  parsed_uri.user
end

#wwwObject

The ww? portion of the subdomain.



77
78
79
# File 'lib/url_parser/model.rb', line 77

def www
  trd.split('.').first.to_s[/www?\d*/] if trd
end