Class: TcxRb::Lap

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Lap

Returns a new instance of Lap.



7
8
9
10
11
12
13
14
15
# File 'lib/tcx_rb/lap.rb', line 7

def initialize(args = {})
  @start_time = args[:start_time].to_s
  @total_time = args[:total_time].to_f
  @distance = args[:distance].to_f
  @calories = args[:calories].to_i
  @intensity = args[:intensity]
  @trigger_method = args[:trigger_method]
  @trackpoints = args[:trackpoints]
end

Instance Attribute Details

#caloriesObject

Returns the value of attribute calories.



16
17
18
# File 'lib/tcx_rb/lap.rb', line 16

def calories
  @calories
end

#distanceObject

Returns the value of attribute distance.



16
17
18
# File 'lib/tcx_rb/lap.rb', line 16

def distance
  @distance
end

#intensityObject

Returns the value of attribute intensity.



16
17
18
# File 'lib/tcx_rb/lap.rb', line 16

def intensity
  @intensity
end

#start_timeObject

Returns the value of attribute start_time.



16
17
18
# File 'lib/tcx_rb/lap.rb', line 16

def start_time
  @start_time
end

#total_timeObject

Returns the value of attribute total_time.



16
17
18
# File 'lib/tcx_rb/lap.rb', line 16

def total_time
  @total_time
end

#trackpointsObject

Returns the value of attribute trackpoints.



16
17
18
# File 'lib/tcx_rb/lap.rb', line 16

def trackpoints
  @trackpoints
end

#trigger_methodObject

Returns the value of attribute trigger_method.



16
17
18
# File 'lib/tcx_rb/lap.rb', line 16

def trigger_method
  @trigger_method
end

Instance Method Details

#active_timeObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tcx_rb/lap.rb', line 84

def active_time
  # aggregation of time spent moving
  active = 0.0
  @trackpoints.each_with_index do |tp, i|
    next if i.zero? || tp.distance.zero?

    prev_tp = @trackpoints[i - 1]
    d_t = Time.parse(tp.time) - Time.parse(prev_tp.time)
    active += d_t
  end
  active
end

#avg_altitudeObject



40
41
42
43
# File 'lib/tcx_rb/lap.rb', line 40

def avg_altitude
  tot = @trackpoints.inject(0.0) { |sum, el| sum + el.altitude }
  tot / @trackpoints.size
end

#avg_heart_rateObject



27
28
29
30
# File 'lib/tcx_rb/lap.rb', line 27

def avg_heart_rate
  tot = @trackpoints.inject(0.0) { |sum, el| sum + el.heart_rate }
  tot / @trackpoints.size
end

#avg_paceObject



97
98
99
# File 'lib/tcx_rb/lap.rb', line 97

def avg_pace
  distance / active_time
end

#max_altitudeObject



32
33
34
# File 'lib/tcx_rb/lap.rb', line 32

def max_altitude
  @trackpoints.map(&:altitude).max
end

#max_heart_rateObject



19
20
21
# File 'lib/tcx_rb/lap.rb', line 19

def max_heart_rate
  @trackpoints.map(&:heart_rate).max
end

#max_paceObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tcx_rb/lap.rb', line 45

def max_pace
  max = 0.0
  @trackpoints.each_with_index do |tp, i|
    # skip first cause we need to refrence previous
    # and if the current trackpoint is 0.0 in distance,
    # we will skip it because that means we're stopped
    next if i.zero? || tp.distance.zero?

    prev_tp = @trackpoints[i - 1]

    # also skip to the next one if i isn't 1 and the previous is 0
    # because we could get a ridiculously high pace.
    next if i != 1 && prev_tp.distance.zero?

    # now we can perform the calculation
    d_dist = tp.distance - prev_tp.distance
    d_t = Time.parse(tp.time) - Time.parse(prev_tp.time)
    pace = d_dist / d_t
    max = pace if pace > max
  end
  max
end

#min_altitudeObject



36
37
38
# File 'lib/tcx_rb/lap.rb', line 36

def min_altitude
  @trackpoints.map(&:altitude).min
end

#min_heart_rateObject



23
24
25
# File 'lib/tcx_rb/lap.rb', line 23

def min_heart_rate
  @trackpoints.map(&:heart_rate).min
end

#min_paceObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tcx_rb/lap.rb', line 68

def min_pace
  min = Float::INFINITY
  @trackpoints.each_with_index do |tp, i|
    next if i.zero? || tp.distance.zero?

    prev_tp = @trackpoints[i - 1]
    next if i != 1 && prev_tp.distance.zero?

    d_dist = tp.distance - prev_tp.distance
    d_t = Time.parse(tp.time) - Time.parse(prev_tp.time)
    pace = d_dist / d_t
    min = pace if pace < min
  end
  min
end