Skip to content

Instantly share code, notes, and snippets.

@dmichael
Last active October 13, 2015 13:21
Show Gist options
  • Save dmichael/1fb1f657e05acc710f25 to your computer and use it in GitHub Desktop.
Save dmichael/1fb1f657e05acc710f25 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'active_support/time'
require 'money'
require 'hashie'
class Translator < Hashie::Dash
include Hashie::Extensions::IgnoreUndeclared
include Hashie::Extensions::Dash::PropertyTranslation
include Hashie::Extensions::Coercion
end
class OrderItemTranslator < Translator
property :amount, from: 'amount', with: -> (value) { Money.new(value * 100) }
property :quantity, from: 'quantity'
end
class OrderTranslator < Translator
property :created_at, from: 'createddate', with: -> (value) { Time.parse(value) }
property :terms, from: 'terms', with: -> (value) { value['name'] }
property :subtotal, from: 'subtotal', with: -> (value) { Money.new(value * 100) }
property :items, from: 'item'
coerce_key :items, Array[OrderItemTranslator]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment