Last active
October 12, 2015 23:32
-
-
Save dmichael/2ced636f4e759fe36057 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
netsuite_order = JSON.parse(File.read('sales_order.json').gsub("\n","")) | |
order_translator = OrderTranslator.new(netsuite_order) | |
puts order_translator.to_hash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/env ruby | |
require 'json' | |
require 'active_support/time' | |
require 'money' | |
class OrderItemTranslator | |
def initialize(netsuite_order_item) | |
@netsuite_order_item = netsuite_order_item | |
end | |
def to_hash | |
{ | |
amount: amount, | |
quantity: @netsuite_order_item['quantity'] | |
} | |
end | |
def money | |
Money.new(@netsuite_order_item['amount']) | |
end | |
end | |
class OrderTranslator | |
def initialize(netsuite_order) | |
@netsuite_order = netsuite_order | |
end | |
def to_hash | |
{ | |
created_at: created_at, | |
terms: terms, | |
subtotal: subtotal, | |
items: items.map(&:to_hash) | |
} | |
end | |
def created_at | |
Time.parse @netsuite_order['createddate'] | |
end | |
def terms | |
@netsuite_order['terms']['name'] | |
end | |
def subtotal | |
Money.new(@netsuite_order['subtotal'] * 100) | |
end | |
def items | |
@netsuite_order['item'].map {|item| OrderItemTranslator.new(item)} | |
end | |
end | |
netsuite_order = JSON.parse(File.read('order.json').gsub("\n","")) | |
order_translator = OrderTranslator.new(netsuite_order) | |
puts order_translator.to_hash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"custbody_order_state":{ | |
"name":"Sent | |
to Production (Full Approval)" | |
}, | |
"terms":{ | |
"name":"NET 30" | |
}, | |
"createddate":"5/5/2015 | |
6:20 pm", | |
"subtotal":1650, | |
"currencyname":"United States Dollar", | |
"item":[ | |
{ | |
"amount":1650, | |
"quantity":1500 | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment