Is it possible to create objects from multiple keys? For example, assuming I’m working with charges (payments) for a service and I’ve made a http request and received a hash like so:
{
"charge": {
"created_at":"2017-05-01 16:31:33 +0800",
"amount":100,
"currency":"USD"
}
}
I want to convert this into a Charge object with a Money object storing the amount and currency.
I can create the Money struct easily enough:
class Money < Dry::Struct
attribute :amount, Types::Int
attribute :currency, Types::String
end
I’d like to create a Charge object using something like:
response = http_get("http://...")
charge = Charge.new(response, JSON.parse(response.body))
To do this I am using dry-initializer:
class Charge
extend Dry::Initializer
param :response, Types::Object
option :created_at, Types::Json::DateTime
end
But I don’t know how to create an instance of Money from the amount and currency keys. I want to store the response object so that I can interrigate it if necessary (https status codes etc.).
Thanks.
Steve.