Test Setup Failed
Push — master ( c4da73...8c2db5 )
by Steven
01:54 queued 21s
created

App.set_user!   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
# frozen_string_literal: true
2
3
# This program is an Alexa skill web service for Mental Health Assessments.  
4
# It is a Sinatra web application that uses Ralyxa to communicate requests and responses to Amazon.
5
# It contains functionality to import and manage surveys and instruments.
6
# It allows an Amazon user to take an assessment by saying "open Mental Health". 
7
# This web application sits behind the https://github.com/stevenbeales/mental-health Alexa skill 
8
# 
9
# Author::    Steven Beales  (mailto:[email protected])
10
# Copyright:: Copyright (c) 2018 Ardint
11
# License::   MIT
12
13
ENV['RACK_ENV'] ||= 'development' 
14
15
require 'sinatra'
16
require 'ralyxa'
17
require 'sinatra-initializers'
18
require_relative 'config/db'
19
require_relative 'app/services/init'
20
require_relative 'app/models/init'
21
22
configure { set :server, :puma }
23
set :app_file, __FILE__
24
set :root, File.dirname(__FILE__)
25
set :public_folder, File.dirname(__FILE__) + '/public'
26
27
# This class is the main entry point to Application.
28
# Takes an incoming Alexa requests and dispatches
29
# to a matching intent in intents folder
30
class App < Sinatra::Base
31
  
32
  # Register initializers a la Rails 
33
  register Sinatra::Initializers
34
35
  # Entry point for requests from Amazon Alexa. 
36
  # The incoming requests are dispatched to intents in the intents folder by Ralyxa.
37
  post '/' do
38
    # store user id in settings
39
    set_user!(request.user_id) 
40
    Ralyxa::Skill.handle(request)
41
  end
42
43
  private
44
 
45
  # Authenticate user. If user doesn't exist, still authenticate user and add them to database
46
  def set_user!
47
    user = User.authenticate(request.user_id)
48
    set :userid, user.id
49
  end
50
end
51