class RacePartnerRegistrations::Event

Attributes

url[R]

Public Class Methods

new(url) click to toggle source
# File lib/race_partner_registrations/event.rb, line 8
def initialize(url)
  @url = url
end

Public Instance Methods

download_registrations!() click to toggle source
# File lib/race_partner_registrations/event.rb, line 24
def download_registrations!
  raise InvalidUrlError unless valid?
  @registrations = []
  get_state_values(page).each do |state_value|
    agent.get(single_page_url(state_value)).search("div#EntrantList tr").each do |row|
      name_td, location_td = [0,1].collect { |n| row.search("td")[n] }
      next unless name_td && location_td
      @registrations << Registration.new(name_td.text.strip, location_td.text.strip)
    end
  end
  @registrations
end
downloaded?() click to toggle source
# File lib/race_partner_registrations/event.rb, line 20
def downloaded?
  !!@registrations
end
registrations() click to toggle source
# File lib/race_partner_registrations/event.rb, line 16
def registrations
  @registrations || raise(RegistrationsNotDownloadedError, "download_registrations! has not been sent to this event.")
end
to_csv() click to toggle source
# File lib/race_partner_registrations/event.rb, line 37
def to_csv
  download_registrations! unless downloaded?
  CSV.generate do |csv|
    csv << ["name", "location"]
    registrations.each do |registration|
      csv << [:name, :location].collect { |attr| registration.send(attr) }
    end
  end
end
valid?() click to toggle source
# File lib/race_partner_registrations/event.rb, line 12
def valid?
  page.search("div#EntrantSearchForm").any?
end

Private Instance Methods

agent() click to toggle source
# File lib/race_partner_registrations/event.rb, line 53
def agent
  @agent = Mechanize.new
end
get_form(page) click to toggle source
# File lib/race_partner_registrations/event.rb, line 57
def get_form(page)
  form_action = page.search("div#EntrantSearchForm form").first.attr("action")
  page.form_with(action: form_action)
end
get_state_values(page) click to toggle source
# File lib/race_partner_registrations/event.rb, line 62
def get_state_values(page)
  get_form(page).field_with(id: "state").options.collect do |state_option|
    state_option.value
  end.reject do |state_value|
    state_value.length == 0
  end
end
page() click to toggle source
# File lib/race_partner_registrations/event.rb, line 49
def page
  @page ||= agent.get(url)
end
single_page_url(state_value) click to toggle source
# File lib/race_partner_registrations/event.rb, line 70
def single_page_url(state_value)
  base_url = url.split("?")[0]
  base_url + "?State=#{state_value}&PageSize=#{200_000_000}&PageIndex=1&Submitted=true"
end