-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitreserve.rb
120 lines (96 loc) · 3.18 KB
/
fitreserve.rb
1
2
3
4
5
6
7
8
9
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'open-uri'
module Scraper
class Fitreserve
attr_reader :url, :page, :studio, :classes
def initialize(url)
@url = url
@page = Nokogiri::HTML(open(url))
@studio = Studio.find_by(schedule_url: url)
@classes = []
end
def parse_classes
page.css('.row.schedule').each do |day|
date = parse_date(day.css('.date').text)
day.css('tr').each do |row|
time_string = row.css('.time').text
if time_string.downcase != 'time' &&
row.css('.reserve a').text.downcase == 'reserve'
time = parse_time(time_string.split(' - '))
klass = {
date: date,
start_time: time[:start_time],
end_time: time[:end_time],
name: format_class(row.css('.name').text),
instructor: row.css('.instructor').text,
duration: time[:duration],
studio_id: studio.id
}
classes.push(Klass.create_from_raw(klass))
end
end
end
classes
end
def parse_studio
name = page.css('.details .name').text
logo = page.css('.details .logo').last.attributes['src'].value
address = page.css('.details .address').text.strip.gsub("\n",' ')
studio_params = {
name: name,
address: address,
logo: logo
}
if @studio.blank?
@studio = Studio.create(studio_params)
self.parse_classes
elsif studio.updated_at.nil? || studio.updated_at < Time.now - 21600
self.parse_classes
@studio.update(studio_params)
end
# Invalidate studio cache
keys = [
"studios/#{studio.id}/classes", "studios/#{studio.id}/unique_classes"
]
keys.each { |key| Rails.cache.delete(key) }
studio
end
def parse_time(str)
end_modifier = str.last.split(' ').last
end_time = Time.parse(str.last)
start_time = Time.parse(str.first + ' ' + end_modifier)
duration = (end_time - start_time) / 60
if duration > 200 || duration < 0
start_modifier = end_modifier == 'PM' ? 'AM' : 'PM'
start_time = Time.parse(str.first + ' ' + start_modifier)
duration = (end_time - start_time) / 60
end
{
start_time: start_time.strftime('%l:%M %p').strip,
end_time: end_time.strftime('%l:%M %p').strip,
duration: duration
}
end
def parse_date(date_string)
date = date_string.split(', ').last
month = Date.parse(date).strftime('%m')
current_year = Date.today.strftime('%Y')
current_month = Date.today.strftime('%m')
if current_month == '12' && month == '01'
year = (current_year.to_i + 1).to_s
else
year = current_year
end
Date.parse(date + ' ' + year)
end
def format_class(class_string)
matches = (class_string || '').downcase.capitalize.scan(/\W[a-z]/)
matches.each do |letter|
class_string.gsub!(letter, letter.upcase)
end
class_string
end
def self.sanitize_level(level)
level.gsub(' to','').gsub(')','').capitalize
end
end
end