Class: TimetrapToggl::Formatter

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

Constant Summary collapse

TOGGABLE_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_toggl/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_toggl/formatter.rb', line 4

def config
  @config
end

#entryObject (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

#codeObject



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

#descriptionObject



52
53
54
# File 'lib/timetrap_toggl/formatter.rb', line 52

def description
  entry[:note].gsub("@#{code}", '').strip
end

#durationObject



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

def duration
  seconds_for_time(entry[:start], entry[:end])
end

#formatObject



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

#projectObject



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_minutesObject



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

#startObject



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

def start
  TogglV8::API.new.iso8601(entry[:start].to_datetime)
end

#task_idObject



38
39
40
# File 'lib/timetrap_toggl/formatter.rb', line 38

def task_id
  alias_config[:task_id]
end