Class: SimMeterAU::Providers::AldiMobile::Parsers::Orders

Inherits:
Object
  • Object
show all
Defined in:
lib/sim_meter_au/providers/aldi_mobile/parsers/orders.rb

Constant Summary collapse

HEADERS =
["Order #", "Number", "Type", "Plan", "Date", "Actions"].freeze

Instance Method Summary collapse

Instance Method Details

#parse(page) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sim_meter_au/providers/aldi_mobile/parsers/orders.rb', line 10

def parse(page)
  table = page.css("table").find do |candidate|
    candidate.css("th").map { |heading| text(heading) } == HEADERS
  end
  return [] unless table

  orders = []
  current = nil

  table.css("tbody tr").each do |row|
    cells = row.css("td").map { |cell| text(cell) }
    if cells.length >= 6
      orders << build_order(current) if current
      current = {
        id: cells[0],
        number: normalize_number(cells[1]),
        type: cells[2],
        plan_name: cells[3],
        date_label: cells[4]
      }
    elsif current && cells.length == 1
      current[:total_cents] = parse_total(cells[0])
      current[:transaction_reference] = cells[0][/Transaction Ref:\s*([^\s]+)/i, 1]
    end
  end

  orders << build_order(current) if current
  orders
end