forked from dachisgroupuk/Bookler
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbooker.rb
More file actions
69 lines (62 loc) · 1.71 KB
/
booker.rb
File metadata and controls
69 lines (62 loc) · 1.71 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'initialiser'
get '/book/:id' do
@book = Book.find(params[:id])
haml :book
end
post '/' do
feedurl = params[:feedurl]
dname = params[:dname]
dpass = params[:dpass]
delicious = Rdelicious.new(dname, dpass)
posts = []
@chapters = []
feed = FeedNormalizer::FeedNormalizer.parse open(feedurl)
feed.entries.each do |post|
puts post.urls.first.index(/[jpg|png|gif]/)
puts post.urls.first
if ((post.urls.first.include? 'jpg') || (post.urls.first.include? 'png') || (post.urls.first.include? 'gif'))
@chapters.push("content"=>"<img src=\""+post.urls.first+"\">")
else
url = 'http://felixcohen.co.uk/readability.php?url='+post.urls.first
begin
page = open(url)
rescue StandardError=>e
puts "Error: #{e}"
else
text = page.read
ensure
puts url
end
@chapters.push("title"=>post.title,"content"=>text)
end
delicious.add(post.urls.first,post.title,'','madeintoabook') if delicious.is_connected?
end
template = File.read('views/book.haml')
haml_engine = Haml::Engine.new(template)
output = haml_engine.render(Object.new, :@chapters => @chapters)
puts output
file = "/tmp/"+feedurl+".pdf"
prince = Prince.new
prince.add_style_sheets("views/print.css")
prince.html_to_file(output, file)
send_file(
file,
:filename => '/tmp/'+feedurl+'.pdf',
:type => 'application/pdf'
)
end
get '/' do
haml :index
end
get '/about' do
haml :about
end
get '/books' do
@books = Book.all :limit => 10
haml :books
end
post '/book' do
book = Book.create(:title => params[:title], :url => params[:feedurl])
book.save
redirect '/book/'+book.id.to_s
end