-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.rb
84 lines (67 loc) · 3.11 KB
/
service.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
require 'time'
class Service
NORMAL_SERVICE = 0
SAILINGS_DISRUPTED = 1
SAILINGS_CANCELLED = 2
UNKNOWN = -99
attr_accessor :service_id, :updated, :sort_order, :area, :route, :status, :additional_info, :disruption_reason, :disruption_date, :disruption_details
def updated=(value)
# Make sure that updated is always in utc
@updated = value.utc if value
end
def disruption_date=(value)
# Make sure that disruption date is always in utc
@disruption_date = value.utc if value
end
def self.fetch_all(client)
services = []
client.query("SELECT * FROM services").each { |row|
services << create_service(row)
}
services
end
def self.fetch(client, id)
statement = client.prepare('SELECT * FROM services WHERE service_id = ?')
row = statement.execute(id).first
create_service(row) if row
end
def self.create_service(row)
service = Service.new(row['service_id'].to_i, Time.at(row['updated']), row['sort_order'].to_i, row['area'], row['route'], row['status'].to_i)
service.additional_info = row['additional_info']
service.disruption_reason = row['disruption_reason']
service.disruption_date = Time.at(row['disruption_date'])
service.disruption_details = row['disruption_details']
if service.updated < Time.now.utc - (30 * 60)
# If we updated more than 30 mins ago, then set the status as unknown
service.status = Service::UNKNOWN
end
service
end
def initialize(service_id, updated = Time.now.utc, sort_order, area, route, status)
@service_id = service_id
@updated = updated.utc
@sort_order = sort_order
@area = area
@route = route
@status = status
end
def save(client)
sql = "INSERT INTO services (service_id, updated, sort_order, area, route, status, additional_info, disruption_reason, disruption_date, disruption_details) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE updated = ?, sort_order = ?, area = ?, route = ?, status = ?, additional_info = ?, disruption_reason = ?, disruption_date = ?, disruption_details = ?;"
sql_statement = client.prepare(sql)
sql_statement.execute(@service_id, @updated.to_i, @sort_order, @area, @route, @status, @additional_info, @disruption_reason, @disruption_date.to_i, @disruption_details, @updated.to_i, @sort_order, @area, @route, @status, @additional_info, @disruption_reason, @disruption_date.to_i, @disruption_details)
end
def to_s
output = []
output << "Service ID: #{@service_id}"
output << "Updated: #{@updated}"
output << "Sort Order: #{@sort_order}"
output << "Area: #{@area}"
output << "Route: #{@route}"
output << "Status: #{@status}"
output << "Disruption Reason: #{@disruption_reason}"
output << "Disruption Date: #{@disruption_date}"
output << "Disruption Details: #{@disruption_details}"
output << "Additional info: #{@additional_info}"
output.join("\n")
end
end