-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotify_of_new_gist_comments
executable file
·212 lines (165 loc) · 4.86 KB
/
notify_of_new_gist_comments
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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env ruby
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
require 'date'
require 'erb'
require 'net/smtp'
class RunDetails
def initialize last_run_time_file_location
@this_run_time = DateTime.now
@last_run_time_file_location = last_run_time_file_location
end
def last_run_time
@last_run_time ||= begin
if File.exist?(last_run_time_file_location)
DateTime.parse(File.read(last_run_time_file_location))
else
DateTime.now - 1
end
end
end
def update_last_run_time
File.open(last_run_time_file_location, "w") { |file| file << this_run_time.xmlschema }
end
private
attr_reader :last_run_time_file_location, :this_run_time
end
class GistRepository
def initialize authorization_token
@authorization_header = authorization_token ? "token #{authorization_token}" : nil
end
def gists_with_comments_updated_since username, last_run_time
get_gists_with_comments(username)
.select{ | gist | gist.any_comments_created_or_updated_since? last_run_time }
end
private
def make_request url, options = {}
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request['Accept'] = options.fetch('Accept', 'application/vnd.github.v3+json')
request['Authorization'] = @authorization_header if @authorization_header
response = http.request(request)
if response.code == '200'
JSON.parse(response.body)
else
raise "Error response #{response.code} #{response.body}"
end
end
def get_gists_for_user username
make_request("https://api.github.com/users/#{username}/gists")
.collect { |gist| Gist.new(gist) }
end
def add_comments gists
gists.select(&:has_comments?).collect do | gist |
gist.comments = get_comments(gist)
gist
end
end
def get_gists_with_comments username
add_comments(get_gists_for_user(username))
end
def get_comments gist
make_request gist.comments_url, 'Accept' => 'application/vnd.github.VERSION.text+json'
end
end
class Gist
def initialize attributes
@attributes = attributes
@comments = []
end
def description
attributes["description"]
end
def comments_url
attributes['comments_url']
end
def has_comments?
attributes["comments"] > 0
end
def comments= comments
@comments = comments
end
def html_url
@attributes['html_url']
end
def to_json options = {}
to_hash.to_json options
end
def to_hash
{
"url" => attributes["url"],
"description" => attributes["description"],
"comments" => comments
}
end
def any_comments_created_or_updated_since? datetime
comments_created_or_updated_since(datetime).any?
end
def comments_created_or_updated_since datetime
comments.select do | comment |
DateTime.parse(comment["updated_at"]) >= datetime
end
end
private
attr_reader :attributes, :comments
end
class NotificationBody
attr_reader :gists, :last_run_time
def initialize gists, last_run_time
@gists = gists
@last_run_time = last_run_time
end
def render
ERB.new(template).result(binding())
end
def template
<<-eos
<% gists.each do | gist | %>
<%= gist.description %>
<%= gist.html_url %>
<% gist.comments_created_or_updated_since(last_run_time).each do | comment | %>
<%= comment['user']['login'] %> said:
<%= comment['body_text'] %>
<% end %>
<% end %>
eos
end
end
class NotificationEmail
attr_reader :recipient, :sender, :sender_password, :smtp_server, :body
def initialize recipient, sender, sender_password, smtp_server, body
@recipient = recipient
@sender = sender
@sender_password = sender_password
@smtp_server = smtp_server
@body = body
end
def send
msg = "From: \"Gist Notifications\" <#{sender}>\nTo: #{recipient}\nSubject: New comments on gists\n\n#{body}"
smtp = Net::SMTP.new smtp_server, 587
smtp.enable_starttls
smtp.start(sender.split("@").last, sender, sender_password, :login) do
smtp.send_message(msg, sender, recipient)
end
end
end
username = ARGV[0]
recipient = ARGV[1]
sender = ARGV[2]
sender_password = ARGV[3]
authorization_token = ARGV[4]
smtp_server = ARGV[5] || 'smtp.gmail.com'
last_run_time_file_location = ARGV[5] || "/tmp/gist-notifications-last-run-time"
run_details = RunDetails.new(last_run_time_file_location)
gists = GistRepository.new(authorization_token).gists_with_comments_updated_since(username, run_details.last_run_time)
puts "#{gists.size} updated since #{run_details.last_run_time}"
if gists.any?
notification_body = NotificationBody.new(gists, run_details.last_run_time).render.tap{ | body | puts body }
NotificationEmail.new(recipient, sender, sender_password, smtp_server, notification_body).send
end
run_details.update_last_run_time