-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rb
72 lines (60 loc) · 1.58 KB
/
main.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
require 'rubygems'
require 'sinatra'
require 'mongo_mapper'
require 'pony'
require 'nokogiri'
require 'open-uri'
require './model'
TPMI_SMTP_OPTIONS = {
:address => "smtp.sendgrid.net",
:port => "587",
:authentication => :plain,
:user_name => '[email protected]',
:password => 'sendwiki',
:domain => 'www.sendwiki.com',
}
def send_email(to, subject, html_body)
Pony.mail(:to => to, :from => 'SendWiki <[email protected]>',
:subject => subject,
:html_body => html_body,
:via => :smtp, :via_options => TPMI_SMTP_OPTIONS
)
end
get '/' do
erb :main
end
post '/' do
email = params[:email]
wikipedia_article = params[:article]
article_object = Article.create({
:article => wikipedia_article,
:email => email
})
article_object.save
# parse the HTML from Wikipedia
article = Nokogiri::HTML(open(wikipedia_article))
subject = article.css('#content h1')
article = article.css('#bodyContent')
# remove stuff that doesn't look great in email
article.css('#coordinates').remove
article.css('#toc').remove
article.css('#jump-to-nav').remove
article.css('.infobox').remove
article.css('script').remove
article.css('.editsection').remove
article.css('.navbox').remove
# Fix links
article.css('#bodyContent a').each do |a|
a['href'] = "http://en.wikipedia.org#{a['href']}"
end
# Handle images
article.css('img').each do |img|
img.remove_attribute('alt')
end
content = article.to_html
send_email(email, subject.text, content)
redirect '/sent'
end
get '/sent' do
erb :sent
end