/
app.rb
142 lines (116 loc) · 4.03 KB
/
app.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require 'sinatra'
require 'sinatra/cache'
require 'haml'
require 'json'
require 'exceptio-ruby'
require 'colored'
require 'cocoapods-core'
APP_ROOT = Pathname.new(File.expand_path('../', __FILE__))
$:.unshift((APP_ROOT + 'lib').to_s)
require 'cocoapods_notifier'
module CocoaPodsNotifier
class CocoaPodsNotifierApp < Sinatra::Application
# Setup
#-------------------------------------------------------------------------#
# The path of the RSS file. Served statically as it is located in the public
# folder.
#
RSS_FILE = File.join(APP_ROOT, 'public/new-pods.rss')
# Configurations.
#
configure do
set :root, APP_ROOT.to_s
set :haml, :format => :html5
register Sinatra::Cache
set :cache_output_dir, File.join(APP_ROOT, 'public')
set :cache_enabled, true
if ENV['RACK_ENV'] == 'production'
ExceptIO::Client.configure "cocoapods-feeds-cocoapods-org", ENV['EXCEPTIO_KEY']
end
end
# Development configurations.
#
configure :development do
require 'awesome_print'
require 'sinatra/reloader'
register Sinatra::Reloader
end
Pod::Specification::Set::Statistics.instance.cache_expiration = 60 * 60 * 24
Pod::Specification::Set::Statistics.instance.cache_file = APP_ROOT + 'caches/statistics.yml'
# Repo Actions
#-------------------------------------------------------------------------#
def self.master_repo
@master_repo ||= Repo.new(APP_ROOT + 'tmp/.cocoapods/master')
end
# Clones the master repo from the remote and generates the feeds and
# sends the tweets for Pods of the last commit.
#
# @note If the tweets for the Pods of the last commit where already sent
# twitter will reject them.
#
def self.init
master_repo.setup_if_needed
update
end
#
#
def self.update
master_repo.update
feed = RSS.new(master_repo.pods, master_repo.creation_dates).feed
File.open(RSS_FILE, 'w') { |f| f.write(feed) }
puts '-> RSS feed created'.cyan unless $silent
master_repo.new_pod_names.each { |pod_name| TwitterNotifier.new.tweet(master_repo.pod_named(pod_name)) }
puts "-> Tweeted #{master_repo.new_pod_names.count} pods".cyan unless $silent
rescue Exception => e
puts "[!] update failed: #{e}".red
puts e.backtrace.join("\n")
ExceptIO::Client.log e, ENV['RACK_ENV']
end
# Routes
#-------------------------------------------------------------------------#
# The home page. Shows a link to the feed and the preview of the tweets
# for the last 30 pods.
#
get '/' do
begin
pods = self.class.master_repo.pods
@creation_dates = self.class.master_repo.creation_dates
@pods_count = pods.length
@new_pods = RSS.new(pods, @creation_dates).pods_for_feed
@pods_tweets = {}
@new_pods.each { |pod| @pods_tweets[pod.name] = TwitterNotifier.new.status_for_pod(pod) }
haml :index
rescue Exception => e
puts "[!] get / failed: #{e}".red
puts e.backtrace.join("\n")
ExceptIO::Client.log e, ENV['RACK_ENV']
status 500
end
end
# The secret (TM) hook used by GitHub to trigger an update of the repo.
# It the process the RSS feed is recreated and the tweets for the Pods
# are sent.
#
post "/#{ENV['HOOK_PATH']}" do
begin
start_time = Time.now
payload = JSON.parse(params[:payload])
if payload['ref'] == "refs/heads/master"
self.class.update
cache_expire('/index')
status 201
body "REINDEXED - #{(Time.now - start_time).to_i} seconds"
else
status 200
body "NO UPDATES - #{(Time.now - start_time).to_i} seconds"
end
rescue Exception => e
puts "[!] get /HOOK_PATH failed: #{e}".red
puts e.backtrace.join("\n")
ExceptIO::Client.log e, ENV['RACK_ENV']
status 500
end
end
#-------------------------------------------------------------------------#
end
end