Skip to content

Commit

Permalink
コントローラの仮実装をし、雰囲気を掴む
Browse files Browse the repository at this point in the history
  • Loading branch information
ayasuda committed Dec 21, 2018
1 parent 70d07af commit 0808803
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
46 changes: 46 additions & 0 deletions app/controllers/clova_controller.rb
@@ -0,0 +1,46 @@
class ClovaController < ApplicationController
def index
clova_request = Clova::Request.parse_request_from(request.body.read)
case
when clova_request.event?
case clova_request.name
when "AudioPlayer.PlayStarted"
# noop
else
# noop
end
when clova_request.intent?
case clova_request.name
when "ReadlistsIntent"
# noop
when "ReadTasksIntent"
# noop
when "StartCheckIntent"
# noop
when "EndCheckIntent"
# noop
when "CheckIntent"
# noop
when "SetDueIntent"
# noop
when "NextIntent"
# noop
when "PrevIntent"
# noop
when "GuideIntent"
# noop
else
# noop
end
when clova_request.launch?
# noop
when clova_request.session_ended?
# noop
else
# something wrong?
# noop
end

render json: {}
end
end
2 changes: 2 additions & 0 deletions app/helpers/clova_helper.rb
@@ -0,0 +1,2 @@
module ClovaHelper
end
37 changes: 37 additions & 0 deletions app/models/clova/request.rb
@@ -1,7 +1,12 @@
require 'forwardable'

module Clova
class Request
extend ::Forwardable
attr_accessor :context, :request, :session, :version

def_delegators :@request, :event?, :intent?, :launch?, :session_ended?, :name, :slots, :payload

class Context
attr_accessor :audio_player, :system

Expand Down Expand Up @@ -113,6 +118,38 @@ def initialize(json)
self.event = Event.new(json&.[]("event"))
self.intent = Intent.new(json&.[]("intent"))
end

def event?
self.type == "EventRequest"
end

def intent?
self.type == "IntentRequest"
end

def launch?
self.type == "LaunchRequest"
end

def session_ended?
self.type == "SessionEndedRequest"
end

def name
case
when event? then "#{self.event.namespace}.#{self.event.name}"
when intent? then self.intent&.name
else ""
end
end

def slots
self.intent&.slots
end

def payload
self.event&.payload
end
end # Request

class Session
Expand Down
2 changes: 2 additions & 0 deletions app/views/clova/index.html.erb
@@ -0,0 +1,2 @@
<h1>Clova#index</h1>
<p>Find me in app/views/clova/index.html.erb</p>
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
post '/clova', to: 'clova#index'
resources :lists do
resources :tasks do
patch 'complete', on: :member
Expand Down
56 changes: 56 additions & 0 deletions test/controllers/clova_controller_test.rb
@@ -0,0 +1,56 @@
require 'test_helper'

class ClovaControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
post(clova_url, params: <<JSON, headers: {"Content-Type": "application/json;charset-UTF-8"})
{
"version": "1.0",
"session": {
"new": false,
"sessionAttributes": {},
"sessionId": "a29cfead-c5ba-474d-8745-6c1a6625f0c5",
"user": {
"userId": "U399a1e08a8d474521fc4bbd8c7b4148f",
"accessToken": "XHapQasdfsdfFsdfasdflQQ7"
}
},
"context": {
"System": {
"application": {
"applicationId": "com.example.extension.pizzabot"
},
"user": {
"userId": "U399a1e08a8d474521fc4bbd8c7b4148f",
"accessToken": "XHapQasdfsdfFsdfasdflQQ7"
},
"device": {
"deviceId": "096e6b27-1717-33e9-b0a7-510a48658a9b",
"display": {
"size": "l100",
"orientation": "landscape",
"dpi": 96,
"contentLayer": {
"width": 640,
"height": 360
}
}
}
}
},
"request": {
"type": "IntentRequest",
"intent": {
"name": "OrderPizza",
"slots": {
"pizzaType": {
"name": "pizzaType",
"value": "ペパロニ"
}
}
}
}
}
JSON
assert_response :success
end
end

0 comments on commit 0808803

Please sign in to comment.