Sha256: f5f95d7f99b4aaaadfc5d3d241a57fb187c5314ed50b673fa4a6bad9034dd3a3
Contents?: true
Size: 1.93 KB
Versions: 3
Compression:
Stored size: 1.93 KB
Contents
require "accept_headers/acceptable" module AcceptHeaders class MediaType include Comparable include Acceptable attr_reader :type, :subtype, :extensions def initialize(type = '*', subtype = '*', q: 1.0, extensions: {}) self.type = type self.subtype = subtype self.q = q self.extensions = extensions end def <=>(other) if q < other.q -1 elsif q > other.q 1 elsif (type == '*' && other.type != '*') || (subtype == '*' && other.subtype != '*') -1 elsif (type != '*' && other.type == '*') || (subtype != '*' && other.subtype == '*') 1 elsif extensions.size < other.extensions.size -1 elsif extensions.size > other.extensions.size 1 else 0 end end def type=(value) @type = value.strip.downcase end def subtype=(value) @subtype = if value.nil? && type == '*' '*' else value.strip.downcase end end def extensions=(hash) @extensions = {} hash.each do |k,v| @extensions[k.strip] = v end @extensions end def to_h { type: type, subtype: subtype, q: q, extensions: extensions } end def to_s qvalue = (q == 0 || q == 1) ? q.to_i : q string = "#{media_range};q=#{qvalue}" if extensions.size > 0 extensions.each { |k, v| string.concat(";#{k}=#{v}") } end string end def media_range "#{type}/#{subtype}" end def match(media_range_string) match_data = Negotiator::MEDIA_TYPE_PATTERN.match(media_range_string) if !match_data false elsif type == match_data[:type] && subtype == match_data[:subtype] true elsif type == match_data[:type] && subtype == '*' true elsif type == '*' && subtype == '*' true else false end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
accept_headers-0.1.0 | lib/accept_headers/media_type.rb |
accept_headers-0.0.9 | lib/accept_headers/media_type.rb |
accept_headers-0.0.8 | lib/accept_headers/media_type.rb |