class EwayRapid::Models::LineItem

Item information

Attributes

description[RW]
quantity[RW]
sku[RW]

The stock keeping unit used to identify this line item

tax[RW]

The tax amount that applies to this line item in cents

total[RW]

The total amount (including tax) charged for this line item in the cents

unit_cost[RW]

The unit cost of this line item in cents

Public Class Methods

from_array(array) click to toggle source
# File lib/eway_rapid/models/models.rb, line 154
def self.from_array(array)
  line_items = Array.new
  array.each {|line_item_hash|
    obj = from_hash(line_item_hash)
    line_items.push(obj)
  }
  line_items
end
from_hash(hash) click to toggle source
# File lib/eway_rapid/models/models.rb, line 143
def self.from_hash(hash)
  line_item = LineItem.new
  line_item.sku = hash[Constants::SKU]
  line_item.description = hash[Constants::DESCRIPTION]
  line_item.quantity = hash[Constants::QUANTITY]
  line_item.unit_cost = hash[Constants::UNIT_COST]
  line_item.tax = hash[Constants::TAX]
  line_item.total = hash[Constants::TOTAL]
  line_item
end
from_json(json) click to toggle source
# File lib/eway_rapid/models/models.rb, line 138
def self.from_json(json)
  hash = JSON.parse(json)
  from_hash(hash)
end
to_array(array) click to toggle source
# File lib/eway_rapid/models/models.rb, line 127
def self.to_array(array)
  line_items = []
  if array
    array.each {|line_item_hash|
      obj = to_hash(line_item_hash)
      line_items.push(obj)
    }
  end
  line_items
end
to_hash(line_item) click to toggle source
# File lib/eway_rapid/models/models.rb, line 118
def self.to_hash(line_item)
  { Constants::SKU => line_item.sku,
   Constants::DESCRIPTION => line_item.description,
   Constants::QUANTITY => line_item.quantity,
   Constants::UNIT_COST => line_item.unit_cost,
   Constants::TAX => line_item.tax,
   Constants::TOTAL => line_item.total } if line_item
end

Public Instance Methods

calculate(unit_cost, unit_tax, quantity) click to toggle source

Set the line item's values so that the total and tax add up correctly

@param [Integer] unit_cost @param [Integer] unit_tax @param [Integer] quantity

# File lib/eway_rapid/models/models.rb, line 111
def calculate(unit_cost, unit_tax, quantity)
  if unit_cost && unit_tax && quantity
    tax = unit_tax * quantity
    quantity * unit_cost + tax
  end
end