Module: Freshtrack

Defined in:
lib/freshtrack/version.rb,
lib/freshtrack.rb

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/freshtrack.rb', line 9

def config
  @config
end

.projectObject (readonly)

Returns the value of attribute project.



9
10
11
# File 'lib/freshtrack.rb', line 9

def project
  @project
end

.taskObject (readonly)

Returns the value of attribute task.



9
10
11
# File 'lib/freshtrack.rb', line 9

def task
  @task
end

Class Method Details

.companyObject



20
21
22
# File 'lib/freshtrack.rb', line 20

def company
  config['company']
end

.condense_time_data(time_data) ⇒ Object



46
47
48
49
# File 'lib/freshtrack.rb', line 46

def condense_time_data(time_data)
  date_data = times_to_dates(time_data)
  group_date_data(date_data)
end

.create_entry(entry_data) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/freshtrack.rb', line 85

def create_entry(entry_data)
  time_entry = FreshBooks::TimeEntry.new
  
  time_entry.project_id = project.project_id
  time_entry.task_id    = task.task_id
  time_entry.date       = entry_data['date']
  time_entry.hours      = entry_data['hours']
  time_entry.notes      = entry_data['notes']
  
  result = time_entry.create
  
  if result
    true
  else
    STDERR.puts "warning: unsuccessful time entry creation for date #{entry_data['date']}"
    nil
  end
end

.get_data(project_name, options = {}) ⇒ Object



73
74
75
76
# File 'lib/freshtrack.rb', line 73

def get_data(project_name, options = {})
  get_project_data(project_name)
  get_time_data(project_name, options)
end

.get_project_data(project_name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/freshtrack.rb', line 32

def get_project_data(project_name)
  raise unless mapping = project_task_mapping[project_name]
  @project = FreshBooks::Project.find_by_name(mapping[:project])
  raise unless @project
  @task = FreshBooks::Task.find_by_name(mapping[:task])
  raise unless @task
end

.get_time_data(project_name, options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/freshtrack.rb', line 40

def get_time_data(project_name, options = {})
  Punch.load
  time_data = Punch.list(project_name, options)
  condense_time_data(time_data)
end

.group_date_data(date_data) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/freshtrack.rb', line 61

def group_date_data(date_data)
  separator = '-' * 20
  grouped = date_data.group_by { |x|  x['date'] }
  grouped.sort.inject([]) do |arr, (date, data)|
    this_date = { 'date' => date }
    this_date['hours'] = data.inject(0) { |sum, x|  sum + x['hours'] }
    this_date['hours'] = ('%.2f' % this_date['hours']).to_f
    this_date['notes'] = data.collect { |x|  x['log'].join("\n") }.join("\n" + separator + "\n")
    arr + [this_date]
  end
end

.initObject



11
12
13
14
# File 'lib/freshtrack.rb', line 11

def init
  load_config
  FreshBooks.setup("#{company}.freshbooks.com", token)
end

.load_configObject



16
17
18
# File 'lib/freshtrack.rb', line 16

def load_config
  @config = YAML.load(File.read(File.expand_path('~/.freshtrack.yml')))
end

.project_task_mappingObject



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

def project_task_mapping
  config['project_task_mapping']
end

.times_to_dates(time_data) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/freshtrack.rb', line 51

def times_to_dates(time_data)
  time_data.each do |td|
    punch_in  = td.delete('in')
    punch_out = td.delete('out')
    
    td['date']  = punch_in.to_date
    td['hours'] = (punch_out - punch_in).secs_to_hours
  end
end

.tokenObject



24
25
26
# File 'lib/freshtrack.rb', line 24

def token
  config['token']
end

.track(project_name, options = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/freshtrack.rb', line 78

def track(project_name, options = {})
  data = get_data(project_name, options)
  data.each do |entry_data|
    create_entry(entry_data)
  end
end