Module: Stripe::V2TypeCoercion
- Defined in:
- lib/stripe/v2_type_coercion.rb
Overview
Shared V2 type coercion logic for encoding/decoding values between native Ruby types and their wire representations.
Used by RequestParams (encode: native → wire) and StripeObject (decode: wire → native).
Class Method Summary collapse
-
.coerce_composite(value, encoding, direction) ⇒ Object
--- composite coercions ---.
- .coerce_decimal_string(value, direction) ⇒ Object
-
.coerce_fields(hash, schema, direction:) ⇒ Object
Coerce all fields in a hash according to a schema map.
-
.coerce_int64_string(value, direction) ⇒ Object
--- leaf coercions ---.
- .coerce_object(value, fields_schema, direction) ⇒ Object
-
.coerce_value(value, encoding, direction:) ⇒ Object
Coerce a single value based on its field encoding schema.
Class Method Details
.coerce_composite(value, encoding, direction) ⇒ Object
--- composite coercions ---
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/stripe/v2_type_coercion.rb', line 83 module_function def coerce_composite(value, encoding, direction) case encoding[:kind] when :object coerce_object(value, encoding[:fields] || {}, direction) when :array return value unless value.is_a?(Array) value.map { |v| coerce_value(v, encoding[:element], direction: direction) } else value end end |
.coerce_decimal_string(value, direction) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/stripe/v2_type_coercion.rb', line 61 module_function def coerce_decimal_string(value, direction) case direction when :encode case value when BigDecimal then value.to_s("F") when Integer, Float then value.to_s when Array then value.map { |v| coerce_decimal_string(v, direction) } else value end when :decode case value when String then Kernel.BigDecimal(value) when Array then value.map { |v| coerce_decimal_string(v, direction) } else value end else Kernel.raise ArgumentError, "unknown direction: #{direction.inspect}" end end |
.coerce_fields(hash, schema, direction:) ⇒ Object
Coerce all fields in a hash according to a schema map.
30 31 32 33 34 35 36 37 38 |
# File 'lib/stripe/v2_type_coercion.rb', line 30 module_function def coerce_fields(hash, schema, direction:) return hash unless hash.is_a?(Hash) return hash if schema.nil? || schema.empty? hash.each_with_object({}) do |(k, v), result| field_encoding = schema[k.to_sym] result[k] = field_encoding ? coerce_value(v, field_encoding, direction: direction) : v end end |
.coerce_int64_string(value, direction) ⇒ Object
--- leaf coercions ---
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/stripe/v2_type_coercion.rb', line 42 module_function def coerce_int64_string(value, direction) case direction when :encode case value when Integer then value.to_s when Array then value.map { |v| v.is_a?(Integer) ? v.to_s : v } else value end when :decode case value when String then Kernel.Integer(value) when Array then value.map { |v| v.is_a?(String) ? Kernel.Integer(v) : v } else value end else Kernel.raise ArgumentError, "unknown direction: #{direction.inspect}" end end |
.coerce_object(value, fields_schema, direction) ⇒ Object
96 97 98 99 100 |
# File 'lib/stripe/v2_type_coercion.rb', line 96 module_function def coerce_object(value, fields_schema, direction) return value unless value.is_a?(Hash) coerce_fields(value, fields_schema, direction: direction) end |
.coerce_value(value, encoding, direction:) ⇒ Object
Coerce a single value based on its field encoding schema. direction: :encode (request: native → wire) or :decode (response: wire → native)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/stripe/v2_type_coercion.rb', line 14 module_function def coerce_value(value, encoding, direction:) return value if value.nil? case encoding when :int64_string coerce_int64_string(value, direction) when :decimal_string coerce_decimal_string(value, direction) when Hash coerce_composite(value, encoding, direction) else value end end |