Module: Spikard::Converters

Defined in:
lib/spikard/converters.rb

Overview

Type conversion utilities for handler parameters

This module handles converting validated JSON data from Rust into Ruby types, particularly for UploadFile instances.

Class Method Summary collapse

Class Method Details

.convert_file_metadata_to_upload_file(file_data) ⇒ UploadFile

Convert file metadata hash to UploadFile instance

Parameters:

  • file_data (Hash)

    File metadata from Rust (filename, content, size, content_type)

Returns:



25
26
27
28
29
30
31
32
33
34
# File 'lib/spikard/converters.rb', line 25

def (file_data)
  UploadFile.new(
    file_data['filename'],
    file_data['content'],
    content_type: file_data['content_type'],
    size: file_data['size'],
    headers: file_data['headers'],
    content_encoding: file_data['content_encoding']
  )
end

.convert_handler_body(body) ⇒ Object

Process handler body parameter, handling UploadFile conversion

This is the main entry point for converting Rust-provided request data into Ruby types. It handles:

  • Single UploadFile

  • Arrays of UploadFile

  • Hashes with UploadFile fields

  • Nested structures

Parameters:

  • body (Object)

    The body parameter from Rust (already JSON-parsed)

Returns:

  • (Object)

    Processed body with UploadFile instances



81
82
83
# File 'lib/spikard/converters.rb', line 81

def convert_handler_body(body)
  process_upload_file_fields(body)
end

.file_metadata?(value) ⇒ Boolean

Check if a value looks like file metadata from Rust

Parameters:

  • value (Object)

    Value to check

Returns:

  • (Boolean)


17
18
19
# File 'lib/spikard/converters.rb', line 17

def file_metadata?(value)
  value.is_a?(Hash) && value.key?('filename') && value.key?('content')
end

.process_upload_file_fields(value) ⇒ Object

Process handler parameters, converting file metadata to UploadFile instances

This method recursively processes the body parameter, looking for file metadata structures and converting them to UploadFile instances.

Parameters:

  • value (Object)

    The value to process (can be Hash, Array, or primitive)

Returns:

  • (Object)

    Processed value with UploadFile instances



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spikard/converters.rb', line 43

def process_upload_file_fields(value)
  # Handle nil
  return value if value.nil?

  # Handle primitives (String, Numeric, Boolean)
  return value unless value.is_a?(Hash) || value.is_a?(Array)

  # Handle arrays - recursively process each element
  if value.is_a?(Array)
    return value.map do |item|
      # Check if this array item is file metadata
      if file_metadata?(item)
        (item)
      else
        # Recursively process nested arrays/hashes
        process_upload_file_fields(item)
      end
    end
  end

  # Handle hashes - check if it's file metadata first
  return (value) if file_metadata?(value)

  # Otherwise, recursively process hash values
  value.transform_values { |v| process_upload_file_fields(v) }
end