Module: Sprockets::URIUtils

### Home Classes Methods

### In Files

- lib/sprockets/uri_utils.rb

### Methods

- #build_asset_uri
- #build_file_digest_uri
- #encode_uri_query_params
- #join_file_uri
- #join_uri
- #parse_asset_uri
- #parse_file_digest_uri
- #parse_uri_query_params
- #split_file_uri
- #split_uri
- #valid_asset_uri?

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::URIUtils

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.

### Public Instance Methods

build_asset_uri(path, params = {}) click to toggle source

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.

    # File lib/sprockets/uri_utils.rb, line 111
    def build_asset_uri(path, params = {})
      join_file_uri("file", nil, path, encode_uri_query_params(params))
    end

build_file_digest_uri(path) click to toggle source

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.

    # File lib/sprockets/uri_utils.rb, line 145
    def build_file_digest_uri(path)
      join_file_uri('file-digest'.freeze, nil, path, nil)
    end

encode_uri_query_params(params) click to toggle source

Internal: Serialize hash of params into query string.

params - Hash of params to serialize

Returns String query or nil if empty.

    # File lib/sprockets/uri_utils.rb, line 154
    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::Generic::DEFAULT_PARSER.escape(value.to_s)}"
        when TrueClass
          query << "#{key}"
        when FalseClass, NilClass
        else
          raise TypeError, "unexpected type: #{value.class}"
        end
      end

      "#{query.join('&')}" if query.any?
    end

join_file_uri(scheme, host, path, query) click to toggle source

Internal: Join file: URI component parts into String.

Returns String.

    # File lib/sprockets/uri_utils.rb, line 59
    def join_file_uri(scheme, host, path, query)
      str = "#{scheme}://"
      str << host if host
      path = "/#{path}" unless path.start_with?("/")
      str << URI::Generic::DEFAULT_PARSER.escape(path)
      str << "?#{query}" if query
      str
    end

join_uri(scheme, userinfo, host, port, registry, path, opaque, query, fragment) click to toggle source

Internal: Join URI component parts into String.

Returns String.

    # File lib/sprockets/uri_utils.rb, line 35
    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) click to toggle source

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.

    # File lib/sprockets/uri_utils.rb, line 90
    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) click to toggle source

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.

    # File lib/sprockets/uri_utils.rb, line 125
    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) click to toggle source

Internal: Parse query string into hash of params

query - String query string

Return Hash of params.

    # File lib/sprockets/uri_utils.rb, line 179
    def parse_uri_query_params(query)
      query.to_s.split('&').reduce({}) do |h, p|
        k, v = p.split('=', 2)
        v = URI::Generic::DEFAULT_PARSER.unescape(v) if v
        h[k.to_sym] = v || true
        h
      end
    end

split_file_uri(uri) click to toggle source

Internal: Parse file: URI into component parts.

uri - String uri

Returns [scheme, host, path, query].

    # File lib/sprockets/uri_utils.rb, line 44
    def split_file_uri(uri)
      scheme, _, host, _, _, path, _, query, _ = URI.split(uri)

      path = URI::Generic::DEFAULT_PARSER.unescape(path)
      path.force_encoding(Encoding::UTF_8)

      # Hack for parsing Windows "file:///C:/Users/IEUser" paths
      path.gsub!(/^\/([a-zA-Z]:)/, '\1'.freeze)

      [scheme, host, path, query]
    end

split_uri(uri) click to toggle source

Internal: Parse URI into component parts.

uri - String uri

Returns Array of components.

    # File lib/sprockets/uri_utils.rb, line 28
    def split_uri(uri)
      URI.split(uri)
    end

valid_asset_uri?(str) click to toggle source

Internal: Check if String is a valid Asset URI.

str - Possible String asset URI.

Returns true or false.

    # File lib/sprockets/uri_utils.rb, line 73
    def valid_asset_uri?(str)
      # Quick prefix check before attempting a full parse
      str.start_with?("file://") && parse_asset_uri(str) ? true : false
    rescue URI::InvalidURIError
      false
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.