Class: GymFinder::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gym_finder/parser.rb

Instance Method Summary collapse

Instance Method Details

#parse_calendar(html) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/gym_finder/parser.rb', line 24

def parse_calendar(html)
  doc = Nokogiri::HTML(html)
  dates_selector = 'td[bgcolor="#87C675"]'
  Calendar.new.tap do |calendar|
    calendar.available_dates = doc.css(dates_selector).map do |node|
      Date.strptime(node.at_css('img')['onclick'][%r{\d{4}/\d{2}/\d{2}}], '%Y/%m/%d')
    end
  end
end

#parse_reservation(html) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gym_finder/parser.rb', line 11

def parse_reservation(html)
  doc = Nokogiri::HTML(html)
  selector = 'img[onclick*="net_booking"]'
  Reservation.new.tap do |reservation|
    reservation.available_courts = doc.css(selector).map do |node|
      Reservation::Court.new.tap do |court|
        court.name = node['title']
        court.pt = node['onclick'][/PT=(\d+)/, 1]
      end
    end
  end
end

#parse_time_table(html) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gym_finder/parser.rb', line 34

def parse_time_table(html)
  doc = Nokogiri::HTML(html)
  TimeTable.new.tap do |table|
    table.time_slots = doc.css('#ContentPlaceHolder1_Step2_data tr:not(:first-child)').map do |row|
      TimeTable::TimeSlot.new.tap do |time_slot|
        time_slot.time = row.at_css('td:first-child').text[/(\d+):\d+~\d+\d+/, 1].to_i
        time_slot.court = row.at_css('td:nth-child(2)').text
        time_slot.price = row.at_css('td:nth-child(3)').text.to_i
        img = row.at_css('td:last-child > img')
        img_src = img['src']
        case img_src
        when 'img/sche01.png'
          time_slot.status = 'available'
          time_slot.qpid = img['onclick'][/Step3Action\((\d+),\d+\)/, 1]
        when 'img/sche02.jpg'
          time_slot.status = 'reserved'
        else
          warn "unknown status: #{img_src}"
          'unknown'
        end
      end
    end
  end
end