-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrocky_balboa.rb
118 lines (93 loc) · 2.81 KB
/
rocky_balboa.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require 'rubygems'
require 'active_support/all'
require 'audite'
require 'dotenv'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
module Capybara::Poltergeist
class Client
private
def redirect_stdout
prev = STDOUT.dup
prev.autoclose = false
$stdout = @write_io
STDOUT.reopen(@write_io)
prev = STDERR.dup
prev.autoclose = false
$stderr = @write_io
STDERR.reopen(@write_io)
yield
ensure
STDOUT.reopen(prev)
$stdout = STDOUT
STDERR.reopen(prev)
$stderr = STDERR
end
end
end
class WarningSuppressor
class << self
def write(message)
if message =~ /QFont::setPixelSize: Pixel size <= 0/ || message =~/CoreText performance note:/ then 0 else puts(message);1;end
end
end
end
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, js_errors: false, phantomjs_logger: WarningSuppressor, timeout: 50)
end
Capybara.current_driver = :poltergeist
Capybara.run_server = false
Capybara.app_host = 'http://punchlock.herokuapp.com'
Dotenv.load
module RockyBalboa
class Puncher
include Capybara::DSL
def initialize(email, password)
raise 'InvalidCredentials' if email.blank? or password.blank?
@email = email
@password = password
play_theme!
login!
end
def punch!(options = {})
project = options.fetch(:project)
from = Date.parse options.fetch(:from)
to = Date.parse options.fetch(:to)
start_hour = options.fetch(:start_hour)
lunch_hour = options.fetch(:lunch_hour)
return_hour = lunch_hour + 1
exit_hour = (8 - (lunch_hour - start_hour)) + (lunch_hour + 1)
(from..to).each do |date|
next if date.saturday? || date.sunday?
create_punch(project, date, start_hour, lunch_hour)
create_punch(project, date, return_hour, exit_hour)
end
end
private
def play_theme!
Thread.new do
player = Audite.new
player.load('rocky_balboa_theme.mp3')
player.start_stream
end
end
def create_punch(project, date, start_hour, end_hour)
puts "Punch for #{date.to_s} - From: #{start_hour} To: #{end_hour}"
visit('/punches/new')
fill_in 'punch[from_time]', with: "#{start_hour}:00"
fill_in 'punch[to_time]', with: "#{end_hour}:00"
fill_in 'punch[when_day]', with: date.to_s
select project, from: 'punch[project_id]'
click_button 'Criar Punch'
end
def login!
visit('/users/sign_in')
fill_in 'E-mail', with: @email
fill_in 'Password', with: @password
click_button 'Sign in'
end
end
end
puncher = RockyBalboa::Puncher.new(ENV['EMAIL'], ENV['PASSWORD'])
puncher.punch!(start_hour: 10, lunch_hour: 13, from: ENV['FROM'], to: ENV['TO'], project: ENV['PROJECT'])