Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Just some minor changes. Totally untested. #3

Merged
merged 5 commits into from about 1 year ago

2 participants

Dave Kroondyk Mike Munroe
Dave Kroondyk

Added a link to the actual crossfit games profile page.
Tweaked a few of the xpath iterations to use Ruby #collect method instead of #each and shoving into array. #collect and #map are synonyms. My general preference is when you are just grabbing one attribute from the items in the collection, I think of that as "collect," meaning... go and collect all these attributes. I would use map when I wan to more or less transform a collection into another collection. I think of it like go and map this collection to something else. But like I said, they both do the same thing.

.collect(&:text) is just shorthand for .collect { |score| score.text }

Mike Munroe mikepmunroe merged commit 9ef3225 into from March 18, 2013
Mike Munroe mikepmunroe closed this March 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
10  README.md
Source Rendered
@@ -4,13 +4,3 @@ The [Crossfit Games](http://http://games.crossfit.com/) site has its [own leader
4 4
 
5 5
 I get crushed at [Crossfit Wicked](http://www.crossfitwicked.com/) and some friends at work get crushed at [Crossfit Southie](http://crossfitsouthie.com/), so those are the only affiliates in the mix for now.
6 6
 
7  
-TODO:
8  
-- [ ] fix #2
9  
-- [ ] create a specific view for Wicked and Southie affiliates
10  
-- [ ] break out tables based off of gender and Games age groupings
11  
-- [ ] spice up the design a bit instead of lame bootstrap
12  
-
13  
-Possible additions:
14  
-- [ ] pull down scores for some other affiliates?
15  
-- [ ] pull down scores from 2012?
16  
-- [ ] create some more cooler views with some of the scoring data, maybe with some comparisons of 2012 vs. 2013 scores?
16  app/controllers/athletes_controller.rb
... ...
@@ -1,24 +1,10 @@
1 1
 class AthletesController < ApplicationController
2  
-  # GET /athletes
3  
-  # GET /athletes.json
4 2
   def index
5  
-    # @athletes = Athlete.all
6 3
     @athletes = Athlete.order("total asc")
7  
-
8  
-    respond_to do |format|
9  
-      format.html # index.html.erb
10  
-      format.json { render json: @athletes }
11  
-    end
12 4
   end
13 5
 
14  
-  # GET /athletes/1
15  
-  # GET /athletes/1.json
16 6
   def show
17 7
     @athlete = Athlete.find(params[:id])
18  
-
19  
-    respond_to do |format|
20  
-      format.html # show.html.erb
21  
-      format.json { render json: @athlete }
22  
-    end
23 8
   end
24 9
 end
  10
+
23  app/models/athlete.rb
@@ -18,21 +18,17 @@ def self.populate_initial_data
18 18
   end
19 19
 
20 20
   def self.populate_details
21  
-    athletes = Athlete.all
22  
-    athletes.each do |athlete|
  21
+    Athlete.all.each do |athlete|
23 22
       details = Nokogiri::HTML(open("http://games.crossfit.com" + athlete.url))
24 23
       name = details.at_css(".page-title").text
25 24
       puts name
26 25
       athlete.name = name.gsub(/^[^:]+:\s*/, "")
27 26
       puts athlete.name
28  
-      attributes = []
29  
-      details.xpath('//dd//text()').each do |attribute|
30  
-        attributes << attribute.text
31  
-      end
  27
+      attributes = details.xpath('//dd//text()').collect(&:text)
32 28
       athlete.region = attributes[0]
33  
-      athlete.team = attributes[1]
  29
+      athlete.team   = attributes[1]
34 30
       athlete.gender = attributes[3]
35  
-      athlete.age = attributes[4]
  31
+      athlete.age    = attributes[4]
36 32
       athlete.height = attributes[5]
37 33
       athlete.weight = attributes[6]
38 34
       athlete.save
@@ -40,21 +36,17 @@ def self.populate_details
40 36
   end
41 37
 
42 38
   def self.populate_scores
43  
-    athletes = Athlete.all
44  
-    athletes.each do |athlete|
  39
+    Athlete.all.each do |athlete|
45 40
       details = Nokogiri::HTML(open("http://games.crossfit.com" + athlete.url))
46 41
       leaderboard_src = details.xpath('//iframe[@id="cf_leaderboard"]/@src').text()
47 42
       leaderboard = Nokogiri::HTML(open(leaderboard_src))
48  
-      scores = []
49  
-      values = []
50  
-      leaderboard.xpath('//table/tbody/tr[@class="highlight"]/td/span[@class="display"]/text()').each do |score|
51  
-        scores << score.text
52  
-      end
  43
+      scores = leaderboard.xpath('//table/tbody/tr[@class="highlight"]/td/span[@class="display"]/text()').collect(&:text)
53 44
       athlete.w1 = scores[0]
54 45
       athlete.w2 = scores[1]
55 46
       athlete.w3 = scores[2]
56 47
       athlete.w4 = scores[3]
57 48
       athlete.w5 = scores[4]
  49
+      values = []
58 50
       scores.each do |value|
59 51
         if (match = value.scan(/\A\d*/))
60 52
           values << match[0].to_i
@@ -65,3 +57,4 @@ def self.populate_scores
65 57
     end
66 58
   end
67 59
 end
  60
+
5  app/views/athletes/index.html.erb
@@ -18,7 +18,7 @@
18 18
     <% @athletes.each do |athlete| %>
19 19
       <tr>
20 20
         <td><%= athlete.name %></td>
21  
-        <td><%= athlete.url %></td>
  21
+        <td><%= line_to athlete.url, "http://games.crossfit.com#{athlete.url}" %></td>
22 22
         <td><%= athlete.affiliate %></td>
23 23
         <td><%= athlete.age %></td>
24 24
         <td><%= athlete.w1 %></td>
@@ -30,4 +30,5 @@
30 30
       </tr>
31 31
     <% end %>
32 32
   </tbody>
33  
-</table>
  33
+</table>
  34
+
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.