|
1 | 1 | # app.rb
|
2 |
| -require 'sinatra' |
| 2 | +require 'sinatra/base' |
| 3 | +require 'rack/ssl' |
3 | 4 | require 'creatubbles'
|
4 | 5 | require 'json'
|
5 | 6 |
|
6 |
| -enable :sessions |
7 |
| - |
8 |
| -def authenticate! |
9 |
| - token = session['token'] |
10 |
| - if token |
11 |
| - @current_user = |
12 |
| - begin |
13 |
| - @creatubbles_client.connect!(JSON.parse(token)) |
14 |
| - @creatubbles_client.users.me |
15 |
| - rescue => err |
16 |
| - session['token'] = nil |
17 |
| - logger.info("Could not parse session token: #{err}") |
18 |
| - redirect to('/') |
19 |
| - end |
20 |
| - else |
21 |
| - redirect to('/sign_in') |
| 7 | +class CreatubblesExampleApplication < Sinatra::Base |
| 8 | + enable :sessions |
| 9 | + use Rack::SSL |
| 10 | + |
| 11 | + def authenticate! |
| 12 | + token = session['token'] |
| 13 | + if token |
| 14 | + @current_user = |
| 15 | + begin |
| 16 | + @creatubbles_client.connect!(JSON.parse(token)) |
| 17 | + @creatubbles_client.users.me |
| 18 | + rescue => err |
| 19 | + session['token'] = nil |
| 20 | + logger.info("Could not parse session token: #{err}") |
| 21 | + redirect to('/') |
| 22 | + end |
| 23 | + else |
| 24 | + redirect to('/sign_in') |
| 25 | + end |
22 | 26 | end
|
23 |
| -end |
24 | 27 |
|
25 |
| -before do |
26 |
| - @creatubbles_client = Creatubbles::Client.new( |
27 |
| - client_id: ENV['CREATUBBLES_CLIENT_ID'], |
28 |
| - client_secret: ENV['CREATUBBLES_CLIENT_SECRET'], |
29 |
| - api_url: ENV['CREATUBBLES_API_URL'] || Creatubbles::DEFAULT_API_URL) |
30 |
| -end |
| 28 | + before do |
| 29 | + @creatubbles_client = Creatubbles::Client.new( |
| 30 | + client_id: ENV['CREATUBBLES_CLIENT_ID'], |
| 31 | + client_secret: ENV['CREATUBBLES_CLIENT_SECRET'], |
| 32 | + api_url: ENV['CREATUBBLES_API_URL'] || Creatubbles::DEFAULT_API_URL) |
| 33 | + end |
31 | 34 |
|
32 |
| -get '/' do |
33 |
| - @creations = @creatubbles_client.creations.recent |
34 |
| - @title = "Recent creations" |
35 |
| - erb :creations |
36 |
| -end |
| 35 | + get '/' do |
| 36 | + @creations = @creatubbles_client.creations.recent |
| 37 | + @title = "Recent creations" |
| 38 | + erb :creations |
| 39 | + end |
37 | 40 |
|
38 |
| -get '/sign_in' do |
39 |
| - redirect @creatubbles_client.start_code_flow(url('/callback')) |
40 |
| -end |
| 41 | + get '/sign_in' do |
| 42 | + start_url = @creatubbles_client.start_code_flow(url('/callback')) |
| 43 | + logger.info("redirecting to Creatubbles: #{start_url}") |
| 44 | + redirect start_url |
| 45 | + end |
41 | 46 |
|
42 |
| -get '/callback' do |
43 |
| - @creatubbles_client.complete_code_flow(params[:code], url('/callback')) |
44 |
| - logger.info(@creatubbles_client.connection_hash.to_json) |
45 |
| - session['token'] = @creatubbles_client.connection_hash.to_json |
46 |
| - redirect to('/my/creations') |
47 |
| -end |
| 47 | + get '/callback' do |
| 48 | + @creatubbles_client.complete_code_flow(params[:code], url('/callback')) |
| 49 | + logger.info(@creatubbles_client.connection_hash.to_json) |
| 50 | + session['token'] = @creatubbles_client.connection_hash.to_json |
| 51 | + redirect to('/my/creations') |
| 52 | + end |
48 | 53 |
|
49 |
| -before '/my/*' do |
50 |
| - authenticate! |
51 |
| -end |
| 54 | + before '/my/*' do |
| 55 | + authenticate! |
| 56 | + end |
52 | 57 |
|
53 |
| -get '/my/creations' do |
54 |
| - @creations = @current_user.creations |
55 |
| - @title = "My creations" |
56 |
| - erb :creations |
| 58 | + get '/my/creations' do |
| 59 | + @creations = @current_user.creations |
| 60 | + @title = "My creations" |
| 61 | + erb :creations |
| 62 | + end |
57 | 63 | end
|
0 commit comments