Sha256: 0b92f6198d6d8381471adec35438c8ea9682a58d8804ce730718d95b901a1b89

Contents?: true

Size: 1.68 KB

Versions: 15

Compression:

Stored size: 1.68 KB

Contents

module AmazonSellerCentral
  class OrdersPage < Page
    attr_accessor :body
    
    def initialize
      @page_no = 0
    end

    def self.has_next?(page)
      has_next ||= page.search(".//div[@id='nextPage']").any?
    end

    def self.next_page(page, mech, uri_base)
      next_page ||= begin
                       raise NoNextPageAvailableError unless has_next?

                       next_page = mech.agent.get("#{uri_base}&searchPageOffset=#{@page_no + 1}")
                     end
    end

    def self.pending_orders
      uri_base = 'https://sellercentral.amazon.com/gp/orders-v2/list/ref=ag_myo_dos4_home?ie=UTF8&showCancelled=0&searchType=OrderStatus&ignoreSearchType=1&statusFilter=Pending&searchFulfillers=mfn&preSelectedRange=30&searchDateOption=preSelected&sortBy=OrderStatusDescending&itemsPerPage=100'
      
      mech = AmazonSellerCentral.mechanizer
      mech.login_to_seller_central
      page = mech.agent.get(uri_base)
      pending_orders = []
      begin
        (page.parser.css('tr.list-row-odd') + page.parser.css('tr.list-row-even')).each do |row|
          pending_orders << order_row_to_object(row)
        end
        if has_next?(page)
          page = next_page(page, mech, uri_base)
          had_next = true
        else
          had_next = false
        end
      end while had_next
      pending_orders
    end

    def self.order_row_to_object(row)
      o = Order.new
      row.css('td').each_with_index do |td, i|
        txt = td.text.strip
        case i
        when 1
          o.date = Time.parse(txt)
        when 4
          o.order_id = txt.match(/^(\d{3}-\d+-\d+)\s/)[1]
        when 12
          o.status = txt
        end
      end
      o
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
amazon_seller_central-0.3.1 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.3.0 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.16 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.15 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.14 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.13 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.12 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.11 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.10 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.9 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.8 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.7 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.6 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.5 lib/amazon_seller_central/orders_page.rb
amazon_seller_central-0.2.4 lib/amazon_seller_central/orders_page.rb