Module: HttpHeaders::Utils::List
- Defined in:
- lib/http_headers/utils/list.rb
Overview
Constant Summary collapse
- HEADER_DELIMITER =
','
- PARAMETER_DELIMITER =
';'
Class Method Summary collapse
- .new(combined, entry_klazz:) ⇒ Object
- .parse(combined, entry_klazz:) ⇒ Object
- .stringify_entry(entry) ⇒ Object
- .to_header(list) ⇒ Object
Class Method Details
.new(combined, entry_klazz:) ⇒ Object
46 47 48 49 |
# 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:) ⇒ Object
38 39 40 41 42 43 44 |
# 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) ⇒ Object
57 58 59 60 61 |
# 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) ⇒ Object
51 52 53 54 55 |
# 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 |