Class: Cryptopay::Encoder
- Inherits:
-
Object
- Object
- Cryptopay::Encoder
- Defined in:
- lib/cryptopay/encoder.rb
Instance Method Summary collapse
- #build_from_hash(data) ⇒ Object
-
#initialize(name:, attribute_map:, types:, nullables:) ⇒ Encoder
constructor
A new instance of Encoder.
- #sanitize(attributes) ⇒ Object
- #to_hash(attributes) ⇒ Object
Constructor Details
#initialize(name:, attribute_map:, types:, nullables:) ⇒ Encoder
Returns a new instance of Encoder.
5 6 7 8 9 10 |
# File 'lib/cryptopay/encoder.rb', line 5 def initialize(name:, attribute_map:, types:, nullables:) @name = name @attribute_map = attribute_map @types = types @nullables = nullables end |
Instance Method Details
#build_from_hash(data) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cryptopay/encoder.rb', line 26 def build_from_hash(data) types.each_with_object({}) do |(key, type), attributes| value = data[attribute_map[key]] if value.nil? attributes[key] = nil if nullables.include?(key) elsif type =~ /\AArray<(.*)>/i attributes[key] = value.map { |v| _deserialize(Regexp.last_match(1), v) } else attributes[key] = _deserialize(type, value) end end end |
#sanitize(attributes) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cryptopay/encoder.rb', line 12 def sanitize(attributes) attributes.each_with_object({}) do |(k, v), h| unless attribute_map.key?(k.to_sym) raise( ArgumentError, "`#{k}` is not a valid attribute in `#{name}`. " \ "Please check the name to make sure it's valid. List of attributes: " + attribute_map.keys.inspect ) end h[k.to_sym] = v end end |
#to_hash(attributes) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cryptopay/encoder.rb', line 40 def to_hash(attributes) attribute_map.each_with_object({}) do |(attr, param), hash| value = attributes[attr] if value.nil? nullable = nullables.include?(attr) # Skip non-nullable attributes with nil values next unless nullable # Skip nullable attributes which was not explicitly set next unless attributes.key?(attr) end hash[param] = _to_hash(value) end end |