Skip to content

Instantly share code, notes, and snippets.

@dmichael
Last active October 13, 2015 12:48
Show Gist options
  • Save dmichael/7197b4749ca2dd661cc7 to your computer and use it in GitHub Desktop.
Save dmichael/7197b4749ca2dd661cc7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'active_support/time'
require 'money'
# A class to translate the embedded order items
#
class OrderItemTranslator
# Initialize with the Hash version of the NetSuite JSON
#
def initialize(netsuite_order_item)
@netsuite_order_item = netsuite_order_item
end
# A method to return our translated object. Could be called "translate" I suppose.
#
def to_hash
{
amount: amount,
quantity: @netsuite_order_item['quantity']
}
end
def amount
Money.new(@netsuite_order_item['amount'])
end
end
# A class to translate the NetSuite order into something useable in app-space.
#
class OrderTranslator
# Initialize with the Hash version of the NetSuite JSON
#
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
# The created_at date must be parsed
#
def created_at
Time.parse @netsuite_order['createddate']
end
# The terms are plucked out of the "terms" Hash
#
def terms
@netsuite_order['terms']['name']
end
# Let's use Money here since that is the currency of our application
# with regards to pricing information. Pun intended.
#
def subtotal
Money.new(@netsuite_order['subtotal'] * 100)
end
def items
@netsuite_order['item'].map {|item| OrderItemTranslator.new(item)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment