class AmazonOrder::Client

Attributes

options[RW]
session[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/amazon_order/client.rb, line 7
def initialize(options = {})
  @options = options
  @client = AmazonAuth::Client.new(@options)
  extend(AmazonAuth::SessionExtension)
end

Public Instance Methods

base_dir() click to toggle source
# File lib/amazon_order/client.rb, line 13
def base_dir
  options.fetch(:base_dir, 'orders')
end
current_page_node() click to toggle source
# File lib/amazon_order/client.rb, line 122
def current_page_node
  wait_for_selector('.a-pagination .a-selected')
  doc.css('.a-pagination .a-selected a').first
end
fetch_amazon_orders() click to toggle source
# File lib/amazon_order/client.rb, line 33
def fetch_amazon_orders
  sign_in
  go_to_amazon_order_page
  year_to.to_i.downto(year_from.to_i) do |year|
    fetch_orders_for_year(year: year)
  end
end
fetch_orders_for_year(options = {}) click to toggle source
# File lib/amazon_order/client.rb, line 81
def fetch_orders_for_year(options = {})
  year = options.fetch(:year, Time.current.year)
  if switch_year(year)
    save_page_for(year, current_page_node.try!(:text))
    while (node = next_page_node) do
      session.visit node.attr('href')
      save_page_for(year, current_page_node.text)
      break if limit && limit <= current_page_node.text.to_i
    end
  end
end
file_glob_pattern() click to toggle source
# File lib/amazon_order/client.rb, line 51
def file_glob_pattern
  File.join(Capybara.save_path, base_dir, '*html')
end
generate_csv() click to toggle source
# File lib/amazon_order/client.rb, line 55
def generate_csv
  writer.generate_csv
end
go_to_amazon_order_page() click to toggle source
# File lib/amazon_order/client.rb, line 67
def go_to_amazon_order_page
  if doc.css('.cvf-account-switcher').present?
    log "Account switcher page was displayed"
    session.first('.cvf-account-switcher-profile-details').click
    wait_for_selector('#nav-main') # Wait for page loading
  end
  link = links_for('a').find{|link| link =~ %r{/order-history} }
  if link.present?
    session.visit link
  else
    log "Link for order history wasn't found in #{session.current_url}"
  end
end
limit() click to toggle source
# File lib/amazon_order/client.rb, line 25
def limit
  options.fetch(:limit, 5)
end
load_amazon_orders() click to toggle source
# File lib/amazon_order/client.rb, line 41
def load_amazon_orders
  orders = []
  Dir.glob(file_glob_pattern).each do |filepath|
    log "Loading #{filepath}"
    parser = AmazonOrder::Parser.new(filepath)
    orders += parser.orders
  end
  orders.sort_by{|o| -o.fetched_at.to_i }.uniq(&:order_number)
end
next_page_node() click to toggle source
# File lib/amazon_order/client.rb, line 127
def next_page_node
  wait_for_selector('.a-pagination .a-selected')
  doc.css('.a-pagination .a-selected ~ .a-normal').css('a').first
end
number_of_orders() click to toggle source
# File lib/amazon_order/client.rb, line 118
def number_of_orders
  doc.css('#controlsContainer .num-orders').text.strip
end
save_page_for(year, page) click to toggle source
# File lib/amazon_order/client.rb, line 107
def save_page_for(year, page)
  log "Saving year:#{year} page:#{page}"
  path = ['order', year.to_s, "p#{page}", Time.current.strftime('%Y%m%d%H%M%S')].join('-') + '.html'
  session.save_page(File.join(base_dir, path))
end
selected_year() click to toggle source
# File lib/amazon_order/client.rb, line 113
def selected_year
  wait_for_selector('#orderFilter')
  doc.css('#orderFilter option').find{|o| !o.attr('selected').nil? }.attr('value').gsub(/\D+/,'').to_i
end
sign_in() click to toggle source
# File lib/amazon_order/client.rb, line 63
def sign_in
  @client.sign_in
end
switch_year(year) click to toggle source
# File lib/amazon_order/client.rb, line 93
def switch_year(year)
  return true if year.to_i == selected_year
  session.first('.order-filter-dropdown .a-dropdown-prompt').click
  option = session.all('.a-popover-wrapper .a-dropdown-link').find{|e| e.text.gsub(/\D+/,'').to_i == year.to_i }
  return false if option.nil?
  option.click
  sleep 2
  log "Year:#{year} -> #{number_of_orders}"
  true
rescue => e
  puts "#{e.message}\n#{e.backtrace.join("\n")}"
  false
end
writer() click to toggle source
# File lib/amazon_order/client.rb, line 59
def writer
  @_writer ||= AmazonOrder::Writer.new(file_glob_pattern)
end
year_from() click to toggle source
# File lib/amazon_order/client.rb, line 17
def year_from
  options.fetch(:year_from, Time.current.year)
end
year_to() click to toggle source
# File lib/amazon_order/client.rb, line 21
def year_to
  options.fetch(:year_to, Time.current.year)
end