Class: TomatoHarvest::TimeEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/tomatoharvest/time_entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TimeEntry

Returns a new instance of TimeEntry.



19
20
21
# File 'lib/tomatoharvest/time_entry.rb', line 19

def initialize(options = {})
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/tomatoharvest/time_entry.rb', line 7

def options
  @options
end

Class Method Details

.build_and_test(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/tomatoharvest/time_entry.rb', line 9

def self.build_and_test(options = {})
  required = ["domain", "username", "password", "project", "task", "name"].to_set
  keys = options.keys.to_set
  if required.subset?(keys)
    new(options).tap do |entry|
      entry.test
    end
  end
end

Instance Method Details

#dateObject

Date for task (today), formatted properly for tracker



70
71
72
# File 'lib/tomatoharvest/time_entry.rb', line 70

def date
  Date.today
end

#log(seconds) ⇒ Object

Persist time entry to Harvest tracker



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tomatoharvest/time_entry.rb', line 34

def log(seconds)
  hours = seconds_to_hours(seconds)
  return if hours == 0
  options = {
    notes: notes,
    hours: hours,
    spent_at: date,
    project_id: project.id,
    task_id: task.id
  }


  existing_entry = time_api.all.find do |entry|
    entry.notes == self.notes
  end

  if existing_entry
    existing_entry.hours += hours
    existing_entry.spent_at = date
    time_api.update(existing_entry)
  else
    entry = Harvest::TimeEntry.new(options)
    time_api.create(entry)
  end
end

#notesObject

Notes to send to tracker



63
64
65
# File 'lib/tomatoharvest/time_entry.rb', line 63

def notes
  options["name"]
end

#projectObject

Harvest project with name matching options



86
87
88
89
90
# File 'lib/tomatoharvest/time_entry.rb', line 86

def project
  time_api.trackable_projects.find do |project|
    project.name == options["project"]
  end
end

#seconds_to_hours(seconds) ⇒ Object

Convert seconds into hours



77
78
79
80
81
# File 'lib/tomatoharvest/time_entry.rb', line 77

def seconds_to_hours(seconds)
  minutes = (seconds / 60.0)
  unrounded = (minutes / 60.0)
  (unrounded * 100).ceil / 100.0
end

#taskObject

Harvest task with name matching options



95
96
97
98
99
# File 'lib/tomatoharvest/time_entry.rb', line 95

def task
  project.tasks.find do |task|
    task.name == options["task"]
  end
end

#testObject

Check that the project and task were found on Harvest



26
27
28
29
# File 'lib/tomatoharvest/time_entry.rb', line 26

def test
  raise "Couldn't find project"   unless project
  raise "Couldn't find task type" unless task
end