-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
43 lines (39 loc) · 1.15 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
require 'sinatra'
require 'json'
require 'firebase'
require 'nestful'
require 'dotenv'
require 'stathat'
require_relative 'lib/iamchucknorris.rb'
enable :sessions
configure do
Dotenv.load if settings.development?
Firebase.base_uri = "https://glio-mxit-users.firebaseio.com/#{ENV['MXIT_APP_NAME']}/"
end
get '/' do
begin
create_user unless get_user
track_login
@joke = JSON.load(Nestful.get('http://api.icndb.com/jokes/random?exclude=[explicit,nerdy]').body)["value"]["joke"]
@mixup_ad = Nestful.get("http://serve.mixup.hapnic.com/#{ENV['MXIT_APP_NAME']}").body
StatHat::API.ez_post_count('iamchucknorris - jokes requested', 'emile@silvis.co.za', 1)
erb :joke
rescue
erb "Oops, something went wrong. <a href='/'>Try again</a> later."
end
end
helpers do
def get_user
mxit_user = MxitUser.new(request.env)
data = Firebase.get(mxit_user.user_id).response.body
data == "null" ? nil : data
end
def create_user
mxit_user = MxitUser.new(request.env)
Firebase.set(mxit_user.user_id, {:date_joined => Time.now})
end
def track_login
mxit_user = MxitUser.new(request.env)
Firebase.update(mxit_user.user_id, {:last_login => Time.now})
end
end