alexa-rails 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 313cea798184b82bb7e8ad3a18c2948508dc22f7
4
- data.tar.gz: 49d36e905f428bb11a5023512d86888bb8f07701
2
+ SHA256:
3
+ metadata.gz: 37d49fb2475d98d5c65ab9a9b383e04d59354a2f2301bc80b6e73daa68edb07c
4
+ data.tar.gz: 0f24851a1b5d60b2801749a2aa710e6abbab29e56473fb8abc91a6340b61419e
5
5
  SHA512:
6
- metadata.gz: 1d057ed83fa856bde32cbf5411d818330a7060bed6c98bea6604a96e2d20106abd9a5aa5a8c690eb09df80cc49163dc5c5261de8819e0261f80b160f8fcd6917
7
- data.tar.gz: ab87cd7060f2fcced5ec2d9ca79023e722e1b8f346817b1b44d2a4b889eb2ddd0e476498b2fc033602927aab040bb14ef67b0fe7692e29e9eb750b2194fc806b
6
+ metadata.gz: 52e1ca2f604b126d3013d739c9c70b7889cac16d6824aa9f947fc2c17b05550820e2c90e7083918e28be548f7b24f39a4f47f4878fa856028dd792a6a835d532
7
+ data.tar.gz: df7368c54678c6a1bb731c934b186df3db0a52fbdf6244d3690334e60fd09eaaf3fb385480e1ace1feeb50a8cabc54a06d809d6246a345b825136e80d86ae56d
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
- # Alexa
1
+ # Alexa-rails
2
2
  `alexa-rails` is a ruby gem which is a mountable rails engine that will add abilities to your Ruby on Rails application to handle Amazon alexa requests and responses.
3
3
 
4
4
  ## Intallation/Usage
5
5
 
6
- Do the usual by adding the following to your Gemfile:
6
+ Do the usual by adding the following to your `Gemfile`:
7
7
 
8
8
  ```ruby
9
- gem install alexa-rails
9
+ gem 'alexa-rails'
10
10
  ```
11
11
 
12
12
  ### Migrations
@@ -33,9 +33,7 @@ Set alexa skill IDs in environment config (ex: config/environments/development.r
33
33
  "amzn1.ask.skill.xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
34
34
  ]
35
35
 
36
- config.x.alexa.default_card_title = [
37
- "Alexa rails"
38
- ]
36
+ config.x.alexa.default_card_title = "Alexa rails"
39
37
  ```
40
38
 
41
39
  Mount the engine for routes handling in your routes
@@ -49,15 +47,17 @@ Mount the engine for routes handling in your routes
49
47
  end
50
48
  ```
51
49
 
52
- ### Request handling
50
+ This will make you Rails app accept `POST` requests from Alexa at
51
+ ```
52
+ https://<your-domain>/alexa/intent_handlers
53
+ ```
54
+ This has to be provided as the HTTPS endpoint URL for the skill.
53
55
 
54
- After the above steps, your application is ready to accept requests from Alexa
55
- servers at `/alexa/intent_handlers`.
56
- You will have to provide that in the HTTPS endpoint URL for your skill.
56
+ ### Request handling
57
57
 
58
58
  To handle an intent, you will have to create an intent handler class.
59
59
  For example, if your intent is named `PlaceOrder`, you will have to create
60
- the following file under you `app/lib/intent_handlers` directory.
60
+ the following file under you `app/lib/alexa/intent_handlers` directory.
61
61
 
62
62
  ```ruby
63
63
  module Alexa
@@ -65,6 +65,7 @@ module Alexa
65
65
  class PlaceOrder < Alexa::IntentHandlers::Base
66
66
  def handle
67
67
  ...
68
+ response # intent handlers should always return the +response+ object
68
69
  end
69
70
  end
70
71
  end
@@ -73,12 +74,21 @@ end
73
74
 
74
75
  All intent handlers should contain a `#handle` method that has required logic
75
76
  as to how to handle the intent request. For example, adding session variables,
76
- setting response to elicit slots, etc.
77
+ setting response to elicit slots, delegate etc.
78
+
79
+ **Note**: `#handle` method should always return the `response` object.
80
+ `response` object in available in the scope of `#handle`.
77
81
 
78
- Adding session variable:
82
+ See [Handling Amazon's Built-in Intents / Other request types] section to see
83
+ how to handle Amazon's built-in intent and other requests types.
84
+
85
+ #### Adding session variable:
79
86
 
80
87
  ```ruby
81
88
  session.merge!(my_var: value)
89
+
90
+ # Accesssing session variable
91
+ session[:my_var]
82
92
  ```
83
93
 
84
94
  #### Slot elicitations
@@ -90,6 +100,20 @@ slot and the respecitve views are used.
90
100
  response.elicit_slot!(:SlotName)
91
101
  ```
92
102
 
103
+ #### Delete response
104
+
105
+ `#handle` is expected to return an instance of `Alexa::Response` or its subclasses.
106
+ In normal cases, the `response` object is returned.
107
+ In cases where the slots elicitation is delegated to alexa, an instance of
108
+ `Alexa::Responses::Delegate` has to be returned.
109
+
110
+ ```ruby
111
+ def handle
112
+ ...
113
+ return Alexa::Responses::Delegate.new
114
+ end
115
+ ```
116
+
93
117
  ### Views
94
118
 
95
119
  The content for speech and display cards is not set in the intent handler
@@ -146,6 +170,31 @@ view file for the response as follows:
146
170
 
147
171
  ```
148
172
 
173
+ ## Handling Amazon's Built-in Intents / Other request types
174
+
175
+ Requests for Amazon's built-in intents and other request types
176
+ are also handled with intent handlers.
177
+ Below is the mapping for every request type and respective
178
+ intent handler classes.
179
+ These intent handlers are not included in the gem as they are a
180
+ but specific to the application.
181
+ Refer to the table below and implement the intent handlers.
182
+
183
+ Built-in Intents:
184
+
185
+ | Intent name | Handler class |
186
+ | ------------- | ------------- |
187
+ | AMAZON.CancelIntent | `Alexa::IntentHandlers::GoodBye` |
188
+ | AMAZON.StopIntent | `Alexa::IntentHandlers::GoodBye` |
189
+ | AMAZON.HelpIntent | `Alexa::IntentHandlers::Help` |
190
+
191
+ Other request types:
192
+
193
+ | Request type | Handler class |
194
+ | ------------- | ------------- |
195
+ | Launch request | `Alexa::IntentHandlers::LaunchApp` |
196
+ | Session end request | `Alexa::IntentHandlers::SessionEnd` |
197
+
149
198
  ## Installation
150
199
  Add this line to your application's Gemfile:
151
200
 
data/lib/alexa/request.rb CHANGED
@@ -82,16 +82,7 @@ module Alexa
82
82
  end
83
83
 
84
84
  def locale
85
- request_locale = params["request"]["locale"]
86
- case params["request"]["locale"]
87
- when "en-US", "en-GB", "en-IN", 'en-CA'
88
- # this is to match pjpp locale codes
89
- params["request"]["locale"].gsub("-", "_").downcase
90
- when "de-DE"
91
- "de"
92
- else
93
- "en_gb"
94
- end
85
+ params["request"]["locale"]
95
86
  end
96
87
  end
97
88
  end
data/lib/alexa/session.rb CHANGED
@@ -1,3 +1,17 @@
1
+ # Provides easy access to session data from the request.
2
+ #
3
+ # Extends from +HashWithIndifferentAccess+ and initailised with the
4
+ # session hash from the request
5
+ #
6
+ # <b>Session variables</b>:
7
+ # Session variables can be accessed the same way as accessing hash with keys.
8
+ # ```ruby
9
+ # session[:my_var] # Reads the variable
10
+ # session.merge!(my_var: "Some value") # Write a session variable
11
+ # ```
12
+ #
13
+ # Apart from session handling, class also provides an API to other session
14
+ # related information.
1
15
  module Alexa
2
16
  class Session < HashWithIndifferentAccess
3
17
  def initialize(session={})
@@ -5,14 +19,27 @@ module Alexa
5
19
  super(session["attributes"])
6
20
  end
7
21
 
22
+ # Get the elicitation count of a slot.
23
+ #
24
+ # Elicitation counts are maintained in the session so that they
25
+ # can be referred to within the intenthandlers or the views
26
+ #
27
+ # Options:
28
+ # - slot_name: Slot name to return the elicitation count for
8
29
  def elicitation_count_for(slot_name)
9
30
  self["elicitations"] ||= {}
10
31
  if self["elicitations"][slot_name].present?
11
- return self["elicitations"][slot_name]["count"]
12
- end
32
+ return self["elicitations"][slot_name]["count"] end
13
33
  return 0
14
34
  end
15
35
 
36
+ # Increase the elicitation count of a slot.
37
+ #
38
+ # Elicitation counts are maintained in the session so that they
39
+ # can be referred to within the intenthandlers or the views
40
+ #
41
+ # Options:
42
+ # - slot_name: Slot name to increment the elicitation count for
16
43
  def increment_elicitation_count!(slot_name)
17
44
  count = elicitation_count_for(slot_name)
18
45
  self["elicitations"].merge!(
@@ -20,14 +47,17 @@ module Alexa
20
47
  )
21
48
  end
22
49
 
50
+ # Whether its a new session.
23
51
  def new?
24
52
  @variables["new"] == true
25
53
  end
26
54
 
55
+ # Get application's ID from session
27
56
  def application_id
28
57
  @variables["application"]["applicationId"]
29
58
  end
30
59
 
60
+ # Get Amazon user's ID from session
31
61
  def user_id
32
62
  @variables["user"]["userId"]
33
63
  end
data/lib/alexa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Alexa
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alexa-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sri Vishnu Totakura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-27 00:00:00.000000000 Z
11
+ date: 2018-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '5.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.0'
26
+ version: '5.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pg
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project:
111
- rubygems_version: 2.5.2.1
111
+ rubygems_version: 2.7.6
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Serve Alexa skills with your rails app as backend