Sha256: d45f97a8afad1effdc2b0ad0555071d81411b1ef05649ecbc94fe0ffe1df1c8f
Contents?: true
Size: 1.45 KB
Versions: 1
Compression:
Stored size: 1.45 KB
Contents
# frozen_string_literal: true module Genderize module Io module Parser class Header attr_reader :headers, :response, :strict def initialize(response, strict: true) @response = response @strict = strict @headers = [] end def self.parse(response, strict: true) instance = new(response, strict: strict) instance.parse instance end def parse @headers = @response.split(/\\r\\n|\n|\r/) end def method_missing(sym, *) detect_multi_value_keys(sym) || super end def respond_to_missing?(sym, *) detect_multi_value_keys(sym) || super end def response_code @headers[0].match(%r{HTTP/\d\.\d (\d{3}.*)})[1] end def version @headers[0].match(%r{HTTP/\d\.\d})[0] end private def detect_multi_value_keys(tag) tag = method_sym_to_http_pattern(tag) results = @headers.select { |val| val =~ /^#{tag}:/i }.map { |val| value_from(val) } results.size <= 1 ? results.first : results end def method_sym_to_http_pattern(sym) replacement = @strict ? '-' : '[-_]' sym.to_s.gsub('_', replacement) end def value_from(tag) val = tag.split(/:\s+/)[1] val =~ /^"(.*)"$/ ? Regexp.last_match(1) : val end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
genderize-io-1.4.0 | lib/genderize/io/parser/header.rb |