Class: Tapsilat::OrderListResponse
- Inherits:
-
Object
- Object
- Tapsilat::OrderListResponse
- Defined in:
- lib/tapsilat/orders.rb
Overview
Order list response wrapper class to handle paginated API response
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#cancelled_orders ⇒ Object
Get cancelled orders.
- #count ⇒ Object
- #empty? ⇒ Boolean
-
#first_page? ⇒ Boolean
Helper methods for pagination.
- #has_next_page? ⇒ Boolean
- #has_previous_page? ⇒ Boolean
-
#initialize(response_data) ⇒ OrderListResponse
constructor
A new instance of OrderListResponse.
- #last_page? ⇒ Boolean
- #next_page ⇒ Object
-
#orders_with_status(status_code) ⇒ Object
Get orders with specific status.
- #page ⇒ Object
-
#paid_orders ⇒ Object
Get paid orders.
-
#pending_orders ⇒ Object
Get pending orders.
- #per_page ⇒ Object
- #previous_page ⇒ Object
- #rows ⇒ Object
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
- #total ⇒ Object
-
#total_amount ⇒ Object
Get total amount of all orders in the list.
- #total_pages ⇒ Object
-
#total_paid_amount ⇒ Object
Get total paid amount of all orders in the list.
Constructor Details
#initialize(response_data) ⇒ OrderListResponse
608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'lib/tapsilat/orders.rb', line 608 def initialize(response_data) @data = case response_data when String begin JSON.parse(response_data) rescue JSON::ParserError => e raise OrderError, "Invalid JSON response: #{e.message}" end when Hash response_data else raise OrderError, "Invalid response data type: #{response_data.class}" end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
606 607 608 |
# File 'lib/tapsilat/orders.rb', line 606 def data @data end |
Instance Method Details
#cancelled_orders ⇒ Object
Get cancelled orders
684 685 686 |
# File 'lib/tapsilat/orders.rb', line 684 def cancelled_orders rows.select(&:cancelled?) end |
#count ⇒ Object
702 703 704 |
# File 'lib/tapsilat/orders.rb', line 702 def count rows.length end |
#empty? ⇒ Boolean
698 699 700 |
# File 'lib/tapsilat/orders.rb', line 698 def empty? rows.empty? end |
#first_page? ⇒ Boolean
Helper methods for pagination
644 645 646 |
# File 'lib/tapsilat/orders.rb', line 644 def first_page? page == 1 end |
#has_next_page? ⇒ Boolean
652 653 654 |
# File 'lib/tapsilat/orders.rb', line 652 def has_next_page? page < total_pages end |
#has_previous_page? ⇒ Boolean
656 657 658 |
# File 'lib/tapsilat/orders.rb', line 656 def has_previous_page? page > 1 end |
#last_page? ⇒ Boolean
648 649 650 |
# File 'lib/tapsilat/orders.rb', line 648 def last_page? page >= total_pages end |
#next_page ⇒ Object
660 661 662 |
# File 'lib/tapsilat/orders.rb', line 660 def next_page has_next_page? ? page + 1 : nil end |
#orders_with_status(status_code) ⇒ Object
Get orders with specific status
669 670 671 |
# File 'lib/tapsilat/orders.rb', line 669 def orders_with_status(status_code) rows.select { |order| order.status == status_code } end |
#page ⇒ Object
631 632 633 |
# File 'lib/tapsilat/orders.rb', line 631 def page @data['page']&.to_i || 1 end |
#paid_orders ⇒ Object
Get paid orders
674 675 676 |
# File 'lib/tapsilat/orders.rb', line 674 def paid_orders rows.select(&:paid?) end |
#pending_orders ⇒ Object
Get pending orders
679 680 681 |
# File 'lib/tapsilat/orders.rb', line 679 def pending_orders rows.select(&:pending_payment?) end |
#per_page ⇒ Object
635 636 637 |
# File 'lib/tapsilat/orders.rb', line 635 def per_page @data['per_page']&.to_i || 10 end |
#previous_page ⇒ Object
664 665 666 |
# File 'lib/tapsilat/orders.rb', line 664 def previous_page has_previous_page? ? page - 1 : nil end |
#rows ⇒ Object
623 624 625 |
# File 'lib/tapsilat/orders.rb', line 623 def rows (@data['rows'] || []).map { |order_data| OrderResponse.new(order_data) } end |
#to_h ⇒ Object
706 707 708 |
# File 'lib/tapsilat/orders.rb', line 706 def to_h @data end |
#to_json(*args) ⇒ Object
710 711 712 |
# File 'lib/tapsilat/orders.rb', line 710 def to_json(*args) @data.to_json(*args) end |
#total ⇒ Object
627 628 629 |
# File 'lib/tapsilat/orders.rb', line 627 def total @data['total'].to_i end |
#total_amount ⇒ Object
Get total amount of all orders in the list
689 690 691 |
# File 'lib/tapsilat/orders.rb', line 689 def total_amount rows.sum(&:amount) end |
#total_pages ⇒ Object
639 640 641 |
# File 'lib/tapsilat/orders.rb', line 639 def total_pages @data['total_pages'].to_i end |
#total_paid_amount ⇒ Object
Get total paid amount of all orders in the list
694 695 696 |
# File 'lib/tapsilat/orders.rb', line 694 def total_paid_amount rows.sum(&:paid_amount) end |