Class: AlexaSkillsRuby::Handler

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/alexa_skills_ruby/handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Handler

Returns a new instance of Handler.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/alexa_skills_ruby/handler.rb', line 9

def initialize(opts = {})
  if opts[:application_id]
    @application_id = opts[:application_id]
  end

  if opts[:logger]
    @logger = opts[:logger]
  end

  certificate_cache = opts[:certificate_cache] || SimpleCertificateCache.new
  @skip_signature_validation = !!opts[:skip_signature_validation]
  @signature_validator = SignatureValidator.new(certificate_cache)

  if opts[:root_certificates]
    @signature_validator.add_certificate_authorities([opts[:root_certificates]].flatten)
  end
end

Instance Attribute Details

#application_idObject

Returns the value of attribute application_id.



7
8
9
# File 'lib/alexa_skills_ruby/handler.rb', line 7

def application_id
  @application_id
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/alexa_skills_ruby/handler.rb', line 7

def logger
  @logger
end

#requestObject (readonly)

Returns the value of attribute request.



6
7
8
# File 'lib/alexa_skills_ruby/handler.rb', line 6

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/alexa_skills_ruby/handler.rb', line 6

def response
  @response
end

#sessionObject (readonly)

Returns the value of attribute session.



6
7
8
# File 'lib/alexa_skills_ruby/handler.rb', line 6

def session
  @session
end

#skip_signature_validationObject

Returns the value of attribute skip_signature_validation.



7
8
9
# File 'lib/alexa_skills_ruby/handler.rb', line 7

def skip_signature_validation
  @skip_signature_validation
end

Class Method Details

.on_authenticate(&block) ⇒ Object



87
88
89
# File 'lib/alexa_skills_ruby/handler.rb', line 87

def self.on_authenticate(&block)
  set_callback :authenticate, :before, block
end

.on_intent(intent_name = nil, &block) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/alexa_skills_ruby/handler.rb', line 103

def self.on_intent(intent_name = nil, &block)
  opts = {}
  if intent_name
    opts[:if] = -> { request.intent_name == intent_name }
  end
  set_callback :intent, :before, block, opts
end

.on_launch(&block) ⇒ Object



95
96
97
# File 'lib/alexa_skills_ruby/handler.rb', line 95

def self.on_launch(&block)
  set_callback :launch, :before, block
end

.on_session_end(&block) ⇒ Object



99
100
101
# File 'lib/alexa_skills_ruby/handler.rb', line 99

def self.on_session_end(&block)
  set_callback :session_end, :before, block
end

.on_session_start(&block) ⇒ Object



91
92
93
# File 'lib/alexa_skills_ruby/handler.rb', line 91

def self.on_session_start(&block)
  set_callback :session_start, :before, block
end

.on_verify_signature(&block) ⇒ Object



83
84
85
# File 'lib/alexa_skills_ruby/handler.rb', line 83

def self.on_verify_signature(&block)
  set_callback :verify_signature, :before, block
end

Instance Method Details

#handle(request_json, request_headers = {}) ⇒ Object



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
# File 'lib/alexa_skills_ruby/handler.rb', line 31

def handle(request_json, request_headers = {})
  @skill_request = JsonObjects::SkillsRequest.new(MultiJson.load(request_json))
  @skill_response = JsonObjects::SkillsResponse.new

  @session = @skill_request.session
  @request = @skill_request.request
  @response = @skill_response.response

  run_callbacks :verify_signature do
    unless @skip_signature_validation
      cert_chain_url = request_headers['SignatureCertChainUrl'].to_s.strip
      signature = request_headers['Signature'].to_s.strip
      if cert_chain_url.empty? || signature.empty?
        raise AlexaSkillsRuby::ConfigurationError, 'Missing "SignatureCertChainUrl" or "Signature" header but signature validation is enabled'
      end
      @signature_validator.validate(request_json, cert_chain_url, signature)
    end
  end

  timestamp_diff = (Time.now - Time.iso8601(@request.timestamp)).abs
  raise TimestampValidationError, "Invalid timstamp" if timestamp_diff > 150

  run_callbacks :authenticate do
    if @application_id
      if @application_id != session.application.application_id
        raise InvalidApplicationId, "Invalid: [#{session.application.application_id}]"
      end
    end
  end

  if session.new
    run_callbacks :session_start
  end

  case request
    when JsonObjects::LaunchRequest
      run_callbacks :launch
    when JsonObjects::IntentRequest
      run_callbacks :intent
    when JsonObjects::SessionEndedRequest
      run_callbacks :session_end
  end

  if response.should_end_session
    @skill_response.session_attributes = {}
  else
    @skill_response.session_attributes = session_attributes
  end

  MultiJson.dump(@skill_response.as_json)
end

#session_attributesObject



27
28
29
# File 'lib/alexa_skills_ruby/handler.rb', line 27

def session_attributes
  @session.attributes ||= {}
end