Module: Sprockets::HTTPUtils

### Home Classes Methods

### In Files

- lib/sprockets/http_utils.rb

### Methods

- #find_best_mime_type_match
- #find_best_q_match
- #find_mime_type_matches
- #find_q_matches
- #match_mime_type?
- #match_mime_type_keys
- #parse_q_values

### 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::HTTPUtils

Internal: HTTP URI utilities. Many adapted from Rack::Utils. Mixed into Environment.

### Public Instance Methods

find_best_mime_type_match(q_value_header, available) click to toggle source

Internal: Find the best qvalue match from an Array of available mime type options.

Adapted from Rack::Utils#q_values.

Returns the matched mime type String from available Array or nil.

    # File lib/sprockets/http_utils.rb, line 111
    def find_best_mime_type_match(q_value_header, available)
      find_best_q_match(q_value_header, available) do |a, b|
        match_mime_type?(a, b)
      end
    end

find_best_q_match(q_values, available, &matcher) click to toggle source

Internal: Find the best qvalue match from an Array of available options.

Adapted from Rack::Utils#q_values.

Returns the matched String from available Array or nil.

    # File lib/sprockets/http_utils.rb, line 89
    def find_best_q_match(q_values, available, &matcher)
      find_q_matches(q_values, available, &matcher).first
    end

find_mime_type_matches(q_value_header, available) click to toggle source

Internal: Find the all qvalue match from an Array of available mime type options.

Adapted from Rack::Utils#q_values.

Returns Array of matched mime type Strings from available Array or [].

    # File lib/sprockets/http_utils.rb, line 99
    def find_mime_type_matches(q_value_header, available)
      find_q_matches(q_value_header, available) do |a, b|
        match_mime_type?(a, b)
      end
    end

find_q_matches(q_values, available, &matcher) click to toggle source

Internal: Find all qvalue matches from an Array of available options.

Adapted from Rack::Utils#q_values.

Returns Array of matched Strings from available Array or [].

    # File lib/sprockets/http_utils.rb, line 58
    def find_q_matches(q_values, available, &matcher)
      matcher ||= lambda { |a, b| a == b }

      matches = []

      case q_values
      when Array
      when String
        q_values = parse_q_values(q_values)
      when NilClass
        q_values = []
      else
        raise TypeError, "unknown q_values type: #{q_values.class}"
      end

      q_values.each do |accepted, quality|
        if match = available.find { |option| matcher.call(option, accepted) }
          matches << [match, quality]
        end
      end

      matches.sort_by! { |match, quality| -quality }
      matches.map! { |match, quality| match }
      matches
    end

match_mime_type?(value, matcher) click to toggle source

Public: Test mime type against mime range.

    match_mime_type?('text/html', 'text/*') => true
    match_mime_type?('text/plain', '*') => true
    match_mime_type?('text/html', 'application/json') => false

Returns true if the given value is a mime match for the given mime match specification, false otherwise.

    # File lib/sprockets/http_utils.rb, line 15
    def match_mime_type?(value, matcher)
      v1, v2 = value.split('/', 2)
      m1, m2 = matcher.split('/', 2)
      (m1 == '*' || v1 == m1) && (m2.nil? || m2 == '*' || m2 == v2)
    end

match_mime_type_keys(hash, mime_type) click to toggle source

Public: Return values from Hash where the key matches the mime type.

hash - Hash of String matcher keys to Object values mime_type - String mime type

Returns Array of Object values.

    # File lib/sprockets/http_utils.rb, line 27
    def match_mime_type_keys(hash, mime_type)
      type, subtype = mime_type.split('/', 2)
      [
        hash["*"],
        hash["*/*"],
        hash["#{type}/*"],
        hash["#{type}/#{subtype}"]
      ].compact
    end

parse_q_values(values) click to toggle source

Internal: Parse Accept header quality values.

Adapted from Rack::Utils#q_values.

Returns an Array of [String, Float].

    # File lib/sprockets/http_utils.rb, line 42
    def parse_q_values(values)
      values.to_s.split(/\s*,\s*/).map do |part|
        value, parameters = part.split(/\s*;\s*/, 2)
        quality = 1.0
        if md = /\Aq=([\d.]+)/.match(parameters)
          quality = md[1].to_f
        end
        [value, quality]
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.