This repository has been archived by the owner on May 29, 2019. It is now read-only.
/
app.rb
72 lines (60 loc) · 1.48 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
module Gaskit
class App < Sinatra::Base
set :root, Gaskit.root
set :sprockets, Sprockets::Environment.new(root) { |env|
env.static_root = root.join('public', 'assets')
}
configure do
sprockets.append_path(root.join('app', 'assets', 'stylesheets'))
sprockets.append_path(root.join('app', 'assets', 'javascripts'))
sprockets.append_path(root.join('app', 'assets', 'images'))
end
helpers do
def asset_path(source)
if Gaskit.env.production?
settings.sprockets.path(source, true, "assets")
else
settings.sprockets.path("#{source}?#{Time.now.to_i}", false, "assets")
end
end
end
before do
if request.media_type == 'application/json'
request.body.rewind
body = request.body.read
params.merge!(ActiveSupport::JSON.decode(body)) unless body.blank?
end
end
get '/' do
erb :dashboard
end
get '/stories' do
json Story.all
end
post '/stories' do
story = Story.new(params)
story.save
json story
end
put '/stories/:id' do
story = Story.get!(params['id'])
story.attributes = params
story.save!
json story
end
delete '/stories/:id' do
story = Story.get!(params['id'])
story.destroy
:ok
end
get '/users' do
User.ensure_i_exist
json User.all
end
private
def json(data)
content_type :js
data.to_json
end
end
end