-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprem.rb
More file actions
50 lines (37 loc) · 1.17 KB
/
Copy pathprem.rb
File metadata and controls
50 lines (37 loc) · 1.17 KB
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
# myapp.rb
require 'rubygems' # optional for Ruby 1.9 or above.
require 'premailer'
require 'sinatra'
require 'json'
before do
content_type :json
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']
end
post '/premailer/' do
tempFile = "input" + Time.now.strftime('%Y%m%d%H%M%S%L') + ".html"
File.open("public/" + tempFile, "w") do |f|
f.puts request.POST["html"]
end
premailer = Premailer.new("http://localhost:#{request.port}/#{tempFile}", :warn_level => Premailer::Warnings::SAFE)
# Write the HTML output
#File.open("public/output.html", "w") do |fout|
# fout.puts premailer.to_inline_css
#end
# Write the plain-text output
#File.open("public/output.txt", "w") do |fout|
# fout.puts premailer.to_plain_text
#end
File.delete("public/" + tempFile)
# Output any CSS warnings
warnings = Array.new
premailer.warnings.each do |w|
warnings << "#{w[:message]} (#{w[:level]}) may not render properly in #{w[:clients]}"
end
response = {
:html => premailer.to_inline_css,
:plain_text => premailer.to_plain_text,
:warnings => warnings
}
response.to_json
end