Class: AmazonOrder::Client
- Inherits:
-
Object
- Object
- AmazonOrder::Client
- Includes:
- AmazonAuth::CommonExtension
- Defined in:
- lib/amazon_order/client.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
- #base_dir ⇒ Object
- #current_page_node ⇒ Object
- #fetch_amazon_orders ⇒ Object
- #fetch_orders_for_year(options = {}) ⇒ Object
- #file_glob_pattern ⇒ Object
- #generate_csv ⇒ Object
- #go_to_amazon_order_page ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #limit ⇒ Object
- #load_amazon_orders ⇒ Object
- #next_page_node ⇒ Object
- #number_of_orders ⇒ Object
- #save_page_for(year, page) ⇒ Object
- #selected_year ⇒ Object
- #sign_in ⇒ Object
- #switch_year(year) ⇒ Object
- #writer ⇒ Object
- #year_from ⇒ Object
- #year_to ⇒ Object
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 = @client = AmazonAuth::Client.new(@options) extend(AmazonAuth::SessionExtension) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/amazon_order/client.rb', line 5 def @options end |
#session ⇒ Object
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_dir ⇒ Object
13 14 15 |
# File 'lib/amazon_order/client.rb', line 13 def base_dir .fetch(:base_dir, 'orders') end |
#current_page_node ⇒ Object
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_orders ⇒ Object
33 34 35 36 37 38 39 |
# 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 = {}) ⇒ 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( = {}) year = .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 ⇒ Object
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_csv ⇒ Object
55 56 57 |
# File 'lib/amazon_order/client.rb', line 55 def generate_csv writer.generate_csv end |
#go_to_amazon_order_page ⇒ Object
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 |
#limit ⇒ Object
25 26 27 |
# File 'lib/amazon_order/client.rb', line 25 def limit .fetch(:limit, 5) end |
#load_amazon_orders ⇒ Object
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_node ⇒ Object
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_orders ⇒ Object
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_year ⇒ Object
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_in ⇒ Object
63 64 65 |
# File 'lib/amazon_order/client.rb', line 63 def sign_in @client.sign_in 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.}\n#{e.backtrace.join("\n")}" false end |
#writer ⇒ Object
59 60 61 |
# File 'lib/amazon_order/client.rb', line 59 def writer @_writer ||= AmazonOrder::Writer.new(file_glob_pattern) end |
#year_from ⇒ Object
17 18 19 |
# File 'lib/amazon_order/client.rb', line 17 def year_from .fetch(:year_from, Time.current.year) end |
#year_to ⇒ Object
21 22 23 |
# File 'lib/amazon_order/client.rb', line 21 def year_to .fetch(:year_to, Time.current.year) end |