Class: TimetrapToggl::Formatter
- Inherits:
-
Object
- Object
- TimetrapToggl::Formatter
- Defined in:
- lib/timetrap_toggl/formatter.rb
Constant Summary collapse
- TOGGABLE_REGEX =
/@(.*)/
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#entry ⇒ Object
readonly
Returns the value of attribute entry.
Instance Method Summary collapse
- #code ⇒ Object
- #description ⇒ Object
- #duration ⇒ Object
- #format ⇒ Object
-
#initialize(entry, config) ⇒ Formatter
constructor
A new instance of Formatter.
- #project ⇒ Object
- #round_in_minutes ⇒ Object
- #seconds_for_time(start_time, end_time) ⇒ Object
- #start ⇒ Object
- #task_id ⇒ Object
Constructor Details
#initialize(entry, config) ⇒ Formatter
Returns a new instance of Formatter.
6 7 8 9 |
# File 'lib/timetrap_toggl/formatter.rb', line 6 def initialize(entry, config) @entry = entry @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/timetrap_toggl/formatter.rb', line 4 def config @config end |
#entry ⇒ Object (readonly)
Returns the value of attribute entry.
4 5 6 |
# File 'lib/timetrap_toggl/formatter.rb', line 4 def entry @entry end |
Instance Method Details
#code ⇒ Object
46 47 48 49 50 |
# File 'lib/timetrap_toggl/formatter.rb', line 46 def code if match = TOGGABLE_REGEX.match(entry[:note]) code = match[1] end end |
#description ⇒ Object
52 53 54 |
# File 'lib/timetrap_toggl/formatter.rb', line 52 def description entry[:note].gsub("@#{code}", '').strip end |
#duration ⇒ Object
56 57 58 |
# File 'lib/timetrap_toggl/formatter.rb', line 56 def duration seconds_for_time(entry[:start], entry[:end]) end |
#format ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/timetrap_toggl/formatter.rb', line 11 def format if project["id"] { "description" => description, "duration" => duration, "start" => start, "pid" => project["id"], "created_with" => "timetrap-toggl", } else { error: "Project #{project["name"]} does not exist", note: entry[:note] } end end |
#project ⇒ Object
32 33 34 35 36 |
# File 'lib/timetrap_toggl/formatter.rb', line 32 def project config.projects.find do |p| code.downcase == p["name"].downcase.gsub(/[^a-zA-Z\d\s]/, '').gsub(/\s+/, '-') end end |
#round_in_minutes ⇒ Object
42 43 44 |
# File 'lib/timetrap_toggl/formatter.rb', line 42 def round_in_minutes config.round_in_minutes end |
#seconds_for_time(start_time, end_time) ⇒ Object
60 61 62 |
# File 'lib/timetrap_toggl/formatter.rb', line 60 def seconds_for_time(start_time, end_time) (end_time - start_time).to_i end |
#start ⇒ Object
28 29 30 |
# File 'lib/timetrap_toggl/formatter.rb', line 28 def start TogglV8::API.new.iso8601(entry[:start].to_datetime) end |
#task_id ⇒ Object
38 39 40 |
# File 'lib/timetrap_toggl/formatter.rb', line 38 def task_id alias_config[:task_id] end |