Module: Seam::UrlSearchParamsSerializer

Defined in:
lib/seam/url_search_params_serializer.rb

Overview

Serializes parameters to a URL query string following the @seamapi/url-search-params-serializer standard: https://github.com/seamapi/url-search-params-serializer

Class Method Summary collapse

Class Method Details

.nested_update(search_params, params, path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/seam/url_search_params_serializer.rb', line 63

def self.nested_update(search_params, params, path)
  params.each do |key, value|
    unless key.is_a?(String) || key.is_a?(Symbol)
      raise UnserializableParamError.new(
        key.inspect,
        "has a name that is not a string which is unsupported"
      )
    end
    key = key.to_s

    if key.include?(".")
      raise UnserializableParamError.new(
        key,
        'contains one or more dots "." in its name which is unsupported'
      )
    end

    current_path = [*path, key]

    if value.is_a?(Hash)
      nested_update(search_params, value, current_path)
      next
    end

    name = current_path.join(".")

    next if value.nil?

    value = value.to_s if value.is_a?(Symbol)

    next if value.is_a?(String) && value.empty?

    if value.is_a?(Array)
      serialize_array(search_params, name, value)
      next
    end

    search_params.set(name, serialize_value(name, value))
  end
end

.serialize_array(search_params, name, values) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/seam/url_search_params_serializer.rb', line 104

def self.serialize_array(search_params, name, values)
  # The parser reads a single pair with an empty value as an empty array.
  if values.empty?
    search_params.set(name, "")
    return
  end

  values = values.map { |value| value.is_a?(Symbol) ? value.to_s : value }

  if values.length == 1 && values.first == ""
    raise UnserializableParamError.new(
      name,
      "is a single element array containing the empty string which is unsupported"
    )
  end

  if values.any? { |value| value == "" }
    raise UnserializableParamError.new(
      name,
      "is an array containing the empty string which is unsupported"
    )
  end

  if values.any? { |value| value.nil? || value.is_a?(Seam::Null) }
    raise UnserializableParamError.new(
      name,
      "is an array containing null or undefined values which is unsupported"
    )
  end

  values.each { |value| search_params.append(name, serialize_value(name, value)) }
end

.serialize_float(name, value) ⇒ Object

Formats a float exactly like ECMAScript Number::toString.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/seam/url_search_params_serializer.rb', line 153

def self.serialize_float(name, value)
  raise UnserializableParamError.new(name, "is NaN") if value.nan?
  if value.infinite?
    raise UnserializableParamError.new(name, "is #{value.positive? ? "Infinity" : "-Infinity"}")
  end
  return "0" if value.zero?

  digits, point = shortest_decimal(value.abs)
  count = digits.length

  formatted = if point.between?(count, 21)
    digits + "0" * (point - count)
  elsif point.positive? && point <= 21
    "#{digits[0, point]}.#{digits[point..]}"
  elsif point > -6 && point <= 0
    "0.#{"0" * -point}#{digits}"
  else
    mantissa = (count == 1) ? digits : "#{digits[0]}.#{digits[1..]}"
    exponent = point - 1
    "#{mantissa}e#{(exponent >= 0) ? "+" : "-"}#{exponent.abs}"
  end

  value.negative? ? "-#{formatted}" : formatted
end

.serialize_time(time) ⇒ Object

Formats a time exactly like JavaScript's Date#toISOString.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/seam/url_search_params_serializer.rb', line 202

def self.serialize_time(time)
  utc = time.getutc
  year = if utc.year.between?(0, 9999)
    format("%04d", utc.year)
  elsif utc.year > 9999
    format("+%06d", utc.year)
  else
    format("-%06d", -utc.year)
  end
  format(
    "%s-%02d-%02dT%02d:%02d:%02d.%03dZ",
    year, utc.month, utc.day, utc.hour, utc.min, utc.sec, utc.nsec / 1_000_000
  )
end

.serialize_url_search_params(params, strict: false) ⇒ String

Returns The query string, without a leading ?.

Parameters:

  • params (Hash)
  • strict (Boolean) (defaults to: false)

    Whether to add _strict=true to a non-empty query string

Returns:

  • (String)

    The query string, without a leading ?

Raises:



36
37
38
39
40
# File 'lib/seam/url_search_params_serializer.rb', line 36

def self.serialize_url_search_params(params, strict: false)
  search_params = UrlSearchParams.new
  update_url_search_params(search_params, params, strict: strict)
  search_params.to_s
end

.serialize_value(name, value) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/seam/url_search_params_serializer.rb', line 137

def self.serialize_value(name, value)
  case value
  when Seam::Null then ""
  when String then value
  when true, false then value.to_s
  when Integer then value.to_s
  when Float then serialize_float(name, value)
  when Time then serialize_time(value)
  when DateTime then serialize_time(value.to_time)
  when Date then serialize_time(Time.utc(value.year, value.month, value.day))
  else
    raise UnserializableParamError.new(name, "is a #{value.class}")
  end
end

.shortest_decimal(value) ⇒ Object

Returns the shortest round-tripping decimal digits of a positive float and the position of the decimal point relative to the first digit.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/seam/url_search_params_serializer.rb', line 180

def self.shortest_decimal(value)
  repr = value.to_s

  if repr.include?("e")
    mantissa, exponent = repr.split("e")
    integer_part, fraction_part = mantissa.split(".")
    digits = integer_part + (fraction_part || "")
    point = integer_part.length + exponent.to_i
  else
    integer_part, fraction_part = repr.split(".")
    digits = integer_part + (fraction_part || "")
    point = integer_part.length
  end

  leading_zeros = digits[/\A0*/].length
  digits = digits[leading_zeros..]
  point -= leading_zeros

  [digits.sub(/0+\z/, ""), point]
end

.update_url_search_params(search_params, params, strict: false) ⇒ nil

Serializes parameters into an existing Seam::UrlSearchParams collection, preserving pairs it does not overwrite.

Parameters:

  • search_params (UrlSearchParams)
  • params (Hash)
  • strict (Boolean) (defaults to: false)

    Whether to add _strict=true when the resulting collection is non-empty

Returns:

  • (nil)

Raises:



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/seam/url_search_params_serializer.rb', line 51

def self.update_url_search_params(search_params, params, strict: false)
  nested_update(search_params, params, [])
  search_params.sort!

  if strict && !search_params.empty?
    search_params.delete("_strict")
    search_params.append("_strict", "true")
  end

  nil
end