Class: DreamingGod::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/dreaming_god/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Session

Returns a new instance of Session.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dreaming_god/session.rb', line 5

def initialize session
  @session = session
  @db_session = "Db#{self.class.to_s.split(':').last}".constantize.where(:userId => session.userId).first
  
  if !@db_session 
    dbs = "Db#{self.class.to_s.split(':').last}".constantize.new
    dbs.sessionId = session.sessionId
    dbs.userId = session.userId
    dbs.save!
    @db_session = dbs
    @new_flag = true
  else
    @new_flag = false
  end
end

Instance Method Details

#audio(url) ⇒ Object

somesite.com/file.mp3



209
210
211
# File 'lib/dreaming_god/session.rb', line 209

def audio url
  "<audio src=\"https://#{url}\" />"
end

#break(length) ⇒ Object

> 3s



214
215
216
# File 'lib/dreaming_god/session.rb', line 214

def break length
  "<break time=\"#{length}\"/> "
end

#imagine(request) ⇒ Object



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
# File 'lib/dreaming_god/session.rb', line 21

def imagine request
  @request = request
  case @request.type
  when 'LaunchRequest'
    # Prepare system
    card = response_card 'Dreaming God'
    ssml_response(speak('Greetings user. Tell me a fact for example, Cat is an animal.'), card, false)
  when 'IntentRequest'
    @intent = Alexagram::Intent.new(@request)
    intent_name = @intent.name.underscore
    case intent_name
    when 'list_rows'
      table_name = @intent.slots['table']['value']  #people
      if !table_name
        result = "What is the name of the table that you want to list?"
        card = response_card result
        ssml_response(speak(result), card, false)
      else
        names = ""
        result = "Db#{(table_name.capitalize.singularize)}".constantize.all.limit(3)
        result.each do |r|
          names << "#{r.label},"
        end
        names = "#{table_name} is empty" if names.length == 0
        res = "Here is the list of #{table_name}, #{names}"
        card = response_card res
        ssml_response(speak(res), card, true)
      end
    when 'create_person'
      result = ""
      table_name = intent_name.split('_').last.pluralize
      name = @intent.slots['personname']['value']   #bob
      person = @intent.slots['person']['value']   #man
      if !name
        result = "What was the name of the #{person}"
      else
        dbp = "Db#{(table_name.capitalize.singularize)}".constantize.new
        dbp.label = name
        dbp.people_type = person
        dbp.save
        result = "Ok. #{name} is a #{person}"
      end
      card = response_card result
      ssml_response(speak("#{result}"), card, true)
    when 'create_place'
      result = ""
      table_name = intent_name.split('_').last.pluralize
      name = @intent.slots['placename']['value']   # bahamas
      place = @intent.slots['place']['value']   # park
      if !name
        result = "What was the name of the #{place}"
      else
        dbp = "Db#{(table_name.capitalize.singularize)}".constantize.new
        dbp.label = name
        dbp.place_type = place
        dbp.save
        result = "Ok. #{name} is a #{place}"
      end
      card = response_card result
      ssml_response(speak("#{result}"), card, true)
    when 'create_thing'
      result = ""
      table_name = intent_name.split('_').last.pluralize
      name = @intent.slots['thingname']['value']   # bananna
      thing = @intent.slots['thing']['value']   # vegetable
      if !name
        result = "What was the name of the #{thing}"
      else
        dbp = "Db#{(table_name.capitalize.singularize)}".constantize.new
        dbp.label = name
        dbp.thing_type = thing
        dbp.save
        result = "Ok. #{name} is a #{thing}"
      end
      card = response_card result
      ssml_response(speak("#{result}"), card, true)
    when 'select_row'
      column_a = @intent.slots['columna']['value'] #all
      case column_a
      when 'thing type'
        column_a = 'thing_type'
      when 'person type'
        column_a = 'person_type'
      when 'place type'
        column_a = 'place_type'
      else
        column_a = (column_a == 'all' ? '*' : "#{column_a}")
      end
      table_name = @intent.slots['table']['value']  #people
      column_b = @intent.slots['columnb']['value'] #name
      compare_value = @intent.slots['comparevalue']['value'] #bob
      sql_string = "SELECT #{column_a} FROM db_#{table_name} WHERE #{column_b} = '#{compare_value}'"
      result = "Db#{(table_name.capitalize.singularize)}".constantize.find_by_sql(sql_string)
      names = ""
      result.each do |r|
        names << "#{r.send(column_a)}, "
      end
      names = "#{table_name} table is empty" if names.length == 0
      card = response_card 'Dreaming God'
      ssml_response(speak("I have found the following, #{names}"), card, true)
    when 'update_row'        
      table_name = @intent.slots['table']['value']  #people
      column_a = @intent.slots['columna']['value'] #label
      value_a = @intent.slots['valuea']['value'] #joe
      column_b = @intent.slots['columnb']['value'] #label
      compare_value = @intent.slots['comparevalue']['value'] #bob
      
      case column_a
      when 'thing type'
        column_a = 'thing_type'
      when 'person type'
        column_a = 'person_type'
      when 'place type'
        column_a = 'place_type'
      else
        column_a = (column_a == 'all' ? '*' : "#{column_a}")
      end
      
      sql_string = "UPDATE db_#{table_name} SET #{column_a} = '#{value_a}' WHERE #{column_b} = '#{compare_value}'"
      result = "Db#{(table_name.capitalize.singularize)}".constantize.find_by_sql(sql_string)
      res = "I have updated the database where #{column_b} is equal to #{compare_value} with #{column_a} set to #{value_a}"
      card = response_card res
      ssml_response(speak(res), card, true)
    when 'destroy_row'
      table_name = @intent.slots['table']['value']  #people
      column = @intent.slots['column']['value'] #label
      compare_value = @intent.slots['comparevalue']['value'] #bob
      
      case column
      when 'thing type'
        column = 'thing_type'
      when 'person type'
        column = 'person_type'
      when 'place type'
        column = 'place_type'
      else
        column = (column == 'all' ? '*' : "#{column}")
      end
      
      sql_string = "DELETE FROM db_#{table_name} where #{column} = '#{compare_value}'"
      "Db#{(table_name.capitalize.singularize)}".constantize.find_by_sql(sql_string)
      res = "I have destroyed rows from the database where #{column} is equal to #{compare_value}"
      card = response_card res
      ssml_response(speak(res), card, true)
    else
    end

  when 'SessionEndedRequest'        
    DbSession.detroy(@db_session.id.to_i)
    result = "End of line"
    card = response_card result
    ssml_response(speak(result), card, true)
  else
    #
  end
end

#is_new?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/dreaming_god/session.rb', line 179

def is_new?
  @new_flag
end

#paragraph(text) ⇒ Object



218
219
220
# File 'lib/dreaming_god/session.rb', line 218

def paragraph text
  "<p>#{text}</p>"
end

#response_card(text = nil) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/dreaming_god/session.rb', line 200

def response_card text=nil
  {
    "type" => "Simple",
    "title" => "Minerva Database",
    "content" => "#{text ||= 'Hello World'}"
  }
end

#sentence(text) ⇒ Object



222
223
224
# File 'lib/dreaming_god/session.rb', line 222

def sentence text
  "<s>#{text}</s>"
end

#speak(text) ⇒ Object



227
228
229
# File 'lib/dreaming_god/session.rb', line 227

def speak text
  "<speak>#{text}</speak>"
end

#ssml_response(text, card, end_session = true) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/dreaming_god/session.rb', line 184

def ssml_response(text, card, end_session=true)
  r = {
    "version" => "1.0",
    "sessionAttributes" => { },
    "response" => {
      "outputSpeech" => {
        "type" => "SSML",
        "ssml" => text
      },
      "shouldEndSession" => end_session
    }
  }
  r["response"]["card"] = card if card
  r.as_json
end