-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
69 lines (61 loc) · 2.27 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
# frozen_string_literal: true
# This program is an Alexa skill web service for Fibromyalgia sufferers.
# It is a Sinatra web application that communicates requests and responses to Amazon.
# This web application sits behind the https://github.com/stevenbeales/fibro-friend Alexa skill
#
# Author:: Steven Beales (mailto:[email protected])
# Copyright:: Copyright (c) 2018 Ardint
# License:: MIT
require_relative 'init'
# Namespace App under Sinatra
module Sinatra
# Main App Class and Entry Point
class MyApp < Sinatra::Base
register Sinatra::Fibro
register Sinatra::Contrib
register Sinatra::Initializers
register Sinatra::ActiveRecordExtension
helpers Sinatra::CustomLogger
set :root, File.dirname(File.expand_path(__FILE__))
set :protection, except: :json_csrf
set :server, :puma
set :log_file, settings.root + AppConstants::LOG_FILE
enable :sessions
enable :logging
# Rack request logging
configure do
file = File.new(settings.log_file, 'a+')
file.sync = true # ensure file flushes to disk automatically
use Rack::CommonLogger, file
end
# Pre process requests from Amazon Alexa.
before do
if request.request_method == "POST"
content_type :json, 'charset' => 'utf-8'
Bugsnag.notify("Received request with headers:\n#{request.env}")
@data = request.body.read
begin
params.merge!(::JSON.parse(@data))
# The request object.
@echo_request = AlexaWebService::Request.new(::JSON.parse(@data))
# Store logged in Alexa user
@user = User.authenticate(@echo_request.user_id)
# If the request body has been read, you need to rewind it.
request.body.rewind
AlexaWebService::Verify.new(request.env, request.body.read)
production? && (halt 400, verified.to_s unless verified == "OK")
rescue StandardError => exception
Bugsnag.notify(exception)
end
end
end
post '/' do
# build_response helper method is registered in lib/sinatra/fibro module
response = build_response(@echo_request, AlexaWebService::Response.new)
response.post
end
# $0 is the executed file
# __FILE__ is the current file
run! if $PROGRAM_NAME == __FILE__
end
end