Class: Tapsilat::OrderListResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/tapsilat/orders.rb

Overview

Order list response wrapper class to handle paginated API response

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (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_ordersObject

Get cancelled orders



684
685
686
# File 'lib/tapsilat/orders.rb', line 684

def cancelled_orders
  rows.select(&:cancelled?)
end

#countObject



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_pageObject



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

#pageObject



631
632
633
# File 'lib/tapsilat/orders.rb', line 631

def page
  @data['page']&.to_i || 1
end

Get paid orders



674
675
676
# File 'lib/tapsilat/orders.rb', line 674

def paid_orders
  rows.select(&:paid?)
end

#pending_ordersObject

Get pending orders



679
680
681
# File 'lib/tapsilat/orders.rb', line 679

def pending_orders
  rows.select(&:pending_payment?)
end

#per_pageObject



635
636
637
# File 'lib/tapsilat/orders.rb', line 635

def per_page
  @data['per_page']&.to_i || 10
end

#previous_pageObject



664
665
666
# File 'lib/tapsilat/orders.rb', line 664

def previous_page
  has_previous_page? ? page - 1 : nil
end

#rowsObject



623
624
625
# File 'lib/tapsilat/orders.rb', line 623

def rows
  (@data['rows'] || []).map { |order_data| OrderResponse.new(order_data) }
end

#to_hObject



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

#totalObject



627
628
629
# File 'lib/tapsilat/orders.rb', line 627

def total
  @data['total'].to_i
end

#total_amountObject

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_pagesObject



639
640
641
# File 'lib/tapsilat/orders.rb', line 639

def total_pages
  @data['total_pages'].to_i
end

#total_paid_amountObject

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