Class: Compactor::Amazon::ScrapedRow

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

Instance Method Summary collapse

Constructor Details

#initialize(node, mechanize) ⇒ ScrapedRow

Returns a new instance of ScrapedRow.



4
5
6
7
# File 'lib/compactor/scraped_row.rb', line 4

def initialize(node, mechanize)
  @node      = node
  @mechanize = mechanize
end

Instance Method Details

#can_download_report?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/compactor/scraped_row.rb', line 9

def can_download_report?
  !report_buttons.blank?
end

#date_rangeObject



79
80
81
# File 'lib/compactor/scraped_row.rb', line 79

def date_range
  @date_range ||= @node.search("td:first-child a").text
end

#deposit_amountObject



74
75
76
77
# File 'lib/compactor/scraped_row.rb', line 74

def deposit_amount
  @deposit_amount = fetch_deposit_amount if !@deposit_amount
  @deposit_amount
end

#download_reportObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/compactor/scraped_row.rb', line 28

def download_report
  buttons           = report_buttons
  button_index      = index_of_button(buttons)
  report_url        = buttons[button_index].node["href"]
  report_identifier = buttons[button_index].node.search(".button_label").text
  type              = ReportScraper.report_type(report_identifier)
  response_body     = @mechanize.get(report_url).body

  [type, response_body]
end

#download_report!(validate = false) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/compactor/scraped_row.rb', line 19

def download_report!(validate=false)
  r_type, r_data = download_report

  # fail if Amazon is saying that total is X but the calculated total is Y
  validate!(r_type, r_data) if validate

  [r_type, r_data]
end

#not_settled_report?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/compactor/scraped_row.rb', line 65

def not_settled_report?
  text = last_div.text

  # Is the report not settled yet? (in pending-like state)
  ["(Processing)", "(Open)", "In Progress"].any? do |report_state|
    text.include?(report_state)
  end
end

#reloadObject



39
40
41
42
43
44
45
46
# File 'lib/compactor/scraped_row.rb', line 39

def reload
  table_rows.each do |row|
    row = ScrapedRow.new(row, @mechanize)
    return row if row.date_range == date_range
  end

  nil
end

#report_buttonsObject



13
14
15
16
17
# File 'lib/compactor/scraped_row.rb', line 13

def report_buttons
  last_cell.search(".secondarySmallButton").map do |ele|
    Mechanize::Page::Link.new(ele.parent, @mechanize, @mechanize.page)
  end
end

#request_reportObject



48
49
50
51
52
53
54
55
# File 'lib/compactor/scraped_row.rb', line 48

def request_report
  button    = last_cell.search(".regenerateButton")[0]
  button_id = button['id']

  @mechanize.post("/gp/payments-account/redrive.html", {
    "groupId" => button_id
  })
end

#requestable_report?Boolean

A settlement period (row) is considered ready to be parsed if it’s not processing, open or in progress. Also the “regenerate” button is not present. This means that all is left is 1 or more buttons to get the actual reports

Returns:

  • (Boolean)


61
62
63
# File 'lib/compactor/scraped_row.rb', line 61

def requestable_report?
  !last_cell.search(".regenerateButton").empty?
end