Class: Toolshed::TimeTracking::Harvest

Inherits:
Object
  • Object
show all
Extended by:
Toolshed::TimeTracking
Defined in:
lib/toolshed/time_tracking/harvest.rb

Constant Summary collapse

MAX_ATTEMPTS =
10
GENERATED_HTML_FILE_LOCATION =
'/tmp/toolshed_generated_time_sheet.html'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Harvest

Returns a new instance of Harvest.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def initialize(options={})
  username = Toolshed::Client::time_tracking_username
  password = Toolshed::Client::time_tracking_password
  owner = Toolshed::Client.time_tracking_owner

  unless (options[:username].nil?)
    username = options[:username]
  end

  unless (options[:password].nil?)
     password = options[:password]
  end

  unless (options[:sub_domain].nil?)
     owner = options[:sub_domain]
  end

  self.harvest_client = ::Harvest.client(owner, username, password)
  self.project_id = options[:project_id] unless !options.has_key?(:project_id)

  # setup formatting
  formatter(options)
end

Instance Attribute Details

#end_list_itemObject

Returns the value of attribute end_list_item.



9
10
11
# File 'lib/toolshed/time_tracking/harvest.rb', line 9

def end_list_item
  @end_list_item
end

#end_unorder_listObject

Returns the value of attribute end_unorder_list.



9
10
11
# File 'lib/toolshed/time_tracking/harvest.rb', line 9

def end_unorder_list
  @end_unorder_list
end

#formatObject

Returns the value of attribute format.



9
10
11
# File 'lib/toolshed/time_tracking/harvest.rb', line 9

def format
  @format
end

#harvest_clientObject

Returns the value of attribute harvest_client.



9
10
11
# File 'lib/toolshed/time_tracking/harvest.rb', line 9

def harvest_client
  @harvest_client
end

#line_breakObject

Returns the value of attribute line_break.



9
10
11
# File 'lib/toolshed/time_tracking/harvest.rb', line 9

def line_break
  @line_break
end

#project_idObject

Returns the value of attribute project_id.



9
10
11
# File 'lib/toolshed/time_tracking/harvest.rb', line 9

def project_id
  @project_id
end

#start_list_itemObject

Returns the value of attribute start_list_item.



9
10
11
# File 'lib/toolshed/time_tracking/harvest.rb', line 9

def start_list_item
  @start_list_item
end

#start_unorder_listObject

Returns the value of attribute start_unorder_list.



9
10
11
# File 'lib/toolshed/time_tracking/harvest.rb', line 9

def start_unorder_list
  @start_unorder_list
end

Class Method Details

.create_instance(options = {}) ⇒ Object



121
122
123
# File 'lib/toolshed/time_tracking/harvest.rb', line 121

def self.create_instance(options={})
  Toolshed::TimeTracking::Harvest.new(options)
end

Instance Method Details

#brought_to_you_by_messageObject



80
81
82
# File 'lib/toolshed/time_tracking/harvest.rb', line 80

def brought_to_you_by_message
  "#{self.line_break}Provided by Toolshed https://rubygems.org/gems/toolshed#{self.line_break}"
end

#displayObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/toolshed/time_tracking/harvest.rb', line 102

def display
  notes = self.previous_notes
  notes = "#{notes}#{self.line_break}#{self.line_break}#{self.todays_notes}"
  notes = "#{notes}#{self.brought_to_you_by_message}"

  if (self.format == 'html')
    FileUtils.rm_rf(Toolshed::TimeTracking::Harvest::GENERATED_HTML_FILE_LOCATION)
    File.open(Toolshed::TimeTracking::Harvest::GENERATED_HTML_FILE_LOCATION, 'w') {|f| f.write(notes) }
    Launchy.open( Toolshed::TimeTracking::Harvest::GENERATED_HTML_FILE_LOCATION ) do |exception|
      puts "Attempted to open #{uri} and failed because #{exception}"
    end
    puts "Checkout out your default or open browser!"
  else
    puts notes
  end

  return
end

#formatter(options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/toolshed/time_tracking/harvest.rb', line 84

def formatter(options={})
  if (options[:format] && options[:format] == 'html')
    self.format = 'html'
    self.line_break = "<br>"
    self.start_list_item = "<li>"
    self.end_list_item = "</li>"
    self.start_unorder_list = "<ul>"
    self.end_unorder_list = "</ul>"
  else
    self.format = 'text'
    self.line_break = "\n"
    self.start_list_item = ""
    self.end_list_item = ""
    self.start_unorder_list = ""
    self.end_unorder_list = ""
  end
end

#previous_notes(days_ago = 1, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/toolshed/time_tracking/harvest.rb', line 45

def previous_notes(days_ago=1, options={})
  notes = "Previous:#{self.line_break}"

  time_entries = previous_time_entries(days_ago, options)

  notes = "#{notes}#{self.start_unorder_list}"
  time_entries.each do |time_entry|
    notes = "#{notes}#{self.start_list_item}#{time_entry.notes}#{self.end_list_item}"
    if (self.end_list_item == '')
      notes = "#{notes}#{self.line_break}"
    end
  end
  notes = "#{notes}#{self.end_unorder_list}"

  notes
end

#previous_time_entries(days_ago, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/toolshed/time_tracking/harvest.rb', line 35

def previous_time_entries(days_ago, options={})
  entries = self.harvest_client.time.all((DateTime.now - days_ago), self.project_id)

  if (entries.size > 0 || days_ago == Toolshed::TimeTracking::Harvest::MAX_ATTEMPTS)
    entries
  else
    entries = self.previous_time_entries(days_ago + 1)
  end
end

#todays_notesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/toolshed/time_tracking/harvest.rb', line 66

def todays_notes
  notes = "Today:#{self.line_break}"
  notes = "#{notes}#{self.start_unorder_list}"
  self.todays_time_entries.each do |time_entry|
    notes = "#{notes}#{self.start_list_item}#{time_entry.notes}#{self.end_list_item}"
    if (self.end_list_item == '')
      notes = "#{notes}#{self.line_break}"
    end
  end
  notes = "#{notes}#{self.end_unorder_list}"

  notes
end

#todays_time_entriesObject



62
63
64
# File 'lib/toolshed/time_tracking/harvest.rb', line 62

def todays_time_entries
  self.harvest_client.time.all(Time.now, self.project_id)
end