Class: MetaInspector::URL
- Inherits:
-
Object
- Object
- MetaInspector::URL
- Defined in:
- lib/meta_inspector/url.rb
Constant Summary collapse
- WELL_KNOWN_TRACKING_PARAMS =
%w( utm_source utm_medium utm_term utm_content utm_campaign )
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
-
.absolutify(url, base_url, options = {}) ⇒ Object
Converts a relative URL to an absolute URL, like: “/faq” => “example.com/faq” Respecting already absolute URLs like the ones starting with http:, ftp:, telnet:, mailto:, javascript: …
Instance Method Summary collapse
- #host ⇒ Object
-
#initialize(initial_url, options = {}) ⇒ URL
constructor
A new instance of URL.
- #root_url ⇒ Object
- #scheme ⇒ Object
- #tracked? ⇒ Boolean
- #untrack! ⇒ Object
- #untracked_url ⇒ Object
Constructor Details
#initialize(initial_url, options = {}) ⇒ URL
Returns a new instance of URL.
7 8 9 10 11 12 13 |
# File 'lib/meta_inspector/url.rb', line 7 def initialize(initial_url, = {}) = self.class.defaults.merge() @normalize = [:normalize] self.url = initial_url end |
Instance Attribute Details
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/meta_inspector/url.rb', line 5 def url @url end |
Class Method Details
.absolutify(url, base_url, options = {}) ⇒ Object
Converts a relative URL to an absolute URL, like:
"/faq" => "http://example.com/faq"
Respecting already absolute URLs like the ones starting with
http:, ftp:, telnet:, mailto:, javascript: ...
Protocol-relative URLs are also resolved to use the same schema as the base_url
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/meta_inspector/url.rb', line 59 def self.absolutify(url, base_url, = {}) = defaults.merge() if url =~ /^\w*\:/i MetaInspector::URL.new(url, ).url else uri = Addressable::URI.join(base_url, url) [:normalize] ? uri.normalize.to_s : uri.to_s end rescue MetaInspector::ParserError, Addressable::URI::InvalidURIError, ArgumentError nil end |
Instance Method Details
#host ⇒ Object
19 20 21 |
# File 'lib/meta_inspector/url.rb', line 19 def host parsed(url) ? parsed(url).host : nil end |
#root_url ⇒ Object
23 24 25 |
# File 'lib/meta_inspector/url.rb', line 23 def root_url "#{scheme}://#{host}/" end |
#scheme ⇒ Object
15 16 17 |
# File 'lib/meta_inspector/url.rb', line 15 def scheme parsed(url) ? parsed(url).scheme : nil end |
#tracked? ⇒ Boolean
29 30 31 32 33 34 |
# File 'lib/meta_inspector/url.rb', line 29 def tracked? u = parsed(url) return false unless u.query_values found_tracking_params = WELL_KNOWN_TRACKING_PARAMS & u.query_values.keys return found_tracking_params.any? end |
#untrack! ⇒ Object
44 45 46 |
# File 'lib/meta_inspector/url.rb', line 44 def untrack! self.url = untracked_url if tracked? end |
#untracked_url ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/meta_inspector/url.rb', line 36 def untracked_url u = parsed(url) return url unless u.query_values query_values = u.query_values.delete_if { |key, _| WELL_KNOWN_TRACKING_PARAMS.include? key } u.query_values = query_values.length > 0 ? query_values : nil u.to_s end |