Class: HarvestWheelman::HarvestSubmit

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

Defined Under Namespace

Classes: UnknownTimeIntervalError

Constant Summary collapse

DATEFORMAT =
"%m/%d/%Y"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings_file) ⇒ HarvestSubmit

Returns a new instance of HarvestSubmit.



19
20
21
22
23
24
# File 'lib/harvest_submit.rb', line 19

def initialize(settings_file)
  File.open(settings_file) do |f|
    @settings = JSON.parse(f.read)
  end
  determine_time_interval!
end

Instance Attribute Details

#fromObject



64
65
66
# File 'lib/harvest_submit.rb', line 64

def from
  @from ||= @settings['pay_period']['from']
end

#harvestObject (readonly)

Returns the value of attribute harvest.



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

def harvest
  @harvest
end

#toObject



68
69
70
# File 'lib/harvest_submit.rb', line 68

def to
  @to ||= @settings['pay_period']['to']
end

Instance Method Details

#bodyObject



96
97
98
99
100
101
# File 'lib/harvest_submit.rb', line 96

def body
  %{Hello,
  Attached is my timesheet for pay period #{from} - #{to}
  Thank you,
  #{whoami}}
end

#date_to_string(input) ⇒ Object



51
52
53
# File 'lib/harvest_submit.rb', line 51

def date_to_string(input)
  input.strftime(DATEFORMAT)
end

#date_valid?(input) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'lib/harvest_submit.rb', line 55

def date_valid?(input)
  return false unless input
  begin
    string_to_date input
  rescue ArgumentError
    false
  end
end

#determine_time_interval!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/harvest_submit.rb', line 32

def determine_time_interval!
  if date_valid? from
    if date_valid? to
    else
      puts "Setting TO via the FROM based on a #{ppw} week pay period"
      @to = date_to_string(1.day.before(ppw.weeks.after(string_to_date(from))))
    end
  elsif date_valid? to
    puts "Setting FROM via the TO based on a ppw week pay period"
    @from = date_to_string(1.day.after(ppw.weeks.before(string_to_date(to))))
  else
    raise UnknownTimeIntervalError
  end
end

#drive_to_pdfObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/harvest_submit.rb', line 103

def drive_to_pdf
  begin
    @driver = Selenium::WebDriver.for :firefox
    @base_url = "https://#{site}.harvestapp.com/"
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
    @driver.get(@base_url + "/account/login")
    @driver.find_element(:id, "email").clear
    @driver.find_element(:id, "email").send_keys user
    @driver.find_element(:id, "user_password").clear
    @driver.find_element(:id, "user_password").send_keys pass
    @driver.find_element(:id, "sign-in-button").click
    @driver.find_element(:link, "Reports").click
    @driver.find_element(:css, "span.btn-dropdown").click
    @driver.find_element(:link, "Custom").click

    @driver.find_element(:id, "start_date").clear
    @driver.find_element(:id, "start_date").send_keys from
    @driver.find_element(:id, "end_date").clear
    @driver.find_element(:id, "end_date").send_keys to

    @driver.find_element(:link, "Update Report").click
    @driver.find_element(:link, "Detailed Report").click
    @driver.find_elements(:tag_name => 'option').find{|i| i.text == 'Task'}.click
    
    puts "Print as PDF. Filename:\n#{pdf_name}"
    pbcopy pdf_name
    @driver.execute_script('window.print()')
    @driver.find_element(:xpath, "//nav[@id='nav']/div/ul/li[4]/a").click
    @driver.find_element(:link, "Sign Out").click
    @driver.quit
    puts "Email your PDF."
    puts "Subject:\n#{subject}"
    puts "Body:\n#{body}"
    pbcopy "#{subject}\n#{body}"
  rescue Exception => ex
    "There was a problem!\n#{ex.backtrace}\n\n#{ex.message}\nExit!"
  end
end

#passObject



80
81
82
# File 'lib/harvest_submit.rb', line 80

def pass
  @settings['harvest']['pass']
end

#pdf_nameObject



88
89
90
# File 'lib/harvest_submit.rb', line 88

def pdf_name
  "#{subject}.pdf"
end

#ppwObject

Pay period weeks



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

def ppw
  @ppw ||= @settings['pay_period']['weeks'] || 2
end

#siteObject



72
73
74
# File 'lib/harvest_submit.rb', line 72

def site
  @settings["harvest"]["site"]
end

#string_to_date(input) ⇒ Object



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

def string_to_date(input)
  Date.strptime(input, DATEFORMAT)
end

#subjectObject



92
93
94
# File 'lib/harvest_submit.rb', line 92

def subject
  "#{whoami}'s Timesheet #{from} - #{to}"
end

#userObject



76
77
78
# File 'lib/harvest_submit.rb', line 76

def user
  @settings['harvest']['user']
end

#whoamiObject



84
85
86
# File 'lib/harvest_submit.rb', line 84

def whoami
  `whoami`.strip
end