Module: Sprockets::URIUtils
- Extended by:
- URIUtils
- Included in:
- Dependencies, Loader, PathDependencyUtils, Processing, Resolve, URIUtils
- Defined in:
- lib/sprockets/uri_utils.rb
Overview
Internal: Asset URI related parsing utilities. Mixed into Environment.
An Asset URI identifies the compiled Asset result. It shares the file: scheme and requires an absolute path.
Other query parameters
type - String output content type. Otherwise assumed from file extension.
This maybe different than the extension if the asset is transformed
from one content type to another. For an example .coffee -> .js.
id - Unique fingerprint of the entire asset and all its metadata. Assets
will only have the same id if they serialize to an identical value.
pipeline - String name of pipeline.
Instance Method Summary collapse
-
#build_asset_uri(path, params = {}) ⇒ Object
Internal: Build Asset URI.
-
#build_file_digest_uri(path) ⇒ Object
Internal: Build file-digest dependency URI.
-
#encode_uri_query_params(params) ⇒ Object
Internal: Serialize hash of params into query string.
-
#join_file_uri(scheme, host, path, query) ⇒ Object
Internal: Join file: URI component parts into String.
-
#join_uri(scheme, userinfo, host, port, registry, path, opaque, query, fragment) ⇒ Object
Internal: Join URI component parts into String.
-
#parse_asset_uri(uri) ⇒ Object
Internal: Parse Asset URI.
-
#parse_file_digest_uri(uri) ⇒ Object
Internal: Parse file-digest dependency URI.
-
#parse_uri_query_params(query) ⇒ Object
Internal: Parse query string into hash of params.
-
#split_file_uri(uri) ⇒ Object
Internal: Parse file: URI into component parts.
-
#split_uri(uri) ⇒ Object
Internal: Parse URI into component parts.
-
#valid_asset_uri?(str) ⇒ Boolean
Internal: Check if String is a valid Asset URI.
Instance Method Details
#build_asset_uri(path, params = {}) ⇒ Object
Internal: Build Asset URI.
Examples
build("/tmp/js/application.coffee", type: "application/javascript")
# => "file:///tmp/js/application.coffee?type=application/javascript"
path - String file path params - Hash of optional parameters
Returns String URI.
117 118 119 |
# File 'lib/sprockets/uri_utils.rb', line 117 def build_asset_uri(path, params = {}) join_file_uri("file", nil, path, encode_uri_query_params(params)) end |
#build_file_digest_uri(path) ⇒ Object
Internal: Build file-digest dependency URI.
Examples
build("/tmp/js/application.js")
# => "file-digest:/tmp/js/application.js"
path - String file path
Returns String URI.
151 152 153 |
# File 'lib/sprockets/uri_utils.rb', line 151 def build_file_digest_uri(path) join_file_uri('file-digest'.freeze, nil, path, nil) end |
#encode_uri_query_params(params) ⇒ Object
Internal: Serialize hash of params into query string.
params - Hash of params to serialize
Returns String query or nil if empty.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/sprockets/uri_utils.rb', line 160 def encode_uri_query_params(params) query = [] params.each do |key, value| case value when Integer query << "#{key}=#{value}" when String, Symbol query << "#{key}=#{URI_PARSER.escape(value.to_s)}" when TrueClass query << "#{key}" when FalseClass, NilClass else raise TypeError, "unexpected type: #{value.class}" end end "#{query.join('&'.freeze)}" if query.any? end |
#join_file_uri(scheme, host, path, query) ⇒ Object
Internal: Join file: URI component parts into String.
Returns String.
65 66 67 68 69 70 71 72 |
# File 'lib/sprockets/uri_utils.rb', line 65 def join_file_uri(scheme, host, path, query) str = +"#{scheme}://" str << host if host path = "/#{path}" unless path.start_with?("/".freeze) str << URI_PARSER.escape(path) str << "?#{query}" if query str end |
#join_uri(scheme, userinfo, host, port, registry, path, opaque, query, fragment) ⇒ Object
Internal: Join URI component parts into String.
Returns String.
39 40 41 |
# File 'lib/sprockets/uri_utils.rb', line 39 def join_uri(scheme, userinfo, host, port, registry, path, opaque, query, fragment) URI::Generic.new(scheme, userinfo, host, port, registry, path, opaque, query, fragment).to_s end |
#parse_asset_uri(uri) ⇒ Object
Internal: Parse Asset URI.
Examples
parse("file:///tmp/js/application.coffee?type=application/javascript")
# => "/tmp/js/application.coffee", {type: "application/javascript"}
uri - String asset URI
Returns String path and Hash of symbolized parameters.
96 97 98 99 100 101 102 103 104 |
# File 'lib/sprockets/uri_utils.rb', line 96 def parse_asset_uri(uri) scheme, _, path, query = split_file_uri(uri) unless scheme == 'file' raise URI::InvalidURIError, "expected file:// scheme: #{uri}" end return path, parse_uri_query_params(query) end |
#parse_file_digest_uri(uri) ⇒ Object
Internal: Parse file-digest dependency URI.
Examples
parse("file-digest:/tmp/js/application.js")
# => "/tmp/js/application.js"
uri - String file-digest URI
Returns String path.
131 132 133 134 135 136 137 138 139 |
# File 'lib/sprockets/uri_utils.rb', line 131 def parse_file_digest_uri(uri) scheme, _, path, _ = split_file_uri(uri) unless scheme == 'file-digest'.freeze raise URI::InvalidURIError, "expected file-digest scheme: #{uri}" end path end |
#parse_uri_query_params(query) ⇒ Object
Internal: Parse query string into hash of params
query - String query string
Return Hash of params.
185 186 187 188 189 190 191 192 |
# File 'lib/sprockets/uri_utils.rb', line 185 def parse_uri_query_params(query) query.to_s.split('&'.freeze).reduce({}) do |h, p| k, v = p.split('='.freeze, 2) v = URI_PARSER.unescape(v) if v h[k.to_sym] = v || true h end end |
#split_file_uri(uri) ⇒ Object
Internal: Parse file: URI into component parts.
uri - String uri
Returns [scheme, host, path, query].
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sprockets/uri_utils.rb', line 48 def split_file_uri(uri) scheme, _, host, _, _, path, _, query, _ = URI.split(uri) path = URI_PARSER.unescape(path) path.force_encoding(Encoding::UTF_8) # Hack for parsing Windows "/C:/Users/IEUser" paths if File::ALT_SEPARATOR && path[2] == ':' path = path[1..-1] end [scheme, host || '', path, query] end |
#split_uri(uri) ⇒ Object
Internal: Parse URI into component parts.
uri - String uri
Returns Array of components.
32 33 34 |
# File 'lib/sprockets/uri_utils.rb', line 32 def split_uri(uri) URI.split(uri) end |
#valid_asset_uri?(str) ⇒ Boolean
Internal: Check if String is a valid Asset URI.
str - Possible String asset URI.
Returns true or false.
79 80 81 82 83 84 |
# File 'lib/sprockets/uri_utils.rb', line 79 def valid_asset_uri?(str) # Quick prefix check before attempting a full parse str.start_with?("file://".freeze) && parse_asset_uri(str) ? true : false rescue URI::InvalidURIError false end |