-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt_master.rb
executable file
·232 lines (184 loc) · 5.6 KB
/
prompt_master.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env ruby
# Require all gems in Gemfile
require "bundler/setup"
Bundler.require
# require classes recursively
Dir[File.dirname(__FILE__) + "/lib/*.rb"].each { |file| require file }
# Sinatra base
class PromptMaster < Sinatra::Base
register Sinatra::ActiveRecordExtension
ActiveSupport::Deprecation.silenced = true
# set root
set :root, File.dirname(__FILE__)
# set public folder
set :public_folder, proc { File.join(root, "public") }
# set views
set :views, proc { File.join(root, "views") }
# use better errors
configure :development do
use BetterErrors::Middleware
BetterErrors.application_root = __dir__
end
helpers do
# access app secrets from ./secrets.yml
def secret(key)
@secrets ||= YAML.load_file(File.join(__dir__, "secrets.yml"))
@secrets[key] || ENV[key] || ""
end
end
# set port
set :port, 8080
error do
erb :"500"
end
scheduler = Rufus::Scheduler.new
# sync everything afret start
scheduler.in "1s" do
Utilities.merge_images
Utilities.cleanup_db
end
scheduler.in "3s" do
Utilities.sync_db
end
#----------------#
# API ROUTES #
#----------------#
# delete category
delete "/category/:id" do
content_type :json
@category = Category.find_by_id(params[:id])
# respond with error if category not found
return {success: false, error: "Category not found"}.to_json if @category.nil?
# delete category
@category.destroy
{success: true}.to_json
end
# add tag to featured tags
put "/tag/:id/feature" do
content_type :json
@tag = Tag.find_by_id(params[:id])
# respond with error if tag not found
return {success: false, error: "Tag not found"}.to_json if @tag.nil?
# add tag to featured tags
@tag.update(featured: true)
{success: true}.to_json
end
# remove tag from featured tags
put "/tag/:id/unfeature" do
content_type :json
@tag = Tag.find_by_id(params[:id])
# respond with error if tag not found
return {success: false, error: "Tag not found"}.to_json if @tag.nil?
# remove tag from featured tags
@tag.update(featured: false)
{success: true}.to_json
end
# hide tag
put "/tag/:id/hide" do
content_type :json
@tag = Tag.find_by_id(params[:id])
# respond with error if tag not found
return {success: false, error: "Tag not found"}.to_json if @tag.nil?
# hide tag
@tag.update(active: false)
{success: true}.to_json
end
# unhide tag
put "/tag/:id/unhide" do
content_type :json
@tag = Tag.find_by_id(params[:id])
# respond with error if tag not found
return {success: false, error: "Tag not found"}.to_json if @tag.nil?
# unhide tag
@tag.update(active: true)
{success: true}.to_json
end
# rate tag
put "/tag/:id/rate" do
content_type :json
@tag = Tag.find_by_id(params[:id])
# respond with error if tag not found
return {success: false, error: "Tag not found"}.to_json if @tag.nil?
# rate tag
@tag.update(rank: params[:rank])
{success: true}.to_json
end
# serve static files from inspiration folder
get "/inspiration/*" do
send_file "./inspiration/#{params[:splat].first}"
end
# serve static files from inspiration folder
get "/api/inspiration/*" do
send_file "./inspiration/#{params[:splat].first}"
end
# API
# get all categories
get "/api/categories" do
content_type :json
# Get all categories with first tag avoiding N+1 queries
Category.includes(:tags).reorder("name ASC").map do |category|
{
id: category.id,
name: category.name,
image: category.image,
image_size: category.image_size,
tags_count: category.tags.size,
sets_count: category.tags.first&.images&.count || 0
}
end.to_json
end
# get category by id
get "/api/category/:id" do
content_type :json
@category = Category.find_by_id(params[:id])
# respond with error if category not found
return {success: false, error: "Category not found"}.to_json if @category.nil?
# Get category with first tag avoiding N+1 queries
{
id: @category.id,
name: @category.name,
image: @category.image,
image_size: @category.image_size,
tags_count: @category.tags.size,
sets_count: @category.tags.first&.images&.count || 0
}.to_json
end
# get category tags
get "/api/category/:id/tags" do
content_type :json
@category = Category.find_by_id(params[:id])
# respond with error if category not found
return {success: false, error: "Category not found"}.to_json if @category.nil?
# Get category tags
@category.tags.map(&:as_json).to_json
end
# update tag
put "/api/tag/:id" do
content_type :json
@tag = Tag.find_by_id(params[:id])
# respond with error if tag not found
return {success: false, error: "Tag not found"}.to_json if @tag.nil?
# filter allowed params (rank, active, featured)
params = request.body.read
params = JSON.parse(params)
params = params.select { |k, v| %w[rank active featured].include?(k) }
# update tag
@tag.update(params)
# respond with updated tag
@tag.to_json
end
# delete tag from database and all associated images from disk
delete "/api/tag/:id" do
content_type :json
@tag = Tag.find_by_id(params[:id])
# respond with error if tag not found
return {success: false, error: "Tag not found"}.to_json if @tag.nil?
# delete tag
@tag.destroy
{success: true}.to_json
end
# root route responds with index.html from public folder
get "/*" do
send_file File.join(settings.public_folder, "index.html")
end
end