Class: AmazonOrder::Client

Inherits:
Object
  • Object
show all
Includes:
AmazonAuth::CommonExtension
Defined in:
lib/amazon_order/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/amazon_order/client.rb', line 7

def initialize(options = {})
  @options = options
  @client = AmazonAuth::Client.new(@options)
  extend(AmazonAuth::SessionExtension)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/amazon_order/client.rb', line 5

def options
  @options
end

#sessionObject

Returns the value of attribute session.



5
6
7
# File 'lib/amazon_order/client.rb', line 5

def session
  @session
end

Instance Method Details

#base_dirObject



13
14
15
# File 'lib/amazon_order/client.rb', line 13

def base_dir
  options.fetch(:base_dir, 'orders')
end

#current_page_nodeObject



122
123
124
125
# 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_ordersObject



33
34
35
36
37
38
39
# File 'lib/amazon_order/client.rb', line 33

def fetch_amazon_orders
  
  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 = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# 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_patternObject



51
52
53
# File 'lib/amazon_order/client.rb', line 51

def file_glob_pattern
  File.join(Capybara.save_path, base_dir, '*html')
end

#generate_csvObject



55
56
57
# File 'lib/amazon_order/client.rb', line 55

def generate_csv
  writer.generate_csv
end

#go_to_amazon_order_pageObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#limitObject



25
26
27
# File 'lib/amazon_order/client.rb', line 25

def limit
  options.fetch(:limit, 5)
end

#load_amazon_ordersObject



41
42
43
44
45
46
47
48
49
# 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_nodeObject



127
128
129
130
# 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_ordersObject



118
119
120
# 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) ⇒ Object



107
108
109
110
111
# 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_yearObject



113
114
115
116
# File 'lib/amazon_order/client.rb', line 113

def selected_year
  wait_for_selector('#time-filter, #orderFilter')
  doc.css('#time-filter option, #orderFilter option').find{|o| !o.attr('selected').nil? }.attr('value').gsub(/\D+/,'').to_i
end

#sign_inObject



63
64
65
# File 'lib/amazon_order/client.rb', line 63

def 
  @client.
end

#switch_year(year) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/amazon_order/client.rb', line 93

def switch_year(year)
  return true if year.to_i == selected_year
  session.first('.a-dropdown-container .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

#writerObject



59
60
61
# File 'lib/amazon_order/client.rb', line 59

def writer
  @_writer ||= AmazonOrder::Writer.new(file_glob_pattern)
end

#year_fromObject



17
18
19
# File 'lib/amazon_order/client.rb', line 17

def year_from
  options.fetch(:year_from, Time.current.year)
end

#year_toObject



21
22
23
# File 'lib/amazon_order/client.rb', line 21

def year_to
  options.fetch(:year_to, Time.current.year)
end