-
Notifications
You must be signed in to change notification settings - Fork 2
Custom Report Formatters
Feature available in v0.4.0 and later
Rescuetime ships with two report formats: CSV and Array. If you would like your report in a different format, don't worry–it's easy to add a custom formatter.
Three things are required to add a custom formatter:
-
Write a class within the module
Rescuetime::Formattersthat inherits fromRescuetime::Formatters::BaseFormatteror one of its descendants - Define the class methods
.nameand.format -
Register your formatters using
Rescuetime.configure
First, the formatters themselves. Here is a basic formatter:
# config/formatters/nil_formatter.rb
module Rescuetime::Formatters
# Turns a productivity report into nothing useful.
class NilFormatter < BaseFormatter
# @return [String] name of this report format
def self.name
'nil'
end
# @param [CSV] _report the raw CSV report from Rescuetime
# @return [nil] the formatted output (in this case, nil)
def self.format(_report)
nil
end
end
endYou can even inherit from an existing formatter:
# config/formatters/shouty_array_formatter.rb
module Rescuetime::Formatters
# Formats a rescuetime report as an array of hashes, except shouting.
class ShoutyArrayFormatter < ArrayFormatter
# @return [String] name of this report format
def self.name
'shouty_array'
end
# @param [CSV] report the raw CSV report from Rescuetime
# @return [Array<Hash>] the formatted output (in this case, a shouty
# array of hashes)
def self.format(report)
array = super(report)
array.map do |hash|
terms = hash.map { |key, value| [key.to_s.upcase, value.to_s.upcase] }
Hash[terms]
end
end
end
endBefore setting your report format, add the path to your formatter(s) to the
Rescuetime configuration using the Rescuetime.configure method. You will be
able to set, append to, or manipulate the formatter_paths setting.
Rescuetime.configure do |config|
path = File.expand_path('../my_custom_formatter.rb', __FILE__)
config.formatter_paths = [path]
endNow Rescuetime will look for the my_custom_formatter.rb file. Multiple paths
may be added as well.
Rescuetime.configure do |config|
config.formatter_paths = [
'config/formatters/*_formatter.rb',
'lib/formatters/**/*_formatter.rb',
]
endFor example, in a Rails app, you could add the configuration file to config/initializers:
# config/initializers/rescuetime.rb
Rescuetime.configure do |config|
path = File.expand_path('../../formatters/*_formatter.rb', __FILE__)
config.formatter_paths += [path]
endRails can now find any formatters ending in _formatter.rb in the folder config/formatters.
For example, for a JSONFormatter:
# config/formatters/json_formatter.rb
module Rescuetime::Formatters
class JSONFormatter < BaseFormatter
def self.name
'json'
end
def self.format(report)
report.to_json
end
end
end