-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_client.rb
63 lines (53 loc) · 1.56 KB
/
api_client.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
require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'json'
require 'pp'
require 'liquid'
$LOAD_PATH.unshift File.dirname(File.expand_path(__FILE__)) + '/lib'
require 'family'
# please go to http://www.geni.com/apps to obtain an app_id and app_secret
APP_ID = '<app-id>'
APP_SECRET = '<app-secret>'
SITE = 'https://www.geni.com'
get '/' do
liquid :index
end
get '/auth' do
redirect client.web_server.authorize_url(
:redirect_uri => redirect_uri
)
end
get '/callback' do
token = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri.tap{|ii| puts ii})
response.set_cookie('token', :value => token.token)
redirect '/family'
end
get '/family' do
redirect '/' unless request.cookies['token']
begin
token = OAuth2::AccessToken.new(client, request.cookies['token'])
node_id = "-#{params[:node_id]}" if params[:node_id]
family = Family.new(token.get("/api/profile#{node_id}/immediate-family"))
liquid :family, :locals => {:handprint => (family && family.handprint), :focus => (family && family.focus_name) }
rescue OAuth2::HTTPError => e
body = JSON.parse(e.response.body)
if body.is_a?(Hash) and body.key?('error')
message = body['error']['message']
end
liquid :family, :locals => {:error => e.message, :message => message}
end
end
def client
OAuth2::Client.new(APP_ID, APP_SECRET,
:site => SITE,
:parse_json => true,
:access_token_path => '/oauth/token'
)
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/callback'
uri.query = nil
uri.to_s
end