Class: Wovnrb::URL
- Inherits:
-
Object
- Object
- Wovnrb::URL
- Defined in:
- lib/wovnrb/services/url.rb
Overview
URL utility ported from html-swapper
Defined Under Namespace
Modules: FileExtension
Class Method Summary collapse
- .absolute_path?(href) ⇒ Boolean
- .absolute_url?(href) ⇒ Boolean
-
.change_protocol(uri, new_protocol) ⇒ Object
Copy of uri [Addressable::URI].
- .file?(href_with_query_and_hash) ⇒ Boolean
- .host_with_port(parsed_uri) ⇒ Object
- .join_paths(*paths) ⇒ Object
-
.normalize_path_slash(original_path, new_path) ⇒ Object
if original path does not end in slash, remove it from new path if original path ends in slash, add it to new path.
-
.normalize_url(href) ⇒ Object
TODO: Maybe this should be applied to all get_attribute calls rather than just href.
- .path_and_query(parsed_uri) ⇒ Object
- .path_and_query_and_hash(parsed_uri) ⇒ Object
-
.prepend_path(url, dir) ⇒ Object
Set the path lang to.
- .prepend_path_slash(path) ⇒ Object
- .relative_path?(href) ⇒ Boolean
- .remove_query_and_hash(href) ⇒ Object
- .resolve_absolute_path(base_url, href) ⇒ Object
- .resolve_absolute_uri(base_url, href) ⇒ Object
- .trim_slashes(path) ⇒ Object
- .valid_protocol?(href) ⇒ Boolean
Class Method Details
.absolute_path?(href) ⇒ Boolean
22 23 24 |
# File 'lib/wovnrb/services/url.rb', line 22 def self.absolute_path?(href) href.match?(%r{^/}) end |
.absolute_url?(href) ⇒ Boolean
18 19 20 |
# File 'lib/wovnrb/services/url.rb', line 18 def self.absolute_url?(href) href =~ %r{^(https?:)?//}i end |
.change_protocol(uri, new_protocol) ⇒ Object
Returns copy of uri [Addressable::URI].
99 100 101 102 103 |
# File 'lib/wovnrb/services/url.rb', line 99 def self.change_protocol(uri, new_protocol) result = uri.dup result.scheme = new_protocol result end |
.file?(href_with_query_and_hash) ⇒ Boolean
112 113 114 115 116 117 118 119 |
# File 'lib/wovnrb/services/url.rb', line 112 def self.file?(href_with_query_and_hash) href = remove_query_and_hash(href_with_query_and_hash) img_files = %r{^(https?://)?.*(\.(#{FileExtension::IMG_FILES}))((\?|#).*)?$}io audio_files = %r{^(https?://)?.*(\.(#{FileExtension::AUDIO_FILES}))((\?|#).*)?$}io video_files = %r{^(https?://)?.*(\.(#{FileExtension::VIDEO_FILES}))((\?|#).*)?$}io doc_files = %r{^(https?://)?.*(\.(#{FileExtension::DOC_FILES}))((\?|#).*)?$}io href.match?(img_files) || href.match?(audio_files) || href.match?(video_files) || href.match?(doc_files) end |
.host_with_port(parsed_uri) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/wovnrb/services/url.rb', line 42 def self.host_with_port(parsed_uri) if parsed_uri.port "#{parsed_uri.host}:#{parsed_uri.port}" else parsed_uri.host.to_s end end |
.join_paths(*paths) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/wovnrb/services/url.rb', line 83 def self.join_paths(*paths) paths.inject('') do |left, right| case [left.end_with?('/'), right.start_with?('/')] when [true, true] left + right[1..-1] when [false, false] left + (right.blank? ? right : "/#{right}") else left + right end end end |
.normalize_path_slash(original_path, new_path) ⇒ Object
if original path does not end in slash, remove it from new path if original path ends in slash, add it to new path
127 128 129 130 131 132 133 134 |
# File 'lib/wovnrb/services/url.rb', line 127 def self.normalize_path_slash(original_path, new_path) if !original_path.end_with?('/') && new_path.end_with?('/') new_path = new_path.chomp('/') elsif original_path.end_with?('/') && !new_path.end_with?('/') new_path += '/' end new_path end |
.normalize_url(href) ⇒ Object
TODO: Maybe this should be applied to all get_attribute calls rather than just href
12 13 14 15 16 |
# File 'lib/wovnrb/services/url.rb', line 12 def self.normalize_url(href) return nil unless href href.delete("\u200b").strip end |
.path_and_query(parsed_uri) ⇒ Object
31 32 33 |
# File 'lib/wovnrb/services/url.rb', line 31 def self.path_and_query(parsed_uri) parsed_uri.path + (parsed_uri.query ? "?#{parsed_uri.query}" : '') end |
.path_and_query_and_hash(parsed_uri) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/wovnrb/services/url.rb', line 35 def self.path_and_query_and_hash(parsed_uri) uri = parsed_uri.path uri += "?#{parsed_uri.query}" if parsed_uri.query uri += "##{parsed_uri.fragment}" if parsed_uri.fragment uri end |
.prepend_path(url, dir) ⇒ Object
Set the path lang to
68 69 70 |
# File 'lib/wovnrb/services/url.rb', line 68 def self.prepend_path(url, dir) url.sub(%r{(.+\.[^/]+)(/|$)}, "\\1/#{dir}\\2") end |
.prepend_path_slash(path) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/wovnrb/services/url.rb', line 76 def self.prepend_path_slash(path) path ||= '' return path if path.starts_with?('/') "/#{path}" end |
.relative_path?(href) ⇒ Boolean
26 27 28 |
# File 'lib/wovnrb/services/url.rb', line 26 def self.relative_path?(href) !absolute_url?(href) && !absolute_path?(href) end |
.remove_query_and_hash(href) ⇒ Object
121 122 123 |
# File 'lib/wovnrb/services/url.rb', line 121 def self.remove_query_and_hash(href) href.gsub(/[#?].*/, '') end |
.resolve_absolute_path(base_url, href) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/wovnrb/services/url.rb', line 58 def self.resolve_absolute_path(base_url, href) normalized_uri = resolve_absolute_uri(base_url, href) path = normalized_uri.path query = normalized_uri.query ? "?#{normalized_uri.query}" : '' fragment = normalized_uri.fragment ? "##{normalized_uri.fragment}" : '' path + query + fragment end |
.resolve_absolute_uri(base_url, href) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/wovnrb/services/url.rb', line 50 def self.resolve_absolute_uri(base_url, href) # This resolves ./../ and also handles href already being absolute Addressable::URI.join(base_url, href) rescue Addressable::URI::InvalidURIError, ArgumentError => e Rollbar.warning('Failed to resolve absolute URI', original_error: e, base_url: base_url, href: href) raise end |
.trim_slashes(path) ⇒ Object
72 73 74 |
# File 'lib/wovnrb/services/url.rb', line 72 def self.trim_slashes(path) path.gsub(%r{^/|/$}, '') end |
.valid_protocol?(href) ⇒ Boolean
105 106 107 108 109 110 |
# File 'lib/wovnrb/services/url.rb', line 105 def self.valid_protocol?(href) scheme_matches = /^\s*(?<scheme>[a-zA-Z]+):/.match(href) scheme = scheme_matches ? scheme_matches[:scheme] : nil scheme.nil? || %w[http https].include?(scheme) end |