class AFL::FixtureImporter

Public Class Methods

new(year, team_name = nil) click to toggle source
# File lib/afl/fixture_importer.rb, line 8
def initialize(year, team_name = nil)
  @year, @team_name = year, team_name
end

Public Instance Methods

import() click to toggle source
# File lib/afl/fixture_importer.rb, line 12
def import
  generate_response(fixture)
end

Private Instance Methods

away_team(match_data) click to toggle source
# File lib/afl/fixture_importer.rb, line 60
def away_team(match_data)
  match_data[:subject].slice((match_data[:subject].index(' v ') + 3)..(match_data[:subject].index('(AFL') - 2))
end
data_path() click to toggle source
# File lib/afl/fixture_importer.rb, line 36
def data_path
  "#{AFL.root}/data/#{@year}"
end
extracted_team_name(filename) click to toggle source
# File lib/afl/fixture_importer.rb, line 76
def extracted_team_name(filename)
  filename.gsub('_', ' ')
end
filename() click to toggle source
# File lib/afl/fixture_importer.rb, line 40
def filename
  filenames.detect { |filename| filename.match(/\A#{@team_name.gsub(' ', '_')}/)}
end
filenames() click to toggle source
# File lib/afl/fixture_importer.rb, line 44
def filenames
  Dir.entries("#{AFL.root}/data/#{@year}")
end
fixture() click to toggle source
# File lib/afl/fixture_importer.rb, line 18
def fixture
  res = []
  CSV.foreach("#{data_path}/#{filename}", {:headers => true, :header_converters => :symbol}) do |row|
    hashed_row = {}
    row.headers.each do |key|
      hashed_row[key.to_sym] = row[key]
    end
    res << hashed_row
  end
  res
end
format_response(match_data) click to toggle source
# File lib/afl/fixture_importer.rb, line 48
def format_response(match_data)
  OpenStruct.new(:home_team => home_team(match_data),
                 :away_team => away_team(match_data),
                 :venue =>  venue(match_data),
                 :time => time(match_data),
                 :round => round(match_data) )
end
generate_response(fixture) click to toggle source
# File lib/afl/fixture_importer.rb, line 30
def generate_response(fixture)
  fixture.map do |match|
    format_response(match)
  end
end
home_team(match_data) click to toggle source
# File lib/afl/fixture_importer.rb, line 56
def home_team(match_data)
  match_data[:subject].slice(0..(match_data[:subject].index(' v ') - 1))
end
round(match_data) click to toggle source
# File lib/afl/fixture_importer.rb, line 72
def round(match_data)
  match_data[:subject].slice((match_data[:subject].index('Rd ') + 3)..(match_data[:subject].index(')') - 1))
end
time(match_data) click to toggle source
# File lib/afl/fixture_importer.rb, line 68
def time(match_data)
  Chronic.parse("#{match_data[:start_date]} #{match_data[:start_time]}" )
end
venue(match_data) click to toggle source
# File lib/afl/fixture_importer.rb, line 64
def venue(match_data)
  match_data[:location]
end