Skip to content

Instantly share code, notes, and snippets.

@elricstorm
Created September 4, 2009 13:19
Show Gist options
  • Save elricstorm/180875 to your computer and use it in GitHub Desktop.
Save elricstorm/180875 to your computer and use it in GitHub Desktop.
named_scope :compiled_this_week, lambda { { :conditions => ['created_at > ? and created_at < ?', Time.now.beginning_of_week, Time.now.end_of_week] } }
# I have a series of constants that use the following format:
OFFENSE_RUSHING = [:rank, :team_id, :games, :carries, :net, :avg, :tds, :ydspg, :wins, :losses, :ties]
# These constants are called in the method below
# url, name, deep only apply to my scraper class and is used for parsing so it's not an issue here
# If no data is found for the current week, new data is created - this works fine
# However, if data is found for the current week, I want to update that data using the constant above
# Keep in mind that I have 37 constants which house different fields and this method is being used for all of them.
# If I try update it says that wrong number of arguments 1 for 2. I'm not sure how to use update with values
def scrape(url,type,name,deep,constant,model)
parser_object = Scraper.new(url,type,name,deep)
parser_object.scrape_data
parser_object.clean_celldata
if model.compiled_this_week.find(:all).empty?
puts "Creating data for #{model} for the following teams:"
parser_object.rows.each do |row|
team = Team.find_by_name(row[1])
values = {:compiled_on => Date.today.strftime('%Y-%m-%d')}
constant.each_with_index do |field, i|
if row[i] == row[1]
values[field] = team.id
else
values[field] = row[i]
end
end
# List each team we create
puts team.name + " ID = " + team.id.to_s
model.create values
end
else
puts "Updating Current Data in #{model} for the following teams:"
parser_object.rows.each do |row|
team = Team.find_by_name(row[1])
values = {:compiled_on => Date.today.strftime('%Y-%m-%d')}
constant.each_with_index do |field, i|
if row[i] == row[1]
values[field] = team.id
else
values[field] = row[i]
end
end
# List each team we update
puts team.name + " ID = " + team.id.to_s
# This will fail below because only 1 of 2 arguments presented
model.update(values)
# This will work below but update the last row of data on top of all data for the current week
# Please note I was just trying a test to see if it was actually processing the right data
# model.update_all(values)
# The output of values is a hash => {:compiled_on=>"2009-09-04", :rank=>"1"}:Hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment