ralyxa 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3802804f734d1955dd3b5e09e5b466304d5296bd
4
- data.tar.gz: a85ebe34a857be6e437b7005569e9efa3f8d166a
3
+ metadata.gz: 6aa4014123fdf96c66aafea914bbf45fc005234d
4
+ data.tar.gz: 9f61bfe7853045176e0b1fe46d2cb525fd5d1077
5
5
  SHA512:
6
- metadata.gz: 82c8bc2e1f457be53163bd95e2cae1c4b86f3a1c216bdffff70519f2f4f83bcfb2098b9c9764f005779549538d9fbbe14158d79fabe87536bbd27757af0d11d0
7
- data.tar.gz: 0f5289e068b2ba14ad9e13ccf0159a6eea25382d4f455b177b08af203716a1af521b02f21d6be23744878b8729303b69cff264e1b3b1bd9321e9984c00615d4f
6
+ metadata.gz: 217f26ef4be39c6e7b80d1666e6d7a9fb5361aeb41e8419e8446a92819ae5b2bd5fb1a3a3ed6f62b17a13171a24d607a810f4847464481480e8680c3528ae1ac
7
+ data.tar.gz: 5e40b794bd024fb2c8b8306b23bf09a423930fd72f825810bf7616443d920ef5f2120aa2a966dc84376fe85df9bb9045fa0c05286ed7dec873dfb76d2ee7575c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ralyxa (1.2.0)
4
+ ralyxa (1.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -91,6 +91,16 @@ intent "ReadFromSession" do
91
91
  end
92
92
  ```
93
93
 
94
+ ##### Reading the session user
95
+
96
+ You can read the session user's `userId` and `accessToken`, and check that the `accessToken` exists:
97
+
98
+ ```ruby
99
+ request.user_id #=> returns the `userId` value from the request session
100
+ request.user_access_token # => returns the `accessToken` value from the request session
101
+ request.user_access_token_exists? # => true if the user has an access token, false if not
102
+ ```
103
+
94
104
  > Go check out the `Alexa::Request` object to see what else you can do with the `request`.
95
105
 
96
106
  ##### Ending sessions
@@ -143,6 +153,18 @@ end
143
153
 
144
154
  > Card images must be under 2MB and available at an SSL-enabled (HTTPS) endpoint.
145
155
 
156
+ ##### Account Linking
157
+
158
+ You can ask Alexa to send a [`LinkAccount`](https://developer.amazon.com/blogs/post/Tx3CX1ETRZZ2NPC/Alexa-Account-Linking-5-Steps-to-Seamlessly-Link-Your-Alexa-Skill-with-Login-wit) card for the user to authenticate via OAuth:
159
+
160
+ ```ruby
161
+ intent "SendAccountLinkingCard" do
162
+ tell("Please authorize via the Alexa app.", card: link_account_card)
163
+ end
164
+ ```
165
+
166
+ After completing authentication, the user's access token is available via `request.user_access_token`. You can check for its existence with `request.user_access_token_exists?`.
167
+
146
168
  ## Development
147
169
 
148
170
  After checking out the repo, run `bundle install` to install dependencies. Then, run `rspec` to run the tests. You can also run `irb` for an interactive prompt that will allow you to experiment.
data/lib/ralyxa/card.rb CHANGED
@@ -2,57 +2,69 @@ require_relative './errors'
2
2
 
3
3
  module Ralyxa
4
4
  class Card
5
- SIMPLE_CARD_TYPE = "Simple"
6
- STANDARD_CARD_TYPE = "Standard"
5
+ LINK_ACCOUNT_CARD_TYPE = "LinkAccount"
6
+ SIMPLE_CARD_TYPE = "Simple"
7
+ STANDARD_CARD_TYPE = "Standard"
7
8
 
8
- def initialize(title, body, image_url)
9
- @title = title
10
- @body = body
11
- @image_url = image_url
9
+ def initialize(options)
10
+ @options = options
12
11
  end
13
12
 
14
13
  def self.as_hash(title, body, image_url = nil)
15
- new(title, body, image_url).to_h
14
+ new(title: title, body: body, image_url: image_url).to_h
15
+ end
16
+
17
+ def self.link_account
18
+ new(link_account: true).to_h
16
19
  end
17
20
 
18
21
  def to_h
19
22
  Hash.new.tap do |card|
20
23
  set_type(card)
21
- card[:title] = @title
22
- set_body(card)
23
- set_image(card) if @image_url
24
+ set_title(card) if @options[:title]
25
+ set_body(card) if @options[:body]
26
+ set_image(card) if @options[:image_url]
24
27
  end
25
28
  end
26
29
 
27
30
  private
28
31
 
29
32
  def set_type(card)
30
- card[:type] = SIMPLE_CARD_TYPE if simple?
31
- card[:type] = STANDARD_CARD_TYPE if standard?
33
+ return card[:type] = LINK_ACCOUNT_CARD_TYPE if link_account?
34
+ card[:type] = SIMPLE_CARD_TYPE if simple?
35
+ card[:type] = STANDARD_CARD_TYPE if standard?
36
+ end
37
+
38
+ def set_title(card)
39
+ card[:title] = @options[:title]
32
40
  end
33
41
 
34
42
  def set_body(card)
35
- card[:content] = @body if simple?
36
- card[:text] = @body if standard?
43
+ card[:content] = @options[:body] if simple?
44
+ card[:text] = @options[:body] if standard?
37
45
  end
38
46
 
39
47
  def set_image(card)
40
- raise UnsecureUrlError.new("Card images must be available at an SSL-enabled (HTTPS) endpoint. Your current image url is: #{ @image_url }") unless secure?(@image_url)
48
+ raise UnsecureUrlError.new("Card images must be available at an SSL-enabled (HTTPS) endpoint. Your current image url is: #{ @options[:image_url] }") unless secure?
41
49
  card[:image] = Hash.new
42
- card[:image][:smallImageUrl] = @image_url
43
- card[:image][:largeImageUrl] = @image_url
50
+ card[:image][:smallImageUrl] = @options[:image_url]
51
+ card[:image][:largeImageUrl] = @options[:image_url]
52
+ end
53
+
54
+ def link_account?
55
+ !!@options[:link_account]
44
56
  end
45
57
 
46
58
  def simple?
47
- !@image_url
59
+ !@options[:image_url]
48
60
  end
49
61
 
50
62
  def standard?
51
- !!@image_url
63
+ !!@options[:image_url]
52
64
  end
53
65
 
54
- def secure?(url)
55
- URI.parse(url).scheme == "https"
66
+ def secure?
67
+ URI.parse(@options[:image_url]).scheme == "https"
56
68
  end
57
69
  end
58
70
  end
@@ -24,6 +24,10 @@ module Ralyxa
24
24
  card_class.as_hash(title, body, image_url)
25
25
  end
26
26
 
27
+ def link_account_card(card_class = Ralyxa::Card)
28
+ card_class.link_account
29
+ end
30
+
27
31
  alias ask respond
28
32
 
29
33
  attr_reader :request
@@ -1,11 +1,19 @@
1
1
  require 'json'
2
+ require 'forwardable'
3
+ require_relative './user'
2
4
 
3
5
  module Ralyxa
4
6
  class Request
7
+ extend Forwardable
5
8
  INTENT_REQUEST_TYPE = "IntentRequest".freeze
6
9
 
7
- def initialize(original_request)
10
+ def_delegator :@user, :id, :user_id
11
+ def_delegator :@user, :access_token, :user_access_token
12
+ def_delegator :@user, :access_token_exists?, :user_access_token_exists?
13
+
14
+ def initialize(original_request, user_class = Ralyxa::User)
8
15
  @request = JSON.parse(original_request.body.read)
16
+ @user = user_class.build(@request)
9
17
  end
10
18
 
11
19
  def intent_name
@@ -0,0 +1,21 @@
1
+ module Ralyxa
2
+ class User
3
+ attr_reader :id, :access_token
4
+
5
+ def initialize(id:, access_token: nil)
6
+ @id = id
7
+ @access_token = access_token
8
+ end
9
+
10
+ def self.build(request)
11
+ new(
12
+ id: request.dig(:session, :user, :userId),
13
+ access_token: request.dig(:session, :user, :accessToken)
14
+ )
15
+ end
16
+
17
+ def access_token_exists?
18
+ !!@access_token
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Ralyxa
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ralyxa
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Morgan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-23 00:00:00.000000000 Z
11
+ date: 2017-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,7 @@ files:
80
80
  - lib/ralyxa/response.rb
81
81
  - lib/ralyxa/response_builder.rb
82
82
  - lib/ralyxa/skill.rb
83
+ - lib/ralyxa/user.rb
83
84
  - lib/ralyxa/version.rb
84
85
  - pkg/ralyxa-1.0.0.gem
85
86
  - ralyxa.gemspec