Sha256: 36b381ddb18ce400559829a25d0feb2b4aa572814d0737c885bfe7e07eaecf9a
Contents?: true
Size: 1.34 KB
Versions: 6
Compression:
Stored size: 1.34 KB
Contents
# frozen_string_literal: true require 'json' # The base class for all JSON objects in the library. class EasyPost::Models::Object def initialize(data) @values = data add_properties(data) end # Convert to a string. def to_s(*_args) JSON.dump(@values) end # Convert object to hash def to_hash JSON.parse(JSON.dump(@values)) end # Get element of an array. def [](key) @values[key.to_s] end # Set the element of an array. def []=(key, value) send(:"#{key}=", value) end private def add_properties(values) values.each do |key, _| define_singleton_method(key) { handle_value(@values[key]) } # getter define_singleton_method("#{key}=") { |v| @values[key] = handle_value(v) } # setter end end def handle_value(val) case val when Hash type = EasyPost::InternalUtilities::StaticMapper::BY_TYPE[val['object']] if val['object'] prefix = EasyPost::InternalUtilities::StaticMapper::BY_PREFIX[val['id'].split('_').first] if val['id'] cls = type || prefix || EasyPost::Models::EasyPostObject cls.new(val) when Array val.map { |item| handle_value(item) } else val end end end # The base class for all API objects in the library that have an ID (plus optional timestamps). class EasyPost::Models::EasyPostObject < EasyPost::Models::Object end
Version data entries
6 entries across 6 versions & 1 rubygems