Class: PostRunner::ActivitiesDB

Inherits:
Object
  • Object
show all
Includes:
Fit4Ruby::Converters
Defined in:
lib/postrunner/ActivitiesDB.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_dir) ⇒ ActivitiesDB

Returns a new instance of ActivitiesDB.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/postrunner/ActivitiesDB.rb', line 13

def initialize(db_dir)
  @db_dir = db_dir
  @fit_dir = File.join(@db_dir, 'fit')
  @archive_file = File.join(@db_dir, 'archive.yml')

  if Dir.exists?(@db_dir)
    begin
      if File.exists?(@archive_file)
        @activities = YAML.load_file(@archive_file)
      else
        @activities = []
      end
    rescue
      Log.fatal "Cannot load archive file '#{@archive_file}': #{$!}"
    end
  else
    create_directories
    @activities = []
  end

  unless @activities.is_a?(Array)
    Log.fatal "The archive file '#{@archive_file}' is corrupted"
  end
end

Instance Method Details

#add(fit_file) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/postrunner/ActivitiesDB.rb', line 38

def add(fit_file)
  base_fit_file = File.basename(fit_file)
  if @activities.find { |a| a.fit_file == base_fit_file }
    Log.debug "Activity #{fit_file} is already included in the archive"
    return false
  end

  begin
    fit_activity = Fit4Ruby.read(fit_file)
  rescue Fit4Ruby::Error
    Log.error "Cannot read #{fit_file}: #{$!}"
    return false
  end

  begin
    FileUtils.cp(fit_file, @fit_dir)
  rescue
    Log.fatal "Cannot copy #{fit_file} into #{@fit_dir}: #{$!}"
  end

  @activities << Activity.new(base_fit_file, fit_activity)
  @activities.sort! do |a1, a2|
    a2.start_time <=> a1.start_time
  end
  sync
  Log.info "#{fit_file} successfully added to archive"

  true
end

#listObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/postrunner/ActivitiesDB.rb', line 112

def list
  i = 0
  @activities.each do |a|
    i += 1
    puts "#{'%4d' % i} " +
         "#{'%-20s' % a.name[0..19]} " +
         "#{'%22s' % a.start_time.strftime("%a, %Y %b %d %H:%M")} " +
         "#{'%7.2f' % (a.distance / 1000)} " +
         "#{'%8s' % secsToHMS(a.duration)} " +
         "#{'%5s' % speedToPace(a.avg_speed)} "
  end
end

#map_to_files(query) ⇒ Object



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
# File 'lib/postrunner/ActivitiesDB.rb', line 80

def map_to_files(query)
  case query
  when /\A-?\d+$\z/
    index = query.to_i
    # The UI counts the activities from 1 to N. Ruby counts from 0 -
    # (N-1).
    index -= 1 if index > 0
    if (a = @activities[index])
      return [ File.join(@fit_dir, a.fit_file) ]
    end
  when /\A-?\d+--?\d+\z/
    idxs = query.match(/(?<sidx>-?\d+)-(?<eidx>-?[0-9]+)/)
    sidx = idxs['sidx'].to_i
    eidx = idxs['eidx'].to_i
    # The UI counts the activities from 1 to N. Ruby counts from 0 -
    # (N-1).
    sidx -= 1 if sidx > 0
    eidx -= 1 if eidx > 0
    unless (as = @activities[sidx..eidx]).empty?
      files = []
      as.each do |a|
        files << File.join(@fit_dir, a.fit_file)
      end
      return files
    end
  else
    Log.error "Invalid activity query: #{query}"
  end

  []
end

#rename(fit_file, name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/postrunner/ActivitiesDB.rb', line 69

def rename(fit_file, name)
  base_fit_file = File.basename(fit_file)
  @activities.each do |a|
    if a.fit_file == base_fit_file
      a.name = name
      sync
      break
    end
  end
end