Class: Compactor::Amazon::ReportScraper

Inherits:
Object
  • Object
show all
Defined in:
lib/compactor/scraper.rb

Instance Method Summary collapse

Constructor Details

#initialize(user_credentials = {}) ⇒ ReportScraper

Returns a new instance of ReportScraper.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/compactor/scraper.rb', line 24

def initialize(user_credentials={})
  @mechanize = agent
  @mechanize.max_file_buffer               = 4 * 1024 * 1024
  @mechanize.max_history                   = 2
  @mechanize.agent.http.verify_mode        = OpenSSL::SSL::VERIFY_NONE
  @mechanize.agent.http.reuse_ssl_sessions = false
  @validate_totals                         = user_credentials[:validate_totals]

  randomize_user_agent!
   user_credentials[:email], user_credentials[:password]
end

Instance Method Details

#buyer_nameObject



92
93
94
95
96
97
98
# File 'lib/compactor/scraper.rb', line 92

def buyer_name
  tr = @mechanize.page.search!("//tr[@class='list-row']/td[@class='data-display-field'][text()=\"Contact Buyer:\"]").first.parent
  td = tr.search!("td[2]")
  td.text.strip
rescue => e
  ""
end

#get_balanceObject



78
79
80
81
82
83
84
# File 'lib/compactor/scraper.rb', line 78

def get_balance
  go_to_past_settlements('', '')
  return 0.0 if page_has_no_results?
  open_row = report_rows.detect { |row| row.not_settled_report? }

  open_row.nil? ? 0.0 : open_row.deposit_amount
end

#get_marketplacesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/compactor/scraper.rb', line 51

def get_marketplaces
  @mechanize.get MARKETPLACE_HOMEPAGE

  marketplace_selector = @mechanize.page.search("#marketplaceSelect, #sc-mkt-switcher-select").first
  if marketplace_selector
    result = []
    marketplace_selector.search("option").each do |ele|
      name = ele.text
      marketplace_id = ele["value"]
      result << [ name, marketplace_id ]
    end
    return result
  end

  marketplace_name = @mechanize.page.search("#market_switch .merch-site-span, #sc-mkt-switcher-form .sc-mkt-switcher-txt")
  if marketplace_name
    return [ [ marketplace_name.text.strip, nil ] ]
  end

  return []
end

#get_orders(order_ids) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/compactor/scraper.rb', line 118

def get_orders(order_ids)
  orders_hash = {}
  order_ids.each do |order_id|
    orders_hash[order_id] = payee_details(order_id)
  end
  orders_hash
end

#marketplacesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/compactor/scraper.rb', line 36

def marketplaces
  marketplaces = wait_for_element { get_marketplaces }
  raise MissingMarketplaceError if marketplaces.blank?

  marketplaces = filter_marketplaces(marketplaces)
  raise NoMarketplacesError if marketplaces.empty?

  marketplaces.map do |, marketplace_id|
    select_marketplace(marketplace_id)
    balance = get_balance

    [ , marketplace_id, balance ]
  end
end

#payee_details(order_id) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/compactor/scraper.rb', line 108

def payee_details(order_id)
  @mechanize.get order_detail_url(order_id)
  order = {}
  order["BuyerName"]       = buyer_name
  order["ShippingAddress"] = shipping_address
  order
rescue => e
  nil
end

#reports(from, to) ⇒ Object



86
87
88
89
90
# File 'lib/compactor/scraper.rb', line 86

def reports(from, to)
  from, to = parse_dates(from, to)
  go_to_past_settlements(from, to)
  get_reports
end

#select_marketplace(marketplace_id) ⇒ Object



73
74
75
76
# File 'lib/compactor/scraper.rb', line 73

def select_marketplace(marketplace_id)
  marketplace_id = CGI.escape(marketplace_id)
  @mechanize.get "https://sellercentral.amazon.com/gp/utilities/set-rainier-prefs.html?ie=UTF8&&marketplaceID=#{marketplace_id}"
end

#shipping_addressObject



100
101
102
103
104
105
106
# File 'lib/compactor/scraper.rb', line 100

def shipping_address
  td = @mechanize.page.search!("//tr[@class='list-row']/td[@class='data-display-field']/strong[text()='Shipping Address:']").first.parent
  addr_lines = td.children.map(&:text).reject { |l| l.blank? || l =~ /^Shipping Address/ }
  parse_address_lines!(addr_lines)
rescue => e
  ""
end