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.



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

def initialize(settings_file)
  File.open(settings_file) do |f|
    @settings = JSON.parse(f.read)
  end
  if not @settings['pay_period']
    @ppw = 2
    @to = date_to_string Date.nearest_sunday
  end
  determine_time_interval!
end

Instance Attribute Details

#fromObject



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

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

#harvestObject (readonly)

Returns the value of attribute harvest.



33
34
35
# File 'lib/harvest_submit.rb', line 33

def harvest
  @harvest
end

#toObject



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

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

Instance Method Details

#bodyObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/harvest_submit.rb', line 117

def body
  <<-EOF
Hello,
Attached is my timesheet for pay period #{from} - #{to}

Thank you,
#{whoami}

Generated using http://rubygems.org/gems/harvest_wheelman
Version #{HarvestWheelman::VERSION}
  EOF
end

#date_to_string(input) ⇒ Object



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

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

#date_valid?(input) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
# File 'lib/harvest_submit.rb', line 76

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

#determine_time_interval!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/harvest_submit.rb', line 53

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/harvest_submit.rb', line 130

def drive_to_pdf
  begin
    puts "Starting chromedriver"
    @driver = Selenium::WebDriver.for :chrome
    @base_url = "https://#{site}.harvestapp.com/"
    puts "Driving to #{@base_url}"
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
    @driver.get(@base_url + "/account/login")
    puts "Signing in as #{user}"
    @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
    puts "Heading to the Reports area"
    @driver.find_element(:link, "Reports").click
    @driver.find_element(:css, "span.btn-dropdown").click
    @driver.find_element(:link, "Custom").click

    puts "Entering custom dates"
    @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

    puts "Requesting detailed report"
    @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}"
    if pbcopy pdf_name
      puts "Filename is currently in your pasteboard for file save dialog..."
    end
    @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
    puts "Email your PDF."
    pbcopy subject
    puts <<-EOF
    -------------------------------
#{subject}


#{body}
    -------------------------------
    EOF
  rescue Exception => ex
puts <<-EOF
Error: #{ex.message}
------------------
#{ex.backtrace.join("\n")} 
EOF
  ensure
    @driver.quit
  end
end

#passObject



101
102
103
# File 'lib/harvest_submit.rb', line 101

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

#pdf_nameObject



109
110
111
# File 'lib/harvest_submit.rb', line 109

def pdf_name
  "#{subject}.pdf"
end

#ppwObject

Pay period weeks



49
50
51
# File 'lib/harvest_submit.rb', line 49

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

#siteObject



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

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

#string_to_date(input) ⇒ Object



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

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

#subjectObject



113
114
115
# File 'lib/harvest_submit.rb', line 113

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

#userObject



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

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

#whoamiObject



105
106
107
# File 'lib/harvest_submit.rb', line 105

def whoami
  `whoami`.strip.capitalize
end