Class: GarminConnectrActivity

Inherits:
Object
  • Object
show all
Defined in:
lib/garmin_connectr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ GarminConnectrActivity

Returns a new instance of GarminConnectrActivity.



52
53
54
55
56
57
58
59
# File 'lib/garmin_connectr.rb', line 52

def initialize( opts )
  @id = opts[:id]
  @splits = []
  @data = {}
  @data[:name] = opts[:name]
  @data[:url] = "http://connect.garmin.com/activity/#{ @id }"
  @loaded = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/garmin_connectr.rb', line 143

def method_missing(name)
  
  self.load! if !@data[name.to_sym] and !@loaded  # lazy loading
  ret = @data[name.to_sym]

  ## Got nothing? Try a variation: Garmin changed a few fields:
  ## =>   min_elevation => minelevation, max_elevation => maxelevation
  ret = @data[name.to_s.gsub('_','').to_sym] if ret == nil

  ret
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



50
51
52
# File 'lib/garmin_connectr.rb', line 50

def data
  @data
end

#idObject

Returns the value of attribute id.



50
51
52
# File 'lib/garmin_connectr.rb', line 50

def id
  @id
end

#loadedObject

Returns the value of attribute loaded.



50
51
52
# File 'lib/garmin_connectr.rb', line 50

def loaded
  @loaded
end

#split_summaryObject

Returns the value of attribute split_summary.



50
51
52
# File 'lib/garmin_connectr.rb', line 50

def split_summary
  @split_summary
end

#splitsObject

Returns the value of attribute splits.



50
51
52
# File 'lib/garmin_connectr.rb', line 50

def splits
  @splits
end

Instance Method Details

#fieldsObject



114
115
116
# File 'lib/garmin_connectr.rb', line 114

def fields
  @data.keys
end

#load!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/garmin_connectr.rb', line 61

def load!
  url = "http://connect.garmin.com/activity/#{ @id }"
  doc = Nokogiri::HTML(open(url))
  
  # HR cell name manipulation
  ['bpm', 'pom', 'hrZones'].each do |hr|
    doc.css("##{ hr }Summary td").each do |e|
      e.inner_html = "Max HR #{ hr.upcase }:" if e.inner_html =~ /Max HR/i
      e.inner_html = "Avg HR #{ hr.upcase }:" if e.inner_html =~ /Avg HR/i
    end
  end
  
  @scrape = {
    :details => {
      :name          => doc.css('#activityName').inner_html,
      :activity_type => doc.css('#activityTypeValue').inner_html.gsub(/[\n\t]+/,''),
      :event_type    => doc.css('#eventTypeValue').inner_html.gsub(/[\n\t]+/,''),
      :timestamp     => doc.css('#timestamp').inner_html.gsub(/[\n\t]+/,''),
      :embed         => doc.css('.detailsEmbedCode').attr('value').value,
      :device        => doc.css('.addInfoDescription a').inner_html
    },
    :summaries => {
      :overall     => { :css => '#detailsOverallBox' },
      :timing      => { :css => '#detailsTimingBox' },
      :elevation   => { :css => '#detailsElevationBox' },
      :heart_rate  => { :css => '#detailsHeartRateBox' },
      :cadence     => { :css => '#detailsCadenceBox' },
      :temperature => { :css => '#detailsTemperatureBox' },
      :power       => { :css => '#detailsPowerBox' }
    }
  }
  
  @scrape[:details][:split_count] = doc.css('.detailsLapsNumber')[0].inner_html.to_i rescue 0
  
  @scrape[:details].each { |k,v| @data[k] = v }
  @scrape[:summaries].each do |k,v|
    doc.css("#{ v[:css] } td").each do |e|
      if e.inner_html =~ /:[ ]?$/
        key = e.inner_html.downcase.gsub(/ $/,'').gsub(/:/,'').gsub(' ','_').to_sym
        @data[key] = e.next.next.inner_html.strip
      end
    end
  end

  ## Splits
  if self.split_count > 0
    load_splits!
  end

  @loaded = true
  self
end