module Sprockets::AssetURI

def self.build(path, params = {})

Returns String URI.

params - Hash of optional parameters
path - String file path

# => "file:///tmp/js/application.coffee?type=application/javascript"
build("/tmp/js/application.coffee", type: "application/javascript")

Examples

Internal: Build Asset URI.
def self.build(path, params = {})
  query = []
  params.each do |key, value|
    case value
    when String
      query << "#{key}=#{value}"
    when TrueClass
      query << "#{key}"
    when FalseClass, NilClass
    else
      raise TypeError, "unexpected type: #{value.class}"
    end
  end
  uri = "file://#{URI::Generic::DEFAULT_PARSER.escape(path)}"
  uri << "?#{query.join('&')}" if query.any?
  uri
end

def self.parse(str)

Returns String path and Hash of symbolized parameters.

str - String asset URI

# => "/tmp/js/application.coffee", {type: "application/javascript"}
parse("file:///tmp/js/application.coffee?type=application/javascript")

Examples

Internal: Parse Asset URI.
def self.parse(str)
  uri = URI(str)
  unless uri.scheme == 'file'
    raise URI::InvalidURIError, "expected file:// scheme: #{str}"
  end
  path = URI::Generic::DEFAULT_PARSER.unescape(uri.path)
  path.force_encoding(Encoding::UTF_8)
  params = uri.query.to_s.split('&').reduce({}) do |h, p|
    k, v = p.split('=', 2)
    h.merge(k.to_sym => v || true)
  end
  return path, params
end