-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.rb
More file actions
53 lines (46 loc) · 1.61 KB
/
app.rb
File metadata and controls
53 lines (46 loc) · 1.61 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
require 'json'
require 'rubygems'
require 'sinatra'
require 'haml'
require './colors'
configure do
set :public_folder, Proc.new { File.join(root, "static") }
enable :sessions
end
get '/' do
haml :index
end
# json endpoint to return your image data
get '/attachable_images' do
# for a normal app, this should return a list of images pulled from
# a database or filesystem or other storage location
#
# for this demo, we return a random list of image pointers
# using placehold.it as our "repo"
content_type :json
images = 5.times.map do |x|
ht = (x + 5) * 10
foreground = Colors.rand_light_color
background = Colors.rand_dark_color
{ file: "http://placehold.it/#{ht}/#{foreground}/#{background}",
caption: "This image is #{ht}x#{ht} and uses colors ##{foreground} and ##{background}"
}
end
images.to_json
end
post '/upload' do
content_type :html
# Here you'd need to add code to handle the image upload give that you've
# submitted the image via a multipart-form. You might push the file to the
# filesystem or S3 or elsewhere
# then return the data - jqueryupload looks for json in an html document (seems a bit odd...)
#
# as you can see, here we're not actually uploading anything, but simply returning another
# fake response acting as if we've successfully uploaded the image.
background = Colors.rand_light_color
foreground = Colors.rand_dark_color
'<html><body>%s</body></html>' % { :file => "http://placehold.it/300x50/#{foreground}/#{background}&text=uploaded",
:caption =>'This image was uploaded at ' + Time.now.to_s,
:status => 1
}.to_json
end