Module: ScalarRubyTest::Runtime

Defined in:
lib/amritk-scalar-test/runtime.rb

Defined Under Namespace

Classes: Page, RawResponse, ResultResponse, Stream, WebSocketConnection, WebSocketMessageError, WebSocketRequest

Constant Summary collapse

RETRYABLE_STATUS =
[408, 409, 429, 500, 502, 503, 504].freeze

Class Method Summary collapse

Class Method Details

.body_encoding_for_content_type(content_type) ⇒ Object



472
473
474
475
476
477
478
# File 'lib/amritk-scalar-test/runtime.rb', line 472

def body_encoding_for_content_type(content_type)
  value = content_type.to_s.downcase
  return :multipart if value.include?("multipart/")
  return :form if value.include?("x-www-form-urlencoded")
  return :json if value.include?("json")
  :binary
end

.compact_hash(hash) ⇒ Object



18
19
20
# File 'lib/amritk-scalar-test/runtime.rb', line 18

def compact_hash(hash)
  hash.each_with_object({}) { |(key, value), result| result[key] = value unless value.nil? }
end

.deserialize(value, descriptor) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/amritk-scalar-test/runtime.rb', line 30

def deserialize(value, descriptor)
  return value if value.nil? || descriptor.nil?
  return value.map { |item| deserialize(item, descriptor.first) } if descriptor.is_a?(Array) && value.is_a?(Array)
  return value unless descriptor.is_a?(String)
  klass = Models.const_get(descriptor, false)
  klass.respond_to?(:from_h) ? klass.from_h(value) : value
rescue NameError
  value
end

.extra_keys(data, known) ⇒ Object



394
395
396
# File 'lib/amritk-scalar-test/runtime.rb', line 394

def extra_keys(data, known)
  data.reject { |key, _value| known.include?(key) }
end

.extract_error_message(body) ⇒ Object



429
430
431
432
433
434
# File 'lib/amritk-scalar-test/runtime.rb', line 429

def extract_error_message(body)
  return nil unless body.is_a?(Hash)
  candidate = body["message"] || body["error"] || body["detail"]
  return candidate if candidate.is_a?(String)
  candidate["message"] if candidate.is_a?(Hash) && candidate["message"].is_a?(String)
end

.header_value(headers, name) ⇒ Object



412
413
414
415
416
# File 'lib/amritk-scalar-test/runtime.rb', line 412

def header_value(headers, name)
  return nil unless headers.is_a?(Hash)
  value = headers[name] || headers[name.to_s.downcase] || headers[name.to_s]
  value.is_a?(Array) ? value.first : value
end


418
419
420
421
422
423
424
425
426
427
# File 'lib/amritk-scalar-test/runtime.rb', line 418

def link_header_url(headers, name, rel)
  header = header_value(headers, name)
  return nil if header.nil?
  header.to_s.split(",").each do |part|
    url = part[/<([^>]+)>/, 1]
    rel_value = part[/rel="?([^";,]+)"?/, 1]
    return url if url && rel_value == rel
  end
  nil
end

.parse_json(text) ⇒ Object



398
399
400
401
402
403
# File 'lib/amritk-scalar-test/runtime.rb', line 398

def parse_json(text)
  return nil if text.nil? || text.empty?
  JSON.parse(text)
rescue JSON::ParserError
  text
end

.request_option_hash(options, *keys) ⇒ Object



442
443
444
445
446
447
448
# File 'lib/amritk-scalar-test/runtime.rb', line 442

def request_option_hash(options, *keys)
  keys.each do |key|
    value = request_option_value(options, key)
    return value if value.is_a?(Hash)
  end
  {}
end

.request_option_value(options, key) ⇒ Object



450
451
452
453
# File 'lib/amritk-scalar-test/runtime.rb', line 450

def request_option_value(options, key)
  value = request_options_hash(options)
  value[key] || value[key.to_s]
end

.request_options_hash(options) ⇒ Object



436
437
438
439
440
# File 'lib/amritk-scalar-test/runtime.rb', line 436

def request_options_hash(options)
  return {} if options.nil?
  value = options.respond_to?(:to_h) ? options.to_h : options
  value.is_a?(Hash) ? value : {}
end


468
469
470
# File 'lib/amritk-scalar-test/runtime.rb', line 468

def safe_cookie_pair(name, value)
  "#{URI.encode_www_form_component(name.to_s)}=#{URI.encode_www_form_component(safe_header_value(value))}"
end

.safe_header_name(value) ⇒ Object

Raises:

  • (ArgumentError)


461
462
463
464
465
466
# File 'lib/amritk-scalar-test/runtime.rb', line 461

def safe_header_name(value)
  text = value.to_s
  raise ArgumentError, "header names cannot be empty" if text.empty?
  raise ArgumentError, "invalid header name #{text.inspect}" unless text.match?(/\A[!#$%&'*+\-.^_`|~0-9A-Za-z]+\z/)
  text
end

.safe_header_value(value) ⇒ Object

Raises:

  • (ArgumentError)


455
456
457
458
459
# File 'lib/amritk-scalar-test/runtime.rb', line 455

def safe_header_value(value)
  text = value.to_s
  raise ArgumentError, "header values cannot contain CR or LF" if text.match?(/[\r\n]/)
  text
end

.serialize(value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/amritk-scalar-test/runtime.rb', line 22

def serialize(value)
  return value.iso8601 if value.respond_to?(:iso8601)
  return value.to_h if value.respond_to?(:to_h)
  return value.map { |item| serialize(item) } if value.is_a?(Array)
  return value.transform_values { |item| serialize(item) } if value.is_a?(Hash)
  value
end

.value_at(value, path) ⇒ Object



405
406
407
408
409
410
# File 'lib/amritk-scalar-test/runtime.rb', line 405

def value_at(value, path)
  path.reduce(value) do |current, key|
    break nil if current.nil?
    current.is_a?(Hash) ? current[key] || current[key.to_s] : nil
  end
end