forked from mikekelly/hal-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
70 lines (59 loc) · 1.66 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
require 'sinatra'
require 'slim'
require 'httparty'
require 'addressable/template'
require 'redcarpet'
require 'sinatra/partial'
require File.expand_path(File.dirname(__FILE__) + '/config')
set :partial_template_engine, :slim
enable :partial_underscores
enable :sessions
get '/' do
slim :index
end
get '/explorer' do
if(params[:root])
session[:current_root] = params[:root]
session[:current_api_token] = nil
redirect to('/explorer')
else
@api_token = session[:current_api_token]
@api_root = session[:current_root]
slim :explorer
end
end
get '/logout' do
session[:current_api_token] = nil
session[:current_root] = nil
redirect '/'
end
# Used to find rels
def dtime_href_for(my_env, rel)
Addressable::Template.new(settings.api_links[my_env][rel]['href'])
end
# Redirect to OAuth page
get '/login/:my_env' do
my_env = (params[:my_env] || :dev).to_sym
session[:current_root] = settings.api_root[my_env]
redirect dtime_href_for(my_env, 'dtime:developers:oauth:authorize').expand({
client_id: settings.api_oauth_client[my_env][:client_id],
redirect_uri: url("/oauth/callback?my_env=#{my_env}")
}).to_str, 303
end
# Get back OAuth token, redirect to explorer
get '/oauth/callback' do
my_env = (params[:my_env] || :dev).to_sym
if params[:error]
redirect '/'
else
token = HTTParty.post(
dtime_href_for(my_env, 'dtime:developers:oauth:token').expand({
code: params[:code],
client_id: settings.api_oauth_client[my_env][:client_id],
client_secret: settings.api_oauth_client[my_env][:client_secret],
})
)
session[:current_api_token] = token["token"]
redirect '/explorer'
end
end