Module: Plaid::Internal::Types::Utils
- Defined in:
- lib/plaid/internal/types/utils.rb
Overview
Utilities for dealing with and checking types
Class Method Summary collapse
- .coerce(target, value, strict: false, preserve_integer_values: false) ⇒ Object
-
.normalize_keys(hash) ⇒ Hash
Converts camelCase keys to snake_case symbols This allows SDK methods to accept both snake_case and camelCase keys e.g., { refundMethod: ... } becomes { refund_method: ... }.
-
.serialize_times(value) ⇒ Object
Converts Time values (including ActiveSupport::TimeWithZone), recursively within hashes and arrays, to RFC 3339 strings with millisecond precision.
- .symbolize_keys(hash) ⇒ Object
-
.unwrap_type(type) ⇒ Object
Resolves a type or type function into a type.
-
.wrap_type(type) ⇒ Proc
Wraps a type into a type function.
Class Method Details
.coerce(target, value, strict: false, preserve_integer_values: false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 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 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 103 104 105 106 |
# File 'lib/plaid/internal/types/utils.rb', line 32 def self.coerce(target, value, strict: false, preserve_integer_values: false) type = unwrap_type(target) # Fast path: exact String/Integer/Float instances are returned as-is, as the branches below would. # Subclasses still go through their `to_s`/`to_i`/`to_f` conversion. return value if EXACT_SCALAR_TYPES.include?(type) && value.instance_of?(type) case type in Array case value when ::Array return type.coerce(value, strict: strict, preserve_integer_values: preserve_integer_values) when Set, ::Hash return coerce(type, value.to_a, preserve_integer_values: preserve_integer_values) end in Hash case value when ::Hash return type.coerce(value, strict: strict, preserve_integer_values: preserve_integer_values) when ::Array return coerce(type, value.to_h, preserve_integer_values: preserve_integer_values) end in Class if type <= NilClass return nil in Class if type <= String case value when String, Symbol, Numeric, TrueClass, FalseClass return value.to_s end in Class if type <= Symbol case value when Symbol, String return value.to_sym end in Class if type <= Integer case value when Numeric, String, Time return value.to_i when Symbol # JSON object keys are parsed as symbols, so numeric map keys arrive as e.g. :"1" parsed = Integer(value.to_s, 10, exception: false) return parsed unless parsed.nil? end in Class if type <= Float return value if preserve_integer_values && value.is_a?(Integer) case value when Numeric, Time, String return value.to_f when Symbol parsed = Float(value.to_s, exception: false) return parsed unless parsed.nil? end in Class if type <= Model case value when type return value when ::Hash return type.coerce(value, strict: strict, preserve_integer_values: preserve_integer_values) end in Module case type in Enum | Union return type.coerce(value, strict: strict, preserve_integer_values: preserve_integer_values) else value # rubocop:disable Lint/Void end else value # rubocop:disable Lint/Void end raise Errors::TypeError, "cannot coerce value of type `#{value.class}` to `#{target}`" if strict value end |
.normalize_keys(hash) ⇒ Hash
Converts camelCase keys to snake_case symbols This allows SDK methods to accept both snake_case and camelCase keys e.g., { refundMethod: ... } becomes { refund_method: ... }
118 119 120 121 122 123 124 125 |
# File 'lib/plaid/internal/types/utils.rb', line 118 def self.normalize_keys(hash) hash.transform_keys do |key| key_str = key.to_s # Convert camelCase to snake_case snake_case = key_str.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase snake_case.to_sym end end |
.serialize_times(value) ⇒ Object
Converts Time values (including ActiveSupport::TimeWithZone), recursively within hashes and arrays, to
RFC 3339 strings with millisecond precision. Time#to_s (e.g. "2026-10-16 01:40:16 UTC") is not RFC 3339.
Date and DateTime values are left unchanged because their to_s output is already ISO 8601.
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/plaid/internal/types/utils.rb', line 133 def self.serialize_times(value) case value when ::Hash value.transform_values { |item| serialize_times(item) } when ::Array value.map { |item| serialize_times(item) } else value.is_a?(::Time) ? value.iso8601(3) : value end end |
.symbolize_keys(hash) ⇒ Object
108 109 110 |
# File 'lib/plaid/internal/types/utils.rb', line 108 def self.symbolize_keys(hash) hash.transform_keys(&:to_sym) end |
.unwrap_type(type) ⇒ Object
Resolves a type or type function into a type
25 26 27 |
# File 'lib/plaid/internal/types/utils.rb', line 25 def self.unwrap_type(type) type.is_a?(Proc) ? type.call : type end |
.wrap_type(type) ⇒ Proc
Wraps a type into a type function
12 13 14 15 16 17 18 19 |
# File 'lib/plaid/internal/types/utils.rb', line 12 def self.wrap_type(type) case type when Proc type else -> { type } end end |