summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/postrunner/ActivitiesDB.rb12
-rw-r--r--lib/postrunner/Activity.rb20
-rw-r--r--lib/postrunner/ActivityReport.rb66
-rw-r--r--lib/postrunner/FlexiTable.rb1
-rw-r--r--lib/postrunner/Main.rb2
-rw-r--r--test/.PostRunner_spec.rb.swpbin12288 -> 12288 bytes
-rw-r--r--test/PostRunner_spec.rb43
7 files changed, 133 insertions, 11 deletions
diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb
index 2bd7093..e0c61e4 100644
--- a/lib/postrunner/ActivitiesDB.rb
+++ b/lib/postrunner/ActivitiesDB.rb
@@ -36,6 +36,13 @@ module PostRunner
unless @activities.is_a?(Array)
Log.fatal "The archive file '#{@archive_file}' is corrupted"
end
+
+ # The reference to this object is needed inside Activity object but is
+ # not stored in the archive file. We have to retrofit the Activity
+ # instances with this data.
+ @activities.each do |a|
+ a.db = self
+ end
end
def add(fit_file)
@@ -78,6 +85,11 @@ module PostRunner
sync
end
+ def rename(activity, name)
+ activity.rename(name)
+ sync
+ end
+
def check
@activities.each { |a| a.check }
end
diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb
index 838ac8e..aa50789 100644
--- a/lib/postrunner/Activity.rb
+++ b/lib/postrunner/Activity.rb
@@ -1,10 +1,13 @@
require 'fit4ruby'
+require 'postrunner/ActivityReport'
+
module PostRunner
class Activity
attr_reader :fit_file, :name
+ attr_accessor :db
# This is a list of variables that provide data from the fit file. To
# speed up access to it, we cache the data in the activity database.
@@ -28,6 +31,10 @@ module PostRunner
Log.info "FIT file #{@fit_file} is OK"
end
+ def dump
+ load_fit_file
+ end
+
def yaml_initialize(tag, value)
# Create attr_readers for cached variables.
@@CachedVariables.each { |v| self.class.send(:attr_reader, v.to_sym) }
@@ -41,7 +48,7 @@ module PostRunner
end
def encode_with(coder)
- attr_ignore = %w( @fit_activity )
+ attr_ignore = %w( @db @fit_activity )
instance_variables.each do |v|
v = v.to_s
@@ -51,14 +58,9 @@ module PostRunner
end
end
- #def method_missing(method_name, *args, &block)
- # @fit_activity = load_fit_file unless @fit_activity
- # @fit_activity.send(method_name, *args, &block)
- #end
-
- def summary(fit_file)
- load_fit_file
-
+ def summary
+ @fit_activity = load_fit_file unless @fit_activity
+ puts ActivityReport.new(@fit_activity).to_s
end
def rename(name)
diff --git a/lib/postrunner/ActivityReport.rb b/lib/postrunner/ActivityReport.rb
new file mode 100644
index 0000000..971ed3c
--- /dev/null
+++ b/lib/postrunner/ActivityReport.rb
@@ -0,0 +1,66 @@
+require 'fit4ruby'
+require 'postrunner/FlexiTable'
+
+module PostRunner
+
+ class ActivityReport
+
+ include Fit4Ruby::Converters
+
+ def initialize(activity)
+ @activity = activity
+ end
+
+ def to_s
+ session = @activity.sessions[0]
+
+ summary(session) + "\n" + laps
+ end
+
+ private
+
+ def summary(session)
+ t = FlexiTable.new
+ t.enable_frame(false)
+ t.body
+ t.row([ 'Date:', session.start_time ])
+ t.row([ 'Distance:', "#{'%.2f' % (session.distance / 1000.0)} km" ])
+ t.row([ 'Time:', secsToHMS(session.duration) ])
+ t.row([ 'Avg. Pace:',
+ "#{speedToPace(session.avg_speed)} min/km" ])
+ t.row([ 'Total Ascend:', "#{session.ascend} m" ])
+ t.row([ 'Total Descend:', "#{session.descent} m" ])
+ t.row([ 'Calories:', "#{session.calories} m" ])
+ t.row([ 'Avg. HR:', "#{session.avg_heart_rate} bpm" ])
+ t.row([ 'Max. HR:', "#{session.max_heart_rate} bpm" ])
+ t.row([ 'Training Effect:', session.training_effect ])
+ t.row([ 'Avg. Run Cadence:', "#{session.avg_running_cadence.round} spm" ])
+ t.row([ 'Avg. Vertical Oscillation:',
+ "#{'%.1f' % (session.avg_vertical_oscillation / 10)} cm" ])
+ t.row([ 'Avg. Ground Contact Time:',
+ "#{session.avg_stance_time.round} ms" ])
+ t.row([ 'Avg. Stride Length:',
+ "#{'%.2f' % (session.avg_stride_length / 2)} m" ])
+
+ t.to_s
+ end
+
+ def laps
+ t = FlexiTable.new
+ t.head
+ t.row([ 'Duration', 'Avg. Pace', 'Avg. HR', 'Max. HR' ])
+ t.body
+ @activity.laps.each do |lap|
+ t.cell(secsToHMS(lap.total_timer_time))
+ t.cell(speedToPace(lap.avg_speed))
+ t.cell(lap.avg_heart_rate.to_s)
+ t.cell(lap.max_heart_rate.to_s)
+ t.new_row
+ end
+ t.to_s
+ end
+
+ end
+
+end
+
diff --git a/lib/postrunner/FlexiTable.rb b/lib/postrunner/FlexiTable.rb
index 041e414..9655f2e 100644
--- a/lib/postrunner/FlexiTable.rb
+++ b/lib/postrunner/FlexiTable.rb
@@ -259,6 +259,7 @@ module PostRunner
end
def frame_line_to_s
+ return '' unless @enable_frame
s = '+'
@column_attributes.each do |c|
s += '-' * c.min_terminal_width + '+'
diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb
index 89823d4..96d6265 100644
--- a/lib/postrunner/Main.rb
+++ b/lib/postrunner/Main.rb
@@ -222,7 +222,7 @@ EOT
when :dump
activity.dump
when :rename
- activity.rename(@name)
+ @activities.rename(activity, @name)
when :summary
activity.summary
else
diff --git a/test/.PostRunner_spec.rb.swp b/test/.PostRunner_spec.rb.swp
index db98879..cfe7b69 100644
--- a/test/.PostRunner_spec.rb.swp
+++ b/test/.PostRunner_spec.rb.swp
Binary files differ
diff --git a/test/PostRunner_spec.rb b/test/PostRunner_spec.rb
index 31e1510..1c803e9 100644
--- a/test/PostRunner_spec.rb
+++ b/test/PostRunner_spec.rb
@@ -25,12 +25,13 @@ describe PostRunner::Main do
FileUtils.rm_rf(@db_dir)
FileUtils.rm_rf('FILE1.FIT')
create_fit_file('FILE1.FIT', '2014-07-01-8:00')
- #create_fit_file('FILE2.FIT', '2014-07-02-8:00')
+ create_fit_file('FILE2.FIT', '2014-07-02-8:00')
end
after(:all) do
FileUtils.rm_rf(@db_dir)
FileUtils.rm_rf('FILE1.FIT')
+ FileUtils.rm_rf('FILE2.FIT')
end
it 'should abort without arguments' do
@@ -61,9 +62,49 @@ describe PostRunner::Main do
postrunner(%w( check :1 ))
end
+ it 'should check a FIT file' do
+ postrunner(%w( check FILE2.FIT ))
+ end
+
it 'should list the imported file' do
postrunner(%w( list )).index('FILE1.FIT').should be_a(Fixnum)
end
+ it 'should import another FIT file' do
+ postrunner(%w( import FILE2.FIT ))
+ list = postrunner(%w( list ))
+ list.index('FILE1.FIT').should be_a(Fixnum)
+ list.index('FILE2.FIT').should be_a(Fixnum)
+ end
+
+ it 'should delete the first file' do
+ postrunner(%w( delete :2 ))
+ list = postrunner(%w( list ))
+ list.index('FILE1.FIT').should be_nil
+ list.index('FILE2.FIT').should be_a(Fixnum)
+ end
+
+ it 'should not import the deleted file again' do
+ postrunner(%w( import . ))
+ list = postrunner(%w( list ))
+ list.index('FILE1.FIT').should be_nil
+ list.index('FILE2.FIT').should be_a(Fixnum)
+ end
+
+ it 'should rename FILE2.FIT activity' do
+ postrunner(%w( rename :1 --name foobar ))
+ list = postrunner(%w( list ))
+ list.index('FILE2.FIT').should be_nil
+ list.index('foobar').should be_a(Fixnum)
+ end
+
+ it 'should dump an activity from the archive' do
+ postrunner(%w( dump :1 ))
+ end
+
+ it 'should dump a FIT file' do
+ postrunner(%w( dump FILE1.FIT ))
+ end
+
end