Module: Hyde::Util::ParserCommon

Defined in:
lib/hyde/util/parseutils.rb

Overview

Module for all things related to parsing HTTP and related syntax.

Constant Summary collapse

RFC1123_DATE =

strftime parameter to return a correct RFC 1123 date.

"%a, %d %b %Y %H:%M:%S GMT"

Class Method Summary collapse

Class Method Details

.make_value(input, opts, sep = ";") ⇒ String

Construct a parametrized header value. Does some input sanitization during construction

Parameters:

  • input (String)
  • opts (Hash)

Returns:

  • (String)

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hyde/util/parseutils.rb', line 80

def self.make_value(input, opts, sep = ";")
  output = input
  unless input.match? HeaderRegexp::PRINTABLE
    raise Hyde::ParsingError, "input is not ascii printable"
  end

  opts.each do |key, value|
    check_param(key, value)
    newparam = if value.is_a? String
                 "#{sep} #{key}=#{value}"
               else
                 "#{sep} #{key}"
               end
    output += newparam
  end
  output
end

.parse_value(input, sep: ";", unquote: false, regexp: nil) ⇒ Array(String, Hash)

Parse parametrized header values. This method will try the best attempt at decoding parameters. However, it does no decoding on the first argument.

Parameters:

  • input (String)
  • sep (String, Regexp) (defaults to: ";")
  • unquote (Boolean) (defaults to: false)

    interpret params as possibly quoted

  • regexp (Regexp, nil) (defaults to: nil)

    override param matching regexp

Returns:

  • (Array(String, Hash))


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hyde/util/parseutils.rb', line 51

def self.parse_value(input, sep: ";", unquote: false, regexp: nil)
  parts = input.split(sep).map { |x| URI.decode_uri_component(x).strip }
  base = parts.shift
  opts = parts.map do |raw|
    key, value = raw.match(if regexp
                             regexp
                           elsif unquote
                             HeaderRegexp::PARAM_QUOTED
                           else
                             HeaderRegexp::PARAM
                           end).to_a[1..]
    value = case value
            when "" then true
            when /\A".*"\z/ then value.undump
            else value
            end
    [key, value]
  end.to_h
  [base, opts]
end