Class: EwayRapid::Models::LineItem

Inherits:
Object
  • Object
show all
Defined in:
lib/eway_rapid/models/models.rb

Overview

Item information

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



93
94
95
# File 'lib/eway_rapid/models/models.rb', line 93

def description
  @description
end

#quantityObject

Returns the value of attribute quantity.



94
95
96
# File 'lib/eway_rapid/models/models.rb', line 94

def quantity
  @quantity
end

#skuObject

The stock keeping unit used to identify this line item



92
93
94
# File 'lib/eway_rapid/models/models.rb', line 92

def sku
  @sku
end

#taxObject

The tax amount that applies to this line item in cents



100
101
102
# File 'lib/eway_rapid/models/models.rb', line 100

def tax
  @tax
end

#totalObject

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



103
104
105
# File 'lib/eway_rapid/models/models.rb', line 103

def total
  @total
end

#unit_costObject

The unit cost of this line item in cents



97
98
99
# File 'lib/eway_rapid/models/models.rb', line 97

def unit_cost
  @unit_cost
end

Class Method Details

.from_array(array) ⇒ Object



154
155
156
157
158
159
160
161
# 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) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# 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) ⇒ Object



138
139
140
141
# 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) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# 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) ⇒ Object



118
119
120
121
122
123
124
125
# 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

Instance Method Details

#calculate(unit_cost, unit_tax, quantity) ⇒ Object

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

Parameters:

  • unit_cost (Integer)
  • unit_tax (Integer)
  • quantity (Integer)


111
112
113
114
115
116
# 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