Sha256: 97bd1bca039bb96aef01657e9f4d908d3df5e920f6e90b8a1b6011d26553d2a7

Contents?: true

Size: 962 Bytes

Versions: 2

Compression:

Stored size: 962 Bytes

Contents

require "accept_headers/acceptable"

module AcceptHeaders
  class Encoding
    include Comparable
    include Acceptable

    attr_reader :encoding

    ENCODING_PATTERN = /^\s*(?<encoding>[\w!#$%^&*\-\+{}\\|'.`~]+)\s*$/

    def initialize(encoding = '*', q: 1.0)
      self.encoding = encoding
      self.q = q
    end

    def <=>(other)
      q <=> other.q
    end

    def encoding=(value)
      @encoding = value.strip.downcase
    end

    def to_h
      {
        encoding: encoding,
        q: q
      }
    end

    def to_s
      qvalue = (q == 0 || q == 1) ? q.to_i : q
      "#{encoding};q=#{qvalue}"
    end

    def match(encoding_string)
      match_data = ENCODING_PATTERN.match(encoding_string)
      if !match_data
        false
      elsif encoding == match_data[:encoding]
        true
      elsif match_data[:encoding] == 'identity'
        true
      elsif encoding == '*'
        true
      else
        false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
accept_headers-0.0.7 lib/accept_headers/encoding.rb
accept_headers-0.0.6 lib/accept_headers/encoding.rb