/
request.rb
186 lines (147 loc) · 4.83 KB
/
request.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
class AudioPlayer
attr_accessor :offset_in_milliseconds, :player_activity, :stream, :total_in_milliseconds
def initialize(json)
self.offset_in_milliseconds = json&.[]("offsetInMilliseconds")
self.player_activity = json&.[]("playerActivity")
self.stream = json&.[]("stream")
self.total_in_milliseconds = json&.[]("totalInMilliseconds")
end
end # AudioPlayer
class System
attr_accessor :application, :device, :user
class Application
attr_accessor :application_id
def initialize(json)
self.application_id = json&.[]("applicationId")
end
end # Application
class Device
attr_accessor :device_id, :display
class Display
attr_accessor :content_layer, :dpi, :orientation, :size
class ContentLayer
attr_accessor :width, :height
def initialize(json)
self.width = json&.[]("width")
self.height = json&.[]("height")
end
end # ContentLayer
def initialize(json)
self.content_layer = ContentLayer.new(json&.[]("contentLayer"))
self.dpi = json&.[]("dpi")
self.orientation = json&.[]("orientation")
self.size = json&.[]("size")
end
end # Display
def initialize(json)
self.device_id = json&.[]("deviceId")
self.display = Display.new(json&.[]("display"))
end
end # Device
class User
attr_accessor :user_id, :access_token
def initialize(json)
self.user_id = json&.[]("userId")
self.access_token = json&.[]("accessToken")
end
end # User
def initialize(json)
self.application = Application.new(json&.[]("application"))
self.device = Device.new(json&.[]("device"))
self.user = User.new(json&.[]("user"))
end
end # System
def initialize(json)
self.audio_player = AudioPlayer.new(json&.[]("audioPlayer"))
self.system = System.new(json&.[]("System"))
end
end # Context
class Request
attr_accessor :type
# attr for EventRequest
attr_accessor :request_id, :timestamp, :event
# attr for IntentRequest
attr_accessor :intent
class Event
attr_accessor :namespace, :name, :payload
def initialize(json)
self.namespace = json&.[]("namespace")
self.name = json&.[]("name")
self.payload = json&.[]("payload")
end
end # Event
class Intent
attr_accessor :name, :slots
def initialize(json)
self.name = json&.[]("name")
self.slots = json&.[]("slots")
end
end # Intent
def initialize(json)
self.type = json&.[]("type")
self.request_id = json&.[]("requestId")
self.timestamp = json&.[]("timestamp")
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
attr_accessor :new, :session_attributes, :session_id, :user
class User
attr_accessor :user_id, :access_token
def initialize(json)
self.user_id = json&.[]("userId")
self.access_token = json&.[]("accessToken")
end
end # User
def initialize(json)
self.new = json&.[]("new")
self.session_attributes = json&.[]("sessionAttributes")
self.session_id = json&.[]("sessionId")
self.user = User.new(json&.[]("user"))
end
end # Session
def initialize(json)
self.context = Context.new(json&.[]("context"))
self.request = Request.new(json&.[]("request"))
self.session = Session.new(json&.[]("session"))
self.version = json["version"]
end
def self.parse_request_from(request_str)
self.new(JSON.parse(request_str))
end
end # Request
end