module HttpHeaders::Utils::List
@example Accept values
class Accept < DelegateClass(Array) def initialize(value) super HttpHeaders::List.new(value, entry_klazz: Accept::Entry) end class Entry def initialize(media_type, index: parameters:) ... end def q parameters.fetch(:q) { 1.0 }.to_f end def <=>(other) quality = other.q <=> q return quality unless quality.zero? index <=> other.index end end end Accept.new(['*/*; q=0.1', 'application/json, text/html; q=0.8']) # => List['application/json', 'text/html', '*/*']
Constants
- HEADER_DELIMITER
- PARAMETER_DELIMITER
Public Instance Methods
new(combined, entry_klazz:)
click to toggle source
# File lib/http_headers/utils/list.rb, line 46 def new(combined, entry_klazz:) result = parse(combined, entry_klazz: entry_klazz) entry_klazz.instance_methods(false).include?(:<=>) ? result.sort! : result end
parse(combined, entry_klazz:)
click to toggle source
# File lib/http_headers/utils/list.rb, line 38 def parse(combined, entry_klazz:) Array(combined).map { |line| line.split(HEADER_DELIMITER) }.flatten.each_with_index.map do |entry, index| value, *parameters = entry.strip.split(PARAMETER_DELIMITER) indexed_parameters = Hash[Array(parameters).map { |p| p.strip.split('=') }].transform_keys!(&:to_sym) entry_klazz.new(value, index: index, parameters: indexed_parameters) end end
stringify_entry(entry)
click to toggle source
# File lib/http_headers/utils/list.rb, line 57 def stringify_entry(entry) return entry.to_header if entry.respond_to?(:to_header) return entry.to_s if entry.respond_to?(:to_s) entry.inspect end
to_header(list)
click to toggle source
# File lib/http_headers/utils/list.rb, line 51 def to_header(list) # noinspection RubyBlockToMethodReference list.map { |entry| stringify_entry(entry) } .join("#{HEADER_DELIMITER} ") end