Skip to content
Browse files

collect attributes instead of manually shoving into an array

  • Loading branch information...
1 parent ba5ae5f commit 6a344e5461053b3bcfc08eee23863f89cc36db69 Dave Kroondyk committed Mar 18, 2013
Showing with 8 additions and 15 deletions.
  1. +8 −15 app/models/athlete.rb
View
23 app/models/athlete.rb
@@ -18,43 +18,35 @@ def self.populate_initial_data
end
def self.populate_details
- athletes = Athlete.all
- athletes.each do |athlete|
+ Athlete.all.each do |athlete|
details = Nokogiri::HTML(open("http://games.crossfit.com" + athlete.url))
name = details.at_css(".page-title").text
puts name
athlete.name = name.gsub(/^[^:]+:\s*/, "")
puts athlete.name
- attributes = []
- details.xpath('//dd//text()').each do |attribute|
- attributes << attribute.text
- end
+ attributes = details.xpath('//dd//text()').collect(&:text)
athlete.region = attributes[0]
- athlete.team = attributes[1]
+ athlete.team = attributes[1]
athlete.gender = attributes[3]
- athlete.age = attributes[4]
+ athlete.age = attributes[4]
athlete.height = attributes[5]
athlete.weight = attributes[6]
athlete.save
end
end
def self.populate_scores
- athletes = Athlete.all
- athletes.each do |athlete|
+ Athlete.all.each do |athlete|
details = Nokogiri::HTML(open("http://games.crossfit.com" + athlete.url))
leaderboard_src = details.xpath('//iframe[@id="cf_leaderboard"]/@src').text()
leaderboard = Nokogiri::HTML(open(leaderboard_src))
- scores = []
- values = []
- leaderboard.xpath('//table/tbody/tr[@class="highlight"]/td/span[@class="display"]/text()').each do |score|
- scores << score.text
- end
+ scores = leaderboard.xpath('//table/tbody/tr[@class="highlight"]/td/span[@class="display"]/text()').collect(&:text)
athlete.w1 = scores[0]
athlete.w2 = scores[1]
athlete.w3 = scores[2]
athlete.w4 = scores[3]
athlete.w5 = scores[4]
+ values = []
scores.each do |value|
if (match = value.scan(/\A\d*/))
values << match[0].to_i
@@ -65,3 +57,4 @@ def self.populate_scores
end
end
end
+

0 comments on commit 6a344e5

Please sign in to comment.
Something went wrong with that request. Please try again.