Class: TimetrapHarvest::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/timetrap_harvest/formatter.rb

Constant Summary collapse

HARVESTABLE_REGEX =
/@(.*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, config) ⇒ Formatter

Returns a new instance of Formatter.



6
7
8
9
# File 'lib/timetrap_harvest/formatter.rb', line 6

def initialize(entry, config)
  @entry  = entry
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/timetrap_harvest/formatter.rb', line 4

def config
  @config
end

#entryObject (readonly)

Returns the value of attribute entry.



4
5
6
# File 'lib/timetrap_harvest/formatter.rb', line 4

def entry
  @entry
end

Instance Method Details

#alias_configObject



36
37
38
# File 'lib/timetrap_harvest/formatter.rb', line 36

def alias_config
  config.alias_config(code)
end

#codeObject



44
45
46
47
48
# File 'lib/timetrap_harvest/formatter.rb', line 44

def code
  if match = HARVESTABLE_REGEX.match(entry[:note])
    code = match[1]
  end
end

#formatObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/timetrap_harvest/formatter.rb', line 11

def format
  if alias_config && entry[:end]
    { notes:      entry[:note],
      hours:      hours_for_time(entry[:start], entry[:end]),
      project_id: project_id.to_i,
      task_id:    task_id.to_i,
      spent_at:   entry[:start].strftime('%Y%m%d')
    }
  elsif !entry[:end]
    { error: 'Entry not ended yet', note: entry[:note] }
  elsif code
    { error: 'Missing task alias config', note: entry[:note] }
  else
    { error: 'No task alias provided', note: entry[:note] }
  end
end

#hours_for_time(start_time, end_time) ⇒ Object



50
51
52
53
54
# File 'lib/timetrap_harvest/formatter.rb', line 50

def hours_for_time(start_time, end_time)
  minutes = (end_time - start_time) / 60
  rounded = round(minutes)
  hours   = (rounded / 60)
end

#project_idObject



28
29
30
# File 'lib/timetrap_harvest/formatter.rb', line 28

def project_id
  alias_config[:project_id]
end

#round(value, nearest = round_in_minutes) ⇒ Object



56
57
58
# File 'lib/timetrap_harvest/formatter.rb', line 56

def round(value, nearest = round_in_minutes)
  (value % nearest).zero? ? value : (value + nearest) - (value % nearest)
end

#round_in_minutesObject



40
41
42
# File 'lib/timetrap_harvest/formatter.rb', line 40

def round_in_minutes
  config.round_in_minutes
end

#task_idObject



32
33
34
# File 'lib/timetrap_harvest/formatter.rb', line 32

def task_id
  alias_config[:task_id]
end