Class: Movescount::Move

Inherits:
OpenStruct
  • Object
show all
Includes:
ActiveModel::Serialization, HTTParty
Defined in:
lib/movescount/move.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(member, attributes = {}) ⇒ Move

Returns a new instance of Move.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
# File 'lib/movescount/move.rb', line 12

def initialize(member, attributes={})
  # raise ArgumentError, 'First argument should be a member' unless member && member.class == Member
  raise ArgumentError, 'Attributes should include a MoveID' unless attributes['MoveID']
  self.class.base_uri Movescount.configuration.api_uri
  @member = member
  super attributes
end

Instance Attribute Details

#memberObject

debug_output $stderr



10
11
12
# File 'lib/movescount/move.rb', line 10

def member
  @member
end

Instance Method Details

#pointsObject

Returns the gps points or the sampmle points when gps not available



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/movescount/move.rb', line 43

def points
  return @points if @points
  if track_points.any?
    current_index = 0
    # If there are track points available, then create the points based on those
    @points = track_points.map do |track_point|
      # Find the correct index for the corresponding sample
      loop do
        current_index += 1
        # Skip this sample (so loop again) if it is far away from the current point
        break if samples[current_index].LocalTime >= track_point.MeasuredAt - 5e-5
      end
      Point.new self, track_point.to_h.merge(samples[current_index].to_h)
    end
  else
    @points = samples.map do |sample|
      Point.new self, sample.to_h
    end
  end
end

#samples(force = false) ⇒ Object

Return the datapoints (samples) of the move Force argument forces reload of data from api



22
23
24
25
26
27
28
29
# File 'lib/movescount/move.rb', line 22

def samples(force = false)
  # Return samples if present and not forcing reload
  return @samples if @samples && !force
  # Get samples from the api and create points objects
  @samples = get_samples.map do |sample|
    Sample.new self, sample
  end
end

#track_points(force = false) ⇒ Object

Return the gps points Force argument forces reload of data from api



33
34
35
36
37
38
39
40
# File 'lib/movescount/move.rb', line 33

def track_points(force = false)
  # Return points if present and not forcing reload
  return @track_points if @track_points && !force
  # Get points from the api and create points objects
  @track_points = get_track_points.map do |track_point|
    TrackPoint.new self, track_point
  end
end