-
Notifications
You must be signed in to change notification settings - Fork 2
/
frclinks_server.rb
79 lines (67 loc) · 2.07 KB
/
frclinks_server.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
# Copyright 2017 Patrick Fairbank. All Rights Reserved.
# @author [email protected] (Patrick Fairbank)
#
# The main class of the FRCLinks server.
require "cheesy-common"
require "httparty"
require "pathological"
require "sinatra/base"
require "event_links"
require "misc_links"
require "team_links"
module FrcLinks
class Server < Sinatra::Base
EVENTS_FILE = "events.json"
not_found do
instructions
end
get "/" do
instructions
end
# Fetches the list of events for the current year and caches it in a local file.
get "/reindex_events" do
response = HTTParty.get("https://frc-api.firstinspires.org/v2.0/#{default_year}/events",
:headers => { "Authorization" => "Basic #{CheesyCommon::Config.frc_api_token}" })
events = []
response["Events"].each do |event|
next if ["ChampionshipDivision", "OffSeasonWithAzureSync"].include?(event["type"])
next if event["name"].start_with?("I choose not to attend")
name = event["name"]
name.gsub!(/ Regional/, "")
name.gsub!(/ District/, "")
name.gsub!(/ Event/, "")
name.gsub!(/Festival de Robotique - /, "")
name.gsub!(/ presented by.*/, "")
name.gsub!(/ sponsored by.*/, "")
name.gsub!(/ \(.*/, "")
name.gsub!(/ \*.*/, "")
events << { :code => event["code"].downcase, :name => name, :type => event["type"] }
end
events.sort! do |a, b|
if a[:type] == b[:type]
a[:name] <=> b[:name]
else
b[:type] <=> a[:type]
end
end
File.open(EVENTS_FILE, "w") { |file| file.puts(JSON.pretty_generate(events)) }
"Indexed #{events.size} events."
end
get "/robots.txt" do
content_type "text/plain"
"User-agent: *\nDisallow: /"
end
def default_year
year = Time.now.year
if Time.now.month >= 9
year += 1
end
year
end
# Renders a page with instructions on how to use FRCLinks.
def instructions
@events = JSON.parse(File.read(EVENTS_FILE))
erb :instructions
end
end
end