Last active
November 28, 2017 08:42
-
-
Save mktakuya/2805a9833df0120352795bf78bc86dc0 to your computer and use it in GitHub Desktop.
苫小牧高専News FAX版のコード断片
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra/base' | |
require 'sinatra/contrib' | |
require "sinatra/reloader" | |
require 'erb' | |
require 'open-uri' | |
require 'nokogiri' | |
require 'pdfkit' | |
class TmNCTNewsWeb < Sinatra::Base | |
get '/fax' do | |
path = "#{params[:category]}/#{params[:post_id]}.html" | |
url = "http://www.tomakomai-ct.ac.jp/#{path}" | |
doc = Nokogiri::HTML(open(url)) | |
title = doc.title | |
post = doc.search('.post.clearfix')[0].to_html | |
erb = ERB.new(File.read(Dir.pwd + '/views/fax.html.erb')) | |
html = erb.result(binding) | |
pdf = PDFKit.new(html, encoding: 'UTF-8') | |
content_type 'application/pdf' | |
pdf.to_pdf | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<title><%= title %></title> | |
</head> | |
<body> | |
<%= post %> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment