-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
90 lines (68 loc) · 1.74 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'sinatra'
require 'omniauth'
require 'omniauth-twitter'
require 'haml'
require 'json'
require 'data_mapper'
require 'ruby-debug'
require './lib/models'
class IOU < Sinatra::Application
set :root, File.dirname(__FILE__)
set :haml, { format: :html5 }
set :sass, { style: :compact, debug_info: false }
enable :sessions
def self.load_config(file)
if File.exist? file
yaml = YAML.load_file file
set yaml
end
end
configure do
load_config "./config/twitter.yml"
end
use OmniAuth::Builder do
provider :twitter, IOU.settings.consumer_key, IOU.settings.consumer_secret
end
helpers do
def current_user
@current_user ||= User.get(session[:user_id]) if session[:user_id]
end
end
before do
unless ['/sign_in', "/auth/twitter", "/auth/twitter/callback"].include? request.path_info
redirect '/sign_in' unless current_user
end
end
get '/sign_in' do
haml :sign_in
end
get '/auth/twitter/callback' do
auth = request.env['omniauth.auth']
user = User.first(uid: auth["uid"]) || User.create(provider: auth["provider"], uid: auth["uid"], name: auth["info"]["name"], nickname: auth["info"]["nickname"])
session[:user_id] = user.id
redirect '/'
end
get '/session/destroy' do
session[:user_id] = nil
redirect '/sign_in'
end
get '/' do
haml :index
end
get '/api/debtors' do
content_type :json
current_user.debtors.all_with_tallies.to_json
end
post '/api/debtors' do
end
post '/api/tallies' do
content_type :json
params = request.body.read
tally = Tally.new(JSON.parse(params))
if tally.save
tally.to_json#, status: 201
end
#
# record.to_json
end
end