| Class | UsacFile |
| In: |
lib/usac_file.rb
|
| Parent: | Object |
compares current members to USAC database for current year updates member_usac_to column to 12/31/{year}
| USAC_SITE | = | "www.usacycling.org" |
| REGION_FILES | = | { "Central" => "ct", "Mid-Atlantic" => "ma", "Mountain" => "mt", "North Atlantic" => "na", "North Central" => "nc", "North West" => "nw", "South Central" => "sc", "South East" => "se", "South West" => "sw", "West" => "we", "Wisconsin" => "wisc", "Complete" => "all" |
| members_list | [RW] |
# File lib/usac_file.rb, line 28
28: def initialize(person='promo', pword='races')
29: Net::HTTP.start(USAC_SITE) do |http|
30: req = Net::HTTP::Get.new(REGION_FILES[RacingAssociation.current.usac_region])
31: req.basic_auth person, pword
32: response = http.request(req)
33:
34: #parses out the data into a 2D array with other properties (such as column referencing like hashes)
35: @members_list = FasterCSV.parse(response.body, {:col_sep => ",", :quote_char => "?", :headers => true})
36: self.clean_headers
37: end
38: end
cleans up the headers so we can make clean column references
# File lib/usac_file.rb, line 41
41: def clean_headers
42: @members_list.headers.each do |head|
43: head.lstrip!
44: head.downcase!
45: head.sub!(/ /,"_") #spaces replaced with underscore
46: end
47: end
assumes USAC database contains current year‘s members only, all licenses good until end of this year
# File lib/usac_file.rb, line 50
50: def update_people
51: expir_date = Date.new(Date.today.year, 12, 31)
52: people_updated = []
53: @members_list.each {|memusac|
54: #get the parameters in a nice format
55: license = memusac["license#"].to_i.to_s #strips off leading zeros, consistent with our db
56: full_name = memusac["first_name"].to_s + " " + memusac["last_name"].to_s #as specified by find method used below
57: status = memusac["suspension"].to_s
58:
59: #Look for the person. License # is most reliable (e.g. we only have short first name)
60: #but we may not have their USAC License # yet, so also look by full name
61: r = Person.find_by_license(license)
62: dups = Person.find_all_by_name_or_alias(:first_name => memusac["first_name"], :last_name => memusac["last_name"])
63: first_dup = dups.first unless dups.first.nil?
64: if r.nil?
65: r = Person.find_by_name(full_name)
66: r ||= first_dup
67: else
68: #we found someone by license.
69: if r != first_dup #the name USAC has does not match Person name or alias
70: #Let's make an alias with their name at USAC. Helps with importing results
71: begin
72: Alias.create!(:name => full_name, :person => r)
73: rescue Exception => e
74: Rails.logger.warn("Could not create alias #{full_name} for person #{r.name} with license #{r.license}")
75: end
76:
77: end
78: end
79:
80: unless r.nil? #we found somebody
81: if r.license && r.license.match(/\d+/) && r.license != license
82: #person has a license, but not this one. we must have the wrong person or other confusion.
83: Rails.logger.warn("Had person #{r.name} with license #{r.license} but did not match USAC license #{license} for #{memusac["first_name"]} #{memusac["last_name"]}")
84: else
85: #Either the license # matches or we didn't get this data from the member. Either way, safe to overwrite it
86: r.license = license
87: r.member_usac_to = expir_date
88: r.status = status #could be SUSPENDED or PENDING, per USAC IT guys. No current examples
89: people_updated.push(r) if r.save!
90: end
91: end
92: }
93:
94: Rails.logger.info("#{people_updated.length} people were updated with a USAC expiration date of #{expir_date} ")
95: return people_updated
96: end