Class: Pagaris::Order
- Inherits:
-
Object
- Object
- Pagaris::Order
- Defined in:
- lib/pagaris/order.rb
Overview
Constant Summary collapse
- READABLE_ATTRS =
%i(id fee payout_amount status created_at updated_at url)
- WRITABLE_ATTRS =
%i(amount metadata products redirect_url)
- ALL_ATTRS =
READABLE_ATTRS + WRITABLE_ATTRS
Class Method Summary collapse
- .all ⇒ Object
- .create(amount:, metadata: nil, products: nil, redirect_url: nil) ⇒ Object
- .get(id) ⇒ Object
Instance Method Summary collapse
- #cancel ⇒ Object
- #confirm ⇒ Object
- #create ⇒ Object
-
#initialize(amount:, metadata: nil, products: nil, redirect_url: nil) ⇒ Order
constructor
A new instance of Order.
Constructor Details
#initialize(amount:, metadata: nil, products: nil, redirect_url: nil) ⇒ Order
Returns a new instance of Order.
13 14 15 16 17 18 |
# File 'lib/pagaris/order.rb', line 13 def initialize(amount:, metadata: nil, products: nil, redirect_url: nil) @amount = amount @metadata = @products = products @redirect_url = redirect_url end |
Class Method Details
.all ⇒ Object
57 58 59 60 61 |
# File 'lib/pagaris/order.rb', line 57 def self.all Client.get('orders')[:orders].map do |order| self.new(amount: order[:amount]).send(:update_from_response, order) end end |
.create(amount:, metadata: nil, products: nil, redirect_url: nil) ⇒ Object
51 52 53 54 55 |
# File 'lib/pagaris/order.rb', line 51 def self.create(amount:, metadata: nil, products: nil, redirect_url: nil) order = self.new(amount: amount, metadata: , products: products, redirect_url: redirect_url) order.create end |
Instance Method Details
#cancel ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/pagaris/order.rb', line 41 def cancel if !defined?(@id) raise 'Cannot call `#cancel` on a non-persisted `Order`. Maybe you '\ 'have not called `#create` yet?' end response_order = Client.put("orders/#{@id}/cancel")[:order] update_from_response(response_order) end |
#confirm ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/pagaris/order.rb', line 31 def confirm if !defined?(@id) raise 'Cannot call `#confirm` on a non-persisted `Order`. Maybe you '\ 'have not called `#create` yet?' end response_order = Client.put("orders/#{@id}/confirm")[:order] update_from_response(response_order) end |
#create ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pagaris/order.rb', line 20 def create if defined?(@id) raise 'Cannot call `#create` on an `Order` which already has an `id`' end ord_attrs = WRITABLE_ATTRS.map { |a| [a, instance_variable_get("@#{a}")] } body = { order: Hash[ord_attrs] } response_order = Client.post('orders', body)[:order] update_from_response(response_order) end |