/
Rakefile
64 lines (50 loc) · 1.65 KB
/
Rakefile
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
require "dotenv"
require "pry"
require "httparty"
require 'json'
require 'cgi'
namespace :assets do
task :precompile do
sh 'middleman contentful --rebuild'
sh 'middleman build'
end
end
desc "Auto update cron job"
task :cron do
Dotenv.load
@content_url_base = "https://cdn.contentful.com/spaces/#{ENV['CONTENTFUL_SPACE']}/sync?"
@content_url = "#{@content_url_base}access_token=#{ENV['CONTENTFUL_ACCESS_TOKEN']}".gsub(/\s+/, "")
@source_url = "https://github.com/#{ENV['GIT_USER']}/
#{ENV['GIT_REPO']}/archive/
#{ENV['GIT_BRANCH']}.tar.gz".gsub(/\s+/, "")
@build_url = "https://api.heroku.com/apps/#{ENV['HEROKU_APP']}/builds"
sync_token_file = ".sync_token"
if File.file?(sync_token_file)
@sync_token = File.read(sync_token_file)
response = HTTParty.get(@content_url+"&sync_token=#{@sync_token}")
else
FileUtils.rmdir 'data/site'
response = HTTParty.get(@content_url+"&initial=true")
intial_content = JSON.parse(response)
@sync_token = CGI::parse(intial_content["nextSyncUrl"]
.gsub(@content_url_base, ""))["sync_token"][0]
File.open(sync_token_file, 'w') {|f| f.write(@sync_token) }
end
site_content = JSON.parse(response)
if site_content["items"]
build_options = {
:body => {
:source_blob => {
:url => @source_url,
:version => @sync_token
}
}.to_json,
:headers => {
"Accept" => "application/vnd.heroku+json; version=3",
"Content-Type" => "application/json",
"Authorization" => "Bearer #{ENV['HEROKU_AUTH']}"
}
}
build_response = HTTParty.post(@build_url,build_options)
end
end