Class: Moltin::Resource::Cart
- Inherits:
-
Object
- Object
- Moltin::Resource::Cart
- Defined in:
- lib/moltin/resource/cart.rb
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#item_count ⇒ Object
readonly
Returns the value of attribute item_count.
-
#item_subtotal ⇒ Object
readonly
Returns the value of attribute item_subtotal.
-
#item_tax ⇒ Object
readonly
Returns the value of attribute item_tax.
-
#item_total ⇒ Object
readonly
Returns the value of attribute item_total.
Instance Method Summary collapse
- #add_item(options) ⇒ Object
- #checkout ⇒ Object
- #destroy ⇒ Object
-
#initialize(identifier = nil) ⇒ Cart
constructor
A new instance of Cart.
- #items ⇒ Object
- #remove_item(product_identifier) ⇒ Object
- #retrieve ⇒ Object
Constructor Details
#initialize(identifier = nil) ⇒ Cart
16 17 18 19 20 21 22 |
# File 'lib/moltin/resource/cart.rb', line 16 def initialize(identifier = nil) @identifier = identifier || SecureRandom.hex(12) @item_count = 0 @item_tax = 0 @item_total = 0 @item_subtotal = 0 end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
10 11 12 |
# File 'lib/moltin/resource/cart.rb', line 10 def identifier @identifier end |
#item_count ⇒ Object (readonly)
Returns the value of attribute item_count.
11 12 13 |
# File 'lib/moltin/resource/cart.rb', line 11 def item_count @item_count end |
#item_subtotal ⇒ Object (readonly)
Returns the value of attribute item_subtotal.
14 15 16 |
# File 'lib/moltin/resource/cart.rb', line 14 def item_subtotal @item_subtotal end |
#item_tax ⇒ Object (readonly)
Returns the value of attribute item_tax.
12 13 14 |
# File 'lib/moltin/resource/cart.rb', line 12 def item_tax @item_tax end |
#item_total ⇒ Object (readonly)
Returns the value of attribute item_total.
13 14 15 |
# File 'lib/moltin/resource/cart.rb', line 13 def item_total @item_total end |
Instance Method Details
#add_item(options) ⇒ Object
42 43 44 |
# File 'lib/moltin/resource/cart.rb', line 42 def add_item() Moltin::Api::Request.post("carts/#{identifier}", ) end |
#checkout ⇒ Object
34 35 36 |
# File 'lib/moltin/resource/cart.rb', line 34 def checkout Moltin::Resource::Checkout.new(cart: self).retrieve end |
#destroy ⇒ Object
38 39 40 |
# File 'lib/moltin/resource/cart.rb', line 38 def destroy Moltin::Api::Request.delete("carts/#{identifier}") end |
#items ⇒ Object
50 51 52 |
# File 'lib/moltin/resource/cart.rb', line 50 def items @items || [] end |
#remove_item(product_identifier) ⇒ Object
46 47 48 |
# File 'lib/moltin/resource/cart.rb', line 46 def remove_item(product_identifier) Moltin::Api::Request.delete("carts/#{identifier}/item/#{product_identifier}") end |
#retrieve ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/moltin/resource/cart.rb', line 24 def retrieve response = Moltin::Api::Request.get("carts/#{identifier}") return unless response.success? @items = Moltin::ResourceCollection.new 'Moltin::Resource::Product', response.result['contents'].map { |k, v| v.merge('identifier' => k) } @item_count = response.result['total_items'] @item_subtotal = response.result['totals']['pre_discount']['formatted']['without_tax'] @item_tax = response.result['totals']['pre_discount']['formatted']['tax'] @item_total = response.result['totals']['pre_discount']['formatted']['with_tax'] end |