-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
125 lines (103 loc) · 2.75 KB
/
app.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
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
require 'rubygems'
require "bundler"
Bundler.setup
require 'sinatra'
require 'haml'
require 'json'
require 'digest/md5'
# Make a "database" if one doesn't already exist
unless File.exist?('database.json')
File.open('database.json', 'w') do |file|
db = {
'1' => {'id' => 1, 'text' => 'Cereal'},
'2' => {'id' => 2, 'text' => 'Paper Towels'},
'3' => {'id' => 3, 'text' => 'Milk'},
}
file.write(JSON.generate(db))
end
end
# Tell Sinatra we will use Haml for templates
set :haml, {:format => :xhtml }
CACHE_FILES = %w(
/js/jquery.js
/js/Jaml-all.js
/js/sammy/sammy.js
/js/sammy/plugins/sammy.title.js
/js/app.js
/js/gears_init.js
)
# Rack::Offline (http://github.com/wycats/rack-offline) does this for you.
# Doing it ourselves here to make sure I understand what is going on.
get '/manifest.cache' do
version = generate_manifest_version CACHE_FILES
# HTML5 requires the cache manifest have the following MIME type
content_type 'text/cache-manifest', :charset => 'utf-8'
<<-EOS
CACHE MANIFEST
# #{version}
CACHE:
#{CACHE_FILES.join("\n")}
/
FALLBACK:
/about /offline
NETWORK:
/api
EOS
end
# Google Gears (needed for IE8) uses a different format of manifest
get "/manifest.json" do
content_type 'application/json', :charset => 'utf-8'
content ={
"betaManifestVersion" => 2, # Latest gears version
"version" => generate_manifest_version(CACHE_FILES),
"entries" => CACHE_FILES.map {|f| {"url" => f}} + [
{ "url" => "/"},
]
}
JSON.generate content
end
get '/' do
haml :index
end
# Page to show when user is offline and tries to access
# a non-offline-able page
get '/offline' do
haml :offline
end
get '/about' do
"<h1>This is a really cool Shopping List application.</h1>"
end
get '/api/items' do
content_type 'application/json', :charset => 'utf-8'
db = JSON.parse(File.read('database.json'))
JSON.generate(db.values)
end
post '/api/items/:id' do |id|
db = load_database
db[id]['text'] = params['text']
write_database db
status 200
end
get '/api/items/:id' do |id|
content_type 'application/json', :charset => 'utf-8'
db = load_database
JSON.generate db[id]
end
helpers do
def load_database
JSON.parse File.read('database.json')
end
def write_database(data = nil)
File.open('database.json', 'w') do |file|
file.write JSON.generate(data)
end
end
def generate_manifest_version(files)
app_root = File.dirname(__FILE__)
files_with_full_paths = files.map {|f| File.join(app_root, 'public', f)}
# Add this file to the digest so changes to the app will cause a new
# manifest to be downloaded by the browser
all_contents = (files_with_full_paths + [__FILE__]).map {|f| File.read(f)}.join("")
Digest::MD5.hexdigest(all_contents)
end
end