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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/toolshed/time_tracking/harvest.rb', line 13

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

  username = options[:username] unless options[:username].nil?
  password = options[:password] unless options[:password].nil?
  owner = options[:sub_domain] unless options[:sub_domain].nil?

  self.harvest_client = ::Harvest.client(subdomain: owner, username: username, password: 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.



11
12
13
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def end_list_item
  @end_list_item
end

#end_unorder_listObject

Returns the value of attribute end_unorder_list.



11
12
13
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def end_unorder_list
  @end_unorder_list
end

#formatObject

Returns the value of attribute format.



11
12
13
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def format
  @format
end

#harvest_clientObject

Returns the value of attribute harvest_client.



11
12
13
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def harvest_client
  @harvest_client
end

#line_breakObject

Returns the value of attribute line_break.



11
12
13
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def line_break
  @line_break
end

#project_idObject

Returns the value of attribute project_id.



11
12
13
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def project_id
  @project_id
end

#start_list_itemObject

Returns the value of attribute start_list_item.



11
12
13
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def start_list_item
  @start_list_item
end

#start_unorder_listObject

Returns the value of attribute start_unorder_list.



11
12
13
# File 'lib/toolshed/time_tracking/harvest.rb', line 11

def start_unorder_list
  @start_unorder_list
end

Class Method Details

.create_instance(options = {}) ⇒ Object



115
116
117
# File 'lib/toolshed/time_tracking/harvest.rb', line 115

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

Instance Method Details

#brought_to_you_by_messageObject



74
75
76
# File 'lib/toolshed/time_tracking/harvest.rb', line 74

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

#displayObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/toolshed/time_tracking/harvest.rb', line 96

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/toolshed/time_tracking/harvest.rb', line 78

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/toolshed/time_tracking/harvest.rb', line 39

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



29
30
31
32
33
34
35
36
37
# File 'lib/toolshed/time_tracking/harvest.rb', line 29

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



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/toolshed/time_tracking/harvest.rb', line 60

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



56
57
58
# File 'lib/toolshed/time_tracking/harvest.rb', line 56

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