-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.rb
executable file
·66 lines (55 loc) · 1.66 KB
/
run.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
#!/home/nessa/.rvm/rubies/ruby-2.1.5/bin/ruby
# /usr/bin/env ruby
require 'bundler/setup'
require 'httparty'
require 'date'
class ChangeTracker
attr_reader :url, :path
attr_accessor :file, :draft
def initialize
# Url to the file that needs tracking
# It can be any file type: css, js, xml
@url = 'http://yourwebsite.com/path/main.html'
# Name these two files with corresponding extension
@draft = 'draft.html'
@file = 'main.html'
# Since cron runs at the root
# We need to specify a path to the monitor folder
@path = '/path/to/local/folder/'
end
def find_changes
file = self.file
draft = self.draft
path = self.path
message = "Changes detected #{DateTime.now}"
# The script writes the file into the draft
content = HTTParty.get(self.url)
File.open("#{path}#{draft}", 'w') do |f|
f.write(content)
f.close
end
# Use this block to ignore any word or line
# that constantly changes and doesn't need to be tracked
ignored_lines = ['word1' 'line2', 'phrase3']
File.open("#{path}#{file}", 'w') do |f|
File.readlines("#{path}#{draft}").each do |line|
ignored_lines.each do |ignore|
f.write(line) unless line.include?(ignore)
end
end
f.close
end
diff = `cd #{path} && git diff #{file}`
unless diff.empty?
add = system "cd #{path} && git add #{file}"
if add
commit = system "cd #{path} && git commit -m '#{message}'"
if commit
system "cd #{path} && git push --quiet origin master"
end
end
end
end
end
change_tracker = ChangeTracker.new
change_tracker.find_changes