Skip to content

Commit

Permalink
Add checks for canonical names
Browse files Browse the repository at this point in the history
  • Loading branch information
zqianem committed May 3, 2020
1 parent 6aeac8d commit 902db9e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/sciolyff.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'csv'
require 'open-uri'
require 'psych'
require 'date'

Expand Down
23 changes: 23 additions & 0 deletions lib/sciolyff/validator/canonical.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module SciolyFF
# Logic for checking against canonical lists of events and schools
module Validator::Canonical
BASE = 'https://raw.githubusercontent.com/unosmium/canonical-names/master/'

private

def canonical?(rep, file, logger)
return true if @canonical_warned

@canonical_list ||= CSV.parse(URI.open(BASE + file))
# don't try to make this more efficient, harder than it looks because of
# nil comparisons
@canonical_list.include?(rep)
rescue StandardError => e
logger.warn "could not read canonical names file: #{BASE + file}"
logger.debug "#{e}\n #{e.backtrace.first}"
@canonical_warned = true
end
end
end
10 changes: 10 additions & 0 deletions lib/sciolyff/validator/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'sciolyff/validator/checker'
require 'sciolyff/validator/sections'
require 'sciolyff/validator/canonical'

module SciolyFF
# Checks for one event in the Events section of a SciolyFF file
Expand Down Expand Up @@ -78,6 +79,15 @@ def places_start_at_one?(event, logger)
"#{lowest_place} instead of 1"
end

include Validator::Canonical

def in_canonical_list?(event, logger)
rep = [event[:name]]
return true if canonical?(rep, 'events.csv', logger)

logger.warn "non-canonical event: #{event[:name]}"
end

private

def placings_by_place(event)
Expand Down
11 changes: 11 additions & 0 deletions lib/sciolyff/validator/teams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'sciolyff/validator/checker'
require 'sciolyff/validator/sections'
require 'sciolyff/validator/canonical'

module SciolyFF
# Checks for one team in the Teams section of a SciolyFF file
Expand Down Expand Up @@ -70,6 +71,16 @@ def in_a_subdivision_if_possible?(team, logger)
logger.warn "missing subdivision for 'team: #{team[:number]}'"
end

include Validator::Canonical

def in_canonical_list?(team, logger)
rep = [team[:school], team[:city], team[:state]]
return true if canonical?(rep, 'schools.csv', logger)

location = rep[1..-1].compact.join ', '
logger.warn "non-canonical school: #{team[:school]} in #{location}"
end

private

def initialize_teams_info(teams)
Expand Down

0 comments on commit 902db9e

Please sign in to comment.