diff --git a/.gitignore b/.gitignore index 9d3eaab..43eeaf2 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ mkmf.log !.elasticbeanstalk/*.global.yml .DS_ sync-upstream.sh +vendor* \ No newline at end of file diff --git a/fixtures/sample-BodyTemplate1.json b/fixtures/sample-BodyTemplate1.json new file mode 100644 index 0000000..15cf3ea --- /dev/null +++ b/fixtures/sample-BodyTemplate1.json @@ -0,0 +1,30 @@ +{ + "type": "Display.RenderTemplate", + "template": { + "type": "BodyTemplate1", + "title": "BodyTemplate1 Display Title", + "token": "TOKEN", + "backgroundImage": { + "sources": [ + { + "url": "https://example.com/background.png" + } + ] + }, + "textContent": { + "primaryText": { + "text": "BodyTemplate1 Primary Text", + "type": "PlainText" + }, + "secondaryText": { + "text": "BodyTemplate1 Secondary Text", + "type": "PlainText" + }, + "tertiaryText": { + "text": "BodyTemplate1 Tertiary Text", + "type": "PlainText" + } + }, + "backButton": "HIDDEN" + } +} diff --git a/fixtures/sample-BodyTemplate2.json b/fixtures/sample-BodyTemplate2.json new file mode 100644 index 0000000..145b1f1 --- /dev/null +++ b/fixtures/sample-BodyTemplate2.json @@ -0,0 +1,42 @@ +{ + "type": "Display.RenderTemplate", + "template": { + "type": "BodyTemplate2", + "image": { + "contentDescription": "image description", + "sources": [ + { + "url": "https://example.com/image.png", + "size": "1024", + "widthPixels": "720", + "heightPixels": "480" + } + ] + }, + "title": "BodyTemplate2 Display Title", + "token": "TOKEN", + "backgroundImage": { + "contentDescription": "background description", + "sources": [ + { + "url": "https://example.com/background.png" + } + ] + }, + "textContent": { + "primaryText": { + "text": "BodyTemplate2 Primary Text", + "type": "PlainText" + }, + "secondaryText": { + "text": "BodyTemplate2 Secondary Text", + "type": "PlainText" + }, + "tertiaryText": { + "text": "BodyTemplate2 Tertiary Text", + "type": "PlainText" + } + }, + "backButton": "HIDDEN" + } +} \ No newline at end of file diff --git a/fixtures/sample-BodyTemplate3.json b/fixtures/sample-BodyTemplate3.json new file mode 100644 index 0000000..ec3d1b2 --- /dev/null +++ b/fixtures/sample-BodyTemplate3.json @@ -0,0 +1,41 @@ +{ + "type": "Display.RenderTemplate", + "template": { + "type": "BodyTemplate3", + "image": { + "sources": [ + { + "url": "https://example.com/image.png" + } + ] + }, + "title": "BodyTemplate3 Display Title", + "token": "TOKEN", + "backgroundImage": { + "contentDescription": "image description", + "sources": [ + { + "url": "https://example.com/background.png", + "size": "1024", + "widthPixels": "720", + "heightPixels": "480" + } + ] + }, + "textContent": { + "primaryText": { + "text": "BodyTemplate3 Primary Text", + "type": "PlainText" + }, + "secondaryText": { + "text": "BodyTemplate3 Secondary Text", + "type": "PlainText" + }, + "tertiaryText": { + "text": "BodyTemplate3 Tertiary Text", + "type": "PlainText" + } + }, + "backButton": "HIDDEN" + } +} \ No newline at end of file diff --git a/fixtures/sample-BodyTemplate6.json b/fixtures/sample-BodyTemplate6.json new file mode 100644 index 0000000..300943e --- /dev/null +++ b/fixtures/sample-BodyTemplate6.json @@ -0,0 +1,30 @@ +{ + "type": "Display.RenderTemplate", + "template": { + "type": "BodyTemplate6", + "title": "BodyTemplate6 Display Title", + "token": "TOKEN", + "backgroundImage": { + "sources": [ + { + "url": "https://example.com/background.png" + } + ] + }, + "textContent": { + "primaryText": { + "text": "BodyTemplate6 Primary Text", + "type": "PlainText" + }, + "secondaryText": { + "text": "BodyTemplate6 Secondary Text", + "type": "PlainText" + }, + "tertiaryText": { + "text": "BodyTemplate6 Tertiary Text", + "type": "PlainText" + } + }, + "backButton": "HIDDEN" + } +} \ No newline at end of file diff --git a/fixtures/sample-BodyTemplate7.json b/fixtures/sample-BodyTemplate7.json new file mode 100644 index 0000000..7c7b3cc --- /dev/null +++ b/fixtures/sample-BodyTemplate7.json @@ -0,0 +1,23 @@ +{ + "type": "Display.RenderTemplate", + "template": { + "type": "BodyTemplate7", + "image": { + "sources": [ + { + "url": "https://example.com/image.png" + } + ] + }, + "title": "BodyTemplate7 Display Title", + "token": "TOKEN", + "backgroundImage": { + "sources": [ + { + "url": "https://example.com/background.png" + } + ] + }, + "backButton": "HIDDEN" + } +} \ No newline at end of file diff --git a/fixtures/sample-ListTemplate1.json b/fixtures/sample-ListTemplate1.json new file mode 100644 index 0000000..de430aa --- /dev/null +++ b/fixtures/sample-ListTemplate1.json @@ -0,0 +1,158 @@ +{ + "type": "Display.RenderTemplate", + "template": { + "type": "ListTemplate1", + "title": "ListTemplate1 Display Title", + "token": "TOKEN", + "listItems": [ + { + "token": "TOKEN0", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item1.png", + "widthPixels": 88, + "heightPixels": 88, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary Text 0", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 0", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 0", + "type": "PlainText" + } + } + }, + { + "token": "TOKEN1", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item1.png", + "widthPixels": 88, + "heightPixels": 88, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary Text 1", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 1", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 1", + "type": "PlainText" + } + } + }, + { + "token": "TOKEN2", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item2.png", + "widthPixels": 88, + "heightPixels": 88, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary Text 2", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 2", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 2", + "type": "PlainText" + } + } + }, + { + "token": "TOKEN3", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item3.png", + "widthPixels": 88, + "heightPixels": 88, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary Text 3", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 3", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 3", + "type": "PlainText" + } + } + }, + { + "token": "TOKEN4", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item4.png", + "widthPixels": 88, + "heightPixels": 88, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary Text 4", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 4", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 4", + "type": "PlainText" + } + } + } + ], + "backgroundImage": { + "sources": [ + { + "url": "https://example.com/background.png" + } + ] + }, + "backButton": "HIDDEN" + } +} \ No newline at end of file diff --git a/fixtures/sample-ListTemplate2.json b/fixtures/sample-ListTemplate2.json new file mode 100644 index 0000000..c8d1b1d --- /dev/null +++ b/fixtures/sample-ListTemplate2.json @@ -0,0 +1,158 @@ +{ + "type": "Display.RenderTemplate", + "template": { + "type": "ListTemplate2", + "title": "ListTemplate2 Display Title", + "token": "TOKEN", + "listItems": [ + { + "token": "TOKEN0", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item0.png", + "widthPixels": 192, + "heightPixels": 280, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary 0", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 0", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 0", + "type": "PlainText" + } + } + }, + { + "token": "TOKEN1", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item1.png", + "widthPixels": 280, + "heightPixels": 280, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary 1", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 1", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 1", + "type": "PlainText" + } + } + }, + { + "token": "TOKEN2", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item2.png", + "widthPixels": 372, + "heightPixels": 280, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary 2", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 2", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 2", + "type": "PlainText" + } + } + }, + { + "token": "TOKEN3", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item3.png", + "widthPixels": 298, + "heightPixels": 280, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary 3", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 3", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 3", + "type": "PlainText" + } + } + }, + { + "token": "TOKEN4", + "image": { + "contentDescription": "Item Description", + "sources": [ + { + "url": "https://example.com/item4.png", + "widthPixels": 192, + "heightPixels": 280, + "size": "X_SMALL" + } + ] + }, + "textContent": { + "primaryText": { + "text": "Primary 4", + "type": "PlainText" + }, + "secondaryText": { + "text": "Secondary Text 4", + "type": "PlainText" + }, + "tertiaryText": { + "text": "Tertiary Text 4", + "type": "PlainText" + } + } + } + ], + "backgroundImage": { + "sources": [ + { + "url": "https://example.com/background.png" + } + ] + }, + "backButton": "HIDDEN" + } +} \ No newline at end of file diff --git a/fixtures/sample-device-request.json b/fixtures/sample-device-request.json new file mode 100644 index 0000000..8b3841d --- /dev/null +++ b/fixtures/sample-device-request.json @@ -0,0 +1,46 @@ +{ + "version": "1.0", + "session": { + "new": true, + "sessionId": "amzn1.echo-api.session.", + "application": { + "applicationId": "amzn1.ask.skill." + }, + "user": { + "userId": "amzn1.ask.account." + } + }, + "context": { + "AudioPlayer": { + "playerActivity": "FINISHED" + }, + "Display": {}, + "System": { + "application": { + "applicationId": "amzn1.ask.skill." + }, + "user": { + "userId": "amzn1.ask.account." + }, + "device": { + "deviceId": "amzn1.ask.device.", + "supportedInterfaces": { + "AudioPlayer": {}, + "Display": { + "templateVersion": "1.0", + "markupVersion": "1.0" + }, + "VideoApp": {} + } + }, + "apiEndpoint": "https://api.amazonalexa.com", + "apiAccessToken": "aaaa" + } + }, + "request": { + "type": "LaunchRequest", + "requestId": "amzn1.echo-api.request.", + "timestamp": "2018-01-20T10:26:58Z", + "locale": "en-US" + } +} \ No newline at end of file diff --git a/fixtures/sample-element-select-request.json b/fixtures/sample-element-select-request.json new file mode 100644 index 0000000..e884bdf --- /dev/null +++ b/fixtures/sample-element-select-request.json @@ -0,0 +1,52 @@ +{ + "version": "1.0", + "session": { + "new": false, + "sessionId": "amzn1.echo-api.session.", + "application": { + "applicationId": "amzn1.ask.skill." + }, + "attributes": { + "type": "NONE" + }, + "user": { + "userId": "amzn1.ask.account." + } + }, + "context": { + "AudioPlayer": { + "playerActivity": "IDLE" + }, + "Display": { + "token": "display_token" + }, + "System": { + "application": { + "applicationId": "amzn1.ask.skill." + }, + "user": { + "userId": "amzn1.ask.account." + }, + "device": { + "deviceId": "amzn1.ask.device.", + "supportedInterfaces": { + "AudioPlayer": {}, + "Display": { + "templateVersion": "1.0", + "markupVersion": "1.0" + }, + "VideoApp": {} + } + }, + "apiEndpoint": "https://api.fe.amazonalexa.com", + "apiAccessToken": "xxxxxx" + } + }, + "request": { + "type": "Display.ElementSelected", + "requestId": "amzn1.echo-api.request.", + "timestamp": "2018-02-14T13:11:40Z", + "locale": "ja-JP", + "token": "request_token" + } +} \ No newline at end of file diff --git a/lib/alexa_rubykit.rb b/lib/alexa_rubykit.rb index b78affb..da73384 100644 --- a/lib/alexa_rubykit.rb +++ b/lib/alexa_rubykit.rb @@ -3,7 +3,19 @@ require 'alexa_rubykit/response' require 'alexa_rubykit/intent_request' require 'alexa_rubykit/launch_request' +require 'alexa_rubykit/element_selected_request' require 'alexa_rubykit/session_ended_request' +require 'alexa_rubykit/device' +require 'alexa_rubykit/response/display_templates/display_body_template1' +require 'alexa_rubykit/response/display_templates/display_body_template2' +require 'alexa_rubykit/response/display_templates/display_body_template3' +require 'alexa_rubykit/response/display_templates/display_body_template6' +require 'alexa_rubykit/response/display_templates/display_body_template7' +require 'alexa_rubykit/response/display_templates/display_list_template1' +require 'alexa_rubykit/response/display_templates/display_list_template2' +require 'alexa_rubykit/response/display_templates/display_text_context' +require 'alexa_rubykit/response/display_templates/display_image' +require 'alexa_rubykit/response/display_templates/display_video_template' module AlexaRubykit # Prints a JSON object. diff --git a/lib/alexa_rubykit/device.rb b/lib/alexa_rubykit/device.rb new file mode 100644 index 0000000..8961ec0 --- /dev/null +++ b/lib/alexa_rubykit/device.rb @@ -0,0 +1,28 @@ +module AlexaRubykit + # Handles the device object in request. + class Device + def initialize(device) + if device.nil? || device['context'].nil? || device['context']['System'].nil? || device['context']['System']['device'].nil? + @device = nil + else + @device = device['context']['System']['device'] + end + end + + def device_id + @device['deviceId'] if @device + end + + def display_support? + !@device.nil? && !@device['supportedInterfaces'].nil? && !@device['supportedInterfaces']['Display'].nil? + end + + def audio_player_support? + !@device.nil? && !@device['supportedInterfaces'].nil? && !@device['supportedInterfaces']['AudioPlayer'].nil? + end + + def video_app_support? + !@device.nil? && !@device['supportedInterfaces'].nil? && !@device['supportedInterfaces']['VideoApp'].nil? + end + end +end \ No newline at end of file diff --git a/lib/alexa_rubykit/element_selected_request.rb b/lib/alexa_rubykit/element_selected_request.rb new file mode 100644 index 0000000..5bc7aba --- /dev/null +++ b/lib/alexa_rubykit/element_selected_request.rb @@ -0,0 +1,17 @@ +module AlexaRubykit + class ElementSelectedRequest < Request + + attr_accessor :display_token, :request_token + + def initialize(json_request) + super + @type = 'ELEMENT_SELECTED' + @display_token = json_request['context']['Display']['token'] + @request_token = json_request['request']['token'] + end + + def to_s + "LaunchRequest requestID: #{request_id}" + end + end +end \ No newline at end of file diff --git a/lib/alexa_rubykit/request.rb b/lib/alexa_rubykit/request.rb index 4a18dae..d0238cf 100644 --- a/lib/alexa_rubykit/request.rb +++ b/lib/alexa_rubykit/request.rb @@ -6,7 +6,7 @@ module AlexaRubykit class Request require 'json' require 'alexa_rubykit/session' - attr_accessor :version, :type, :session, :json # global + attr_accessor :version, :type, :session, :json, :device # global attr_accessor :request_id, :locale # on request def initialize(json_request) @@ -14,6 +14,8 @@ def initialize(json_request) raise ArgumentError, 'Request ID should exist on all Requests' if @request_id.nil? @version = json_request['version'] @locale = json_request['request']['locale'] + @device = AlexaRubykit::Device.new(json_request) + @json = json_request # TODO: We probably need better session handling. @@ -25,6 +27,7 @@ def initialize(json_request) def self.build_request(json_request) raise ArgumentError, 'Invalid Alexa Request.' unless AlexaRubykit.valid_alexa?(json_request) @request = nil + case json_request['request']['type'] when /Launch/ @request = LaunchRequest.new(json_request) @@ -32,6 +35,8 @@ def self.build_request(json_request) @request = IntentRequest.new(json_request) when /SessionEnded/ @request = SessionEndedRequest.new(json_request) + when 'Display.ElementSelected' + @request = ElementSelectedRequest.new(json_request) else raise ArgumentError, 'Invalid Request Type.' end diff --git a/lib/alexa_rubykit/response.rb b/lib/alexa_rubykit/response.rb index d41e67d..1e9b7a1 100644 --- a/lib/alexa_rubykit/response.rb +++ b/lib/alexa_rubykit/response.rb @@ -47,19 +47,19 @@ def add_audio_url(url, token='', offset=0) end def delegate_dialog_response - @directives = [Dialog.delegate_directive(intents)] + @directives.push(Dialog.delegate_directive(intents)) end def elicit_dialog_response(slot) - @directives = [Dialog.elicit_slot_directive(slot, intents)] + @directives.push(Dialog.elicit_slot_directive(slot, intents)) end def confirm_dialog_slot(slot) - @directives = [Dialog.confirm_slot_directive(slot, intents)] + @directives.push(Dialog.confirm_slot_directive(slot, intents)) end def confirm_dialog_intent - @directives = [Dialog.confirm_intent_directive(intents)] + @directives.push(Dialog.confirm_intent_directive(intents)) end def modify_slot(name, value, confirmation_status) @@ -100,6 +100,20 @@ def add_hash_card(card) @card end + def add_display_template(template) + @directives.push(template.to_hash) + end + + def add_hint_directive(text) + @directives << { + "type" => "Hint", + "hint" => { + "type" => "PlainText", + "text" => text + } + } + end + # Adds a speech to the object, also returns a outputspeech object. def say_response(speech, end_session = true, ssml = false) output_speech = add_speech(speech,ssml) @@ -130,7 +144,7 @@ def build_response_object(session_end = true) @response[:directives] = @directives unless @directives.empty? @response[:card] = @card unless @card.nil? @response[:reprompt] = @reprompt unless session_end && @reprompt.nil? - @response[:shouldEndSession] = session_end + @response[:shouldEndSession] = session_end unless session_end.nil? @response end diff --git a/lib/alexa_rubykit/response/display_templates/display_body_template1.rb b/lib/alexa_rubykit/response/display_templates/display_body_template1.rb new file mode 100644 index 0000000..2c2b1c3 --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_body_template1.rb @@ -0,0 +1,31 @@ +module AlexaRubykit + require 'alexa_rubykit/response/display_templates/display_template' + class DisplayBodyTemplate1 < DisplayTemplate + + attr_accessor :title, :back_button, :background_image + attr_accessor :token, :text_context + + TEMPLATE_NAME = "BodyTemplate1".freeze + def initialize() + super + @token = TEMPLATE_NAME + "_token" + @text_context = DisplayTextContext.new() + end + + def to_hash + template = Hash.new + template[:type] = TEMPLATE_NAME + template[:token] = @token + template[:backButton] = back_button_status + template[:title] = @title + template[:backgroundImage] = @background_image.to_hash unless @background_image.nil? + template[:textContent] = @text_context.to_hash + @directive[:template] = template + @directive + end + + def to_json + self.to_hash.to_json + end + end +end diff --git a/lib/alexa_rubykit/response/display_templates/display_body_template2.rb b/lib/alexa_rubykit/response/display_templates/display_body_template2.rb new file mode 100644 index 0000000..a10233b --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_body_template2.rb @@ -0,0 +1,34 @@ +module AlexaRubykit + + require 'alexa_rubykit/response/display_templates/display_template' + class DisplayBodyTemplate2 < DisplayTemplate + + attr_accessor :title, :back_button, :background_image + attr_accessor :token, :text_context, :image + + TEMPLATE_NAME = "BodyTemplate2".freeze + def initialize() + super + @image = nil + @token = TEMPLATE_NAME + "_token" + @text_context = DisplayTextContext.new() + end + + def to_hash + template = Hash.new + template[:type] = TEMPLATE_NAME + template[:token] = @token + template[:backButton] = back_button_status + template[:title] = @title + template[:image] = @image.to_hash unless @image.nil? + template[:backgroundImage] = @background_image.to_hash unless @background_image.nil? + template[:textContent] = @text_context.to_hash + @directive[:template] = template + @directive + end + + def to_json + self.to_hash.to_json + end + end +end diff --git a/lib/alexa_rubykit/response/display_templates/display_body_template3.rb b/lib/alexa_rubykit/response/display_templates/display_body_template3.rb new file mode 100644 index 0000000..7e4ca7d --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_body_template3.rb @@ -0,0 +1,34 @@ +module AlexaRubykit + + require 'alexa_rubykit/response/display_templates/display_template' + class DisplayBodyTemplate3 < DisplayTemplate + + attr_accessor :title, :back_button, :background_image + attr_accessor :token, :text_context, :image + + TEMPLATE_NAME = "BodyTemplate3".freeze + def initialize() + super + @image = nil + @token = TEMPLATE_NAME + "_token" + @text_context = DisplayTextContext.new() + end + + def to_hash + template = Hash.new + template[:type] = TEMPLATE_NAME + template[:token] = @token + template[:backButton] = back_button_status + template[:title] = @title + template[:image] = @image.to_hash unless @image.nil? + template[:backgroundImage] = @background_image.to_hash unless @background_image.nil? + template[:textContent] = @text_context.to_hash + @directive[:template] = template + @directive + end + + def to_json + self.to_hash.to_json + end + end +end diff --git a/lib/alexa_rubykit/response/display_templates/display_body_template6.rb b/lib/alexa_rubykit/response/display_templates/display_body_template6.rb new file mode 100644 index 0000000..6c659f6 --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_body_template6.rb @@ -0,0 +1,32 @@ +module AlexaRubykit + + require 'alexa_rubykit/response/display_templates/display_template' + class DisplayBodyTemplate6 < DisplayTemplate + + attr_accessor :title, :back_button, :background_image + attr_accessor :token, :text_context + + TEMPLATE_NAME = "BodyTemplate6".freeze + def initialize() + super + @token = TEMPLATE_NAME + "_token" + @text_context = DisplayTextContext.new() + end + + def to_hash + template = Hash.new + template[:type] = TEMPLATE_NAME + template[:token] = @token + template[:backButton] = back_button_status + template[:title] = @title + template[:backgroundImage] = @background_image.to_hash unless @background_image.nil? + template[:textContent] = @text_context.to_hash + @directive[:template] = template + @directive + end + + def to_json + self.to_hash.to_json + end + end +end diff --git a/lib/alexa_rubykit/response/display_templates/display_body_template7.rb b/lib/alexa_rubykit/response/display_templates/display_body_template7.rb new file mode 100644 index 0000000..a48427e --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_body_template7.rb @@ -0,0 +1,32 @@ +module AlexaRubykit + + require 'alexa_rubykit/response/display_templates/display_template' + class DisplayBodyTemplate7 < DisplayTemplate + + attr_accessor :title, :back_button, :background_image + attr_accessor :token, :image + + TEMPLATE_NAME = "BodyTemplate7".freeze + def initialize() + super + @image = nil + @token = TEMPLATE_NAME + "_token" + end + + def to_hash + template = Hash.new + template[:type] = TEMPLATE_NAME + template[:token] = @token + template[:backButton] = back_button_status + template[:title] = @title + template[:image] = @image.to_hash unless @image.nil? + template[:backgroundImage] = @background_image.to_hash unless @background_image.nil? + @directive[:template] = template + @directive + end + + def to_json + self.to_hash.to_json + end + end +end diff --git a/lib/alexa_rubykit/response/display_templates/display_image.rb b/lib/alexa_rubykit/response/display_templates/display_image.rb new file mode 100644 index 0000000..a5257b3 --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_image.rb @@ -0,0 +1,34 @@ +module AlexaRubykit + class DisplayImage + def initialize(url, description="", size=nil, width=nil, height=nil) + @display_image = Hash.new() + @display_image[:contentDescription] = description + @display_image[:sources] = [] + + self.add_source(url, description, size, width, height) + end + + def add_source(url, description="", size=nil, width=nil, height=nil) + source = Hash.new() + source[:url] = url + + if description + source[:description] = description + end + if size + source[:size] = size + end + if width + source[:widthPixels] = width + end + if height + source[:heightPixels] = height + end + @display_image[:sources].push(source) + end + + def to_hash + @display_image + end + end +end \ No newline at end of file diff --git a/lib/alexa_rubykit/response/display_templates/display_list_template1.rb b/lib/alexa_rubykit/response/display_templates/display_list_template1.rb new file mode 100644 index 0000000..98e2e0a --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_list_template1.rb @@ -0,0 +1,46 @@ +module AlexaRubykit + + require 'alexa_rubykit/response/display_templates/display_template' + class DisplayListTemplate1 < DisplayTemplate + + attr_accessor :title, :back_button, :background_image + attr_accessor :token, :list_item + + TEMPLATE_NAME = "ListTemplate1".freeze + def initialize() + super + @token = TEMPLATE_NAME + "_token" + @list_item = [] + end + + def add_list_item(text_content, token=nil, image=nil) + item = Hash.new() + item[:textContent] = text_content + if token + item[:token] = token + else + item[:token] = "LIST_ITEM_" + @list_item.length.to_s + end + if image + item[:image] = image.to_hash + end + @list_item.push(item) + end + + def to_hash + template = Hash.new + template[:type] = TEMPLATE_NAME + template[:token] = @token + template[:backButton] = back_button_status + template[:title] = @title + template[:backgroundImage] = @background_image.to_hash unless @background_image.nil? + template[:listItems] = @list_item + @directive[:template] = template + @directive + end + + def to_json + self.to_hash.to_json + end + end +end diff --git a/lib/alexa_rubykit/response/display_templates/display_list_template2.rb b/lib/alexa_rubykit/response/display_templates/display_list_template2.rb new file mode 100644 index 0000000..4fae4c8 --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_list_template2.rb @@ -0,0 +1,46 @@ +module AlexaRubykit + + require 'alexa_rubykit/response/display_templates/display_template' + class DisplayListTemplate2 < DisplayTemplate + + attr_accessor :title, :back_button, :background_image + attr_accessor :token, :list_item + + TEMPLATE_NAME = "ListTemplate2".freeze + def initialize() + super + @token = TEMPLATE_NAME + "_token" + @list_item = [] + end + + def add_list_item(text_content, token=nil, image=nil) + item = Hash.new() + item[:textContent] = text_content + if token + item[:token] = token + else + item[:token] = "LIST_ITEM_" + @list_item.length.to_s + end + if item + item[:image] = image.to_hash + end + @list_item.push(item) + end + + def to_hash + template = Hash.new + template[:type] = TEMPLATE_NAME + template[:token] = @token + template[:backButton] = back_button_status + template[:title] = @title + template[:backgroundImage] = @background_image.to_hash unless @background_image.nil? + template[:listItems] = @list_item + @directive[:template] = template + @directive + end + + def to_json + self.to_hash.to_json + end + end +end diff --git a/lib/alexa_rubykit/response/display_templates/display_template.rb b/lib/alexa_rubykit/response/display_templates/display_template.rb new file mode 100644 index 0000000..3495982 --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_template.rb @@ -0,0 +1,29 @@ +module AlexaRubykit + # Amazon Alexa Display Interface and Template + # https://developer.amazon.com/ja/docs/custom-skills/display-interface-reference.html + + class DisplayTemplate + + require 'alexa_rubykit/response/display_templates/display_image' + + attr_accessor :title, :back_button, :background_image + + def initialize() + @back_button = true + @title = "" + @directive = { + :type => "Display.RenderTemplate", + :template => {} + } + @background_image = nil + end + + def back_button_status + if @back_button + "VISIBLE" + else + "HIDDEN" + end + end + end +end diff --git a/lib/alexa_rubykit/response/display_templates/display_text_context.rb b/lib/alexa_rubykit/response/display_templates/display_text_context.rb new file mode 100644 index 0000000..98f4983 --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_text_context.rb @@ -0,0 +1,35 @@ +module AlexaRubykit + class DisplayTextContext + PLAIN_TEXT = "PlainText".freeze + RICH_TEXT = "RichText".freeze + + def initialize + @text_context = Hash.new() + end + + def primary_text(type, text) + @text_context[:primaryText] = { + :type => type, + :text => text + } + end + + def secondary_text(type, text) + @text_context[:secondaryText] = { + :type => type, + :text => text + } + end + + def tertiary_text(type, text) + @text_context[:tertiaryText] = { + :type => type, + :text => text + } + end + + def to_hash + @text_context + end + end +end \ No newline at end of file diff --git a/lib/alexa_rubykit/response/display_templates/display_video_template.rb b/lib/alexa_rubykit/response/display_templates/display_video_template.rb new file mode 100644 index 0000000..1a9740c --- /dev/null +++ b/lib/alexa_rubykit/response/display_templates/display_video_template.rb @@ -0,0 +1,31 @@ +module AlexaRubykit + + class DisplayVideoTemplate + + attr_accessor :video_src, :title, :subtitle + + TEMPLATE_NAME = "VideoApp.Launch".freeze + def initialize() + @title = nil + @video_src = nil + @subtitle = "" + end + + def to_hash + template = Hash.new + template[:type] = TEMPLATE_NAME + template[:videoItem] = { + :source => @video_src, + :metadata => { + :title => @title, + :subtitle => @subtitle + } + } + template + end + + def to_json + self.to_hash.to_json + end + end +end diff --git a/spec/body_template1_spec.rb b/spec/body_template1_spec.rb new file mode 100644 index 0000000..30c61fd --- /dev/null +++ b/spec/body_template1_spec.rb @@ -0,0 +1,47 @@ +require 'rspec' + +describe 'Request handling' do + it 'validate json format' do + sample_json = JSON.parse(File.read('fixtures/sample-BodyTemplate1.json')) + template = AlexaRubykit::DisplayBodyTemplate1.new + template.token = sample_json["template"]["token"] + template.back_button = (sample_json["template"]["backButton"] == "VISIBLE") + context = AlexaRubykit::DisplayTextContext.new + context.primary_text( + sample_json["template"]["textContent"]["primaryText"]["type"], + sample_json["template"]["textContent"]["primaryText"]["text"] + ) + context.secondary_text( + sample_json["template"]["textContent"]["secondaryText"]["type"], + sample_json["template"]["textContent"]["secondaryText"]["text"] + ) + context.tertiary_text( + sample_json["template"]["textContent"]["tertiaryText"]["type"], + sample_json["template"]["textContent"]["tertiaryText"]["text"] + ) + background_image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["backgroundImage"]["sources"][0]["url"] + ) + template.text_context = context + template.background_image = background_image + template_json = JSON.parse(template.to_json) + + expect(template_json["template"]["type"]).to eq('BodyTemplate1') + expect(template_json["template"]["textContent"]["primaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["primaryText"]["type"], + "text" => sample_json["template"]["textContent"]["primaryText"]["text"] + }) + expect(template_json["template"]["textContent"]["secondaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["secondaryText"]["type"], + "text" => sample_json["template"]["textContent"]["secondaryText"]["text"] + }) + expect(template_json["template"]["textContent"]["tertiaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["tertiaryText"]["type"], + "text" => sample_json["template"]["textContent"]["tertiaryText"]["text"] + }) + + expect(template_json["template"]["backgroundImage"]["sources"][0]).to include({ + "url" => sample_json["template"]["backgroundImage"]["sources"][0]["url"] + }) + end +end diff --git a/spec/body_template2_spec.rb b/spec/body_template2_spec.rb new file mode 100644 index 0000000..ed2982b --- /dev/null +++ b/spec/body_template2_spec.rb @@ -0,0 +1,65 @@ +require 'rspec' + +describe 'Request handling' do + it 'validate json format' do + sample_json = JSON.parse(File.read('fixtures/sample-BodyTemplate2.json')) + template = AlexaRubykit::DisplayBodyTemplate2.new + template.token = sample_json["template"]["token"] + template.back_button = (sample_json["template"]["backButton"] == "VISIBLE") + context = AlexaRubykit::DisplayTextContext.new + context.primary_text( + sample_json["template"]["textContent"]["primaryText"]["type"], + sample_json["template"]["textContent"]["primaryText"]["text"] + ) + context.secondary_text( + sample_json["template"]["textContent"]["secondaryText"]["type"], + sample_json["template"]["textContent"]["secondaryText"]["text"] + ) + context.tertiary_text( + sample_json["template"]["textContent"]["tertiaryText"]["type"], + sample_json["template"]["textContent"]["tertiaryText"]["text"] + ) + background_image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["backgroundImage"]["sources"][0]["url"], + sample_json["template"]["backgroundImage"]["contentDescription"] + ) + image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["image"]["sources"][0]["url"], + sample_json["template"]["image"]["contentDescription"], + sample_json["template"]["image"]["sources"][0]["size"], + sample_json["template"]["image"]["sources"][0]["widthPixels"], + sample_json["template"]["image"]["sources"][0]["heightPixels"] + ) + template.text_context = context + template.background_image = background_image + template.image = image + template_json = JSON.parse(template.to_json) + + expect(template_json["template"]["type"]).to eq('BodyTemplate2') + expect(template_json["template"]["textContent"]["primaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["primaryText"]["type"], + "text" => sample_json["template"]["textContent"]["primaryText"]["text"] + }) + expect(template_json["template"]["textContent"]["secondaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["secondaryText"]["type"], + "text" => sample_json["template"]["textContent"]["secondaryText"]["text"] + }) + expect(template_json["template"]["textContent"]["tertiaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["tertiaryText"]["type"], + "text" => sample_json["template"]["textContent"]["tertiaryText"]["text"] + }) + + expect(template_json["template"]["backgroundImage"]["contentDescription"]).to eq(sample_json["template"]["backgroundImage"]["contentDescription"]) + expect(template_json["template"]["backgroundImage"]["sources"][0]).to include({ + "url" => sample_json["template"]["backgroundImage"]["sources"][0]["url"] + }) + + expect(template_json["template"]["image"]["contentDescription"]).to eq(sample_json["template"]["image"]["contentDescription"]) + expect(template_json["template"]["image"]["sources"][0]).to include({ + "url" => sample_json["template"]["image"]["sources"][0]["url"], + "size" => sample_json["template"]["image"]["sources"][0]["size"], + "widthPixels" => sample_json["template"]["image"]["sources"][0]["widthPixels"], + "heightPixels" => sample_json["template"]["image"]["sources"][0]["heightPixels"] + }) + end +end diff --git a/spec/body_template3_spec.rb b/spec/body_template3_spec.rb new file mode 100644 index 0000000..ebc69b1 --- /dev/null +++ b/spec/body_template3_spec.rb @@ -0,0 +1,62 @@ +require 'rspec' + +describe 'Request handling' do + it 'validate json format' do + sample_json = JSON.parse(File.read('fixtures/sample-BodyTemplate3.json')) + template = AlexaRubykit::DisplayBodyTemplate3.new + template.token = sample_json["template"]["token"] + template.back_button = (sample_json["template"]["backButton"] == "VISIBLE") + context = AlexaRubykit::DisplayTextContext.new + context.primary_text( + sample_json["template"]["textContent"]["primaryText"]["type"], + sample_json["template"]["textContent"]["primaryText"]["text"] + ) + context.secondary_text( + sample_json["template"]["textContent"]["secondaryText"]["type"], + sample_json["template"]["textContent"]["secondaryText"]["text"] + ) + context.tertiary_text( + sample_json["template"]["textContent"]["tertiaryText"]["type"], + sample_json["template"]["textContent"]["tertiaryText"]["text"] + ) + background_image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["backgroundImage"]["sources"][0]["url"], + sample_json["template"]["backgroundImage"]["contentDescription"], + sample_json["template"]["backgroundImage"]["sources"][0]["size"], + sample_json["template"]["backgroundImage"]["sources"][0]["widthPixels"], + sample_json["template"]["backgroundImage"]["sources"][0]["heightPixels"] + ) + image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["image"]["sources"][0]["url"], + sample_json["template"]["image"]["contentDescription"] + ) + template.text_context = context + template.background_image = background_image + template.image = image + template_json = JSON.parse(template.to_json) + + expect(template_json["template"]["type"]).to eq('BodyTemplate3') + expect(template_json["template"]["textContent"]["primaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["primaryText"]["type"], + "text" => sample_json["template"]["textContent"]["primaryText"]["text"] + }) + expect(template_json["template"]["textContent"]["secondaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["secondaryText"]["type"], + "text" => sample_json["template"]["textContent"]["secondaryText"]["text"] + }) + expect(template_json["template"]["textContent"]["tertiaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["tertiaryText"]["type"], + "text" => sample_json["template"]["textContent"]["tertiaryText"]["text"] + }) + + expect(template_json["template"]["backgroundImage"]["contentDescription"]).to eq(sample_json["template"]["backgroundImage"]["contentDescription"]) + expect(template_json["template"]["backgroundImage"]["sources"][0]).to include({ + "url" => sample_json["template"]["backgroundImage"]["sources"][0]["url"] + }) + + expect(template_json["template"]["image"]["contentDescription"]).to eq(sample_json["template"]["image"]["contentDescription"]) + expect(template_json["template"]["image"]["sources"][0]).to include({ + "url" => sample_json["template"]["image"]["sources"][0]["url"] + }) + end +end diff --git a/spec/body_template6_spec.rb b/spec/body_template6_spec.rb new file mode 100644 index 0000000..273e6bb --- /dev/null +++ b/spec/body_template6_spec.rb @@ -0,0 +1,49 @@ +require 'rspec' + +describe 'Request handling' do + it 'validate json format' do + sample_json = JSON.parse(File.read('fixtures/sample-BodyTemplate6.json')) + template = AlexaRubykit::DisplayBodyTemplate6.new + template.token = sample_json["template"]["token"] + template.back_button = (sample_json["template"]["backButton"] == "VISIBLE") + context = AlexaRubykit::DisplayTextContext.new + context.primary_text( + sample_json["template"]["textContent"]["primaryText"]["type"], + sample_json["template"]["textContent"]["primaryText"]["text"] + ) + context.secondary_text( + sample_json["template"]["textContent"]["secondaryText"]["type"], + sample_json["template"]["textContent"]["secondaryText"]["text"] + ) + context.tertiary_text( + sample_json["template"]["textContent"]["tertiaryText"]["type"], + sample_json["template"]["textContent"]["tertiaryText"]["text"] + ) + background_image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["backgroundImage"]["sources"][0]["url"], + sample_json["template"]["backgroundImage"]["contentDescription"] + ) + template.text_context = context + template.background_image = background_image + template_json = JSON.parse(template.to_json) + + expect(template_json["template"]["type"]).to eq('BodyTemplate6') + expect(template_json["template"]["textContent"]["primaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["primaryText"]["type"], + "text" => sample_json["template"]["textContent"]["primaryText"]["text"] + }) + expect(template_json["template"]["textContent"]["secondaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["secondaryText"]["type"], + "text" => sample_json["template"]["textContent"]["secondaryText"]["text"] + }) + expect(template_json["template"]["textContent"]["tertiaryText"]).to include({ + "type" => sample_json["template"]["textContent"]["tertiaryText"]["type"], + "text" => sample_json["template"]["textContent"]["tertiaryText"]["text"] + }) + + expect(template_json["template"]["backgroundImage"]["contentDescription"]).to eq(sample_json["template"]["backgroundImage"]["contentDescription"]) + expect(template_json["template"]["backgroundImage"]["sources"][0]).to include({ + "url" => sample_json["template"]["backgroundImage"]["sources"][0]["url"] + }) + end +end diff --git a/spec/body_template7_spec.rb b/spec/body_template7_spec.rb new file mode 100644 index 0000000..2c88c93 --- /dev/null +++ b/spec/body_template7_spec.rb @@ -0,0 +1,28 @@ +require 'rspec' + +describe 'Request handling' do + it 'validate json format' do + sample_json = JSON.parse(File.read('fixtures/sample-BodyTemplate7.json')) + template = AlexaRubykit::DisplayBodyTemplate7.new + template.token = sample_json["template"]["token"] + template.back_button = (sample_json["template"]["backButton"] == "VISIBLE") + background_image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["backgroundImage"]["sources"][0]["url"] + ) + template.background_image = background_image + image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["image"]["sources"][0]["url"] + ) + template.image = image + template_json = JSON.parse(template.to_json) + + expect(template_json["template"]["type"]).to eq('BodyTemplate7') + + expect(template_json["template"]["backgroundImage"]["sources"][0]).to include({ + "url" => sample_json["template"]["backgroundImage"]["sources"][0]["url"] + }) + expect(template_json["template"]["image"]["sources"][0]).to include({ + "url" => sample_json["template"]["image"]["sources"][0]["url"] + }) + end +end diff --git a/spec/element_selected_request_spec.rb b/spec/element_selected_request_spec.rb new file mode 100644 index 0000000..daa6b96 --- /dev/null +++ b/spec/element_selected_request_spec.rb @@ -0,0 +1,14 @@ +require 'rspec' +require 'alexa_rubykit' + +describe 'Element Selected Request handling' do + + it 'should accept a element select request' do + sample_request = JSON.parse(File.read('fixtures/sample-element-select-request.json')) + request = AlexaRubykit::build_request(sample_request) + expect(request.type).to eq('ELEMENT_SELECTED') + expect(request.display_token).to eq('display_token') + expect(request.request_token).to eq('request_token') + end + +end diff --git a/spec/list_template1_spec.rb b/spec/list_template1_spec.rb new file mode 100644 index 0000000..3298e3f --- /dev/null +++ b/spec/list_template1_spec.rb @@ -0,0 +1,46 @@ +require 'rspec' + +describe 'Request handling' do + it 'validate json format' do + sample_json = JSON.parse(File.read('fixtures/sample-ListTemplate1.json')) + template = AlexaRubykit::DisplayListTemplate1.new + template.token = sample_json["template"]["token"] + template.back_button = (sample_json["template"]["backButton"] == "VISIBLE") + + background_image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["backgroundImage"]["sources"][0]["url"], + sample_json["template"]["backgroundImage"]["contentDescription"] + ) + + for item in sample_json["template"]["listItems"] do + context = AlexaRubykit::DisplayTextContext.new + context.primary_text( + item["textContent"]["primaryText"]["type"], + item["textContent"]["primaryText"]["text"] + ) + context.secondary_text( + item["textContent"]["secondaryText"]["type"], + item["textContent"]["secondaryText"]["text"] + ) + context.tertiary_text( + item["textContent"]["tertiaryText"]["type"], + item["textContent"]["tertiaryText"]["text"] + ) + image = AlexaRubykit::DisplayImage.new( + item["image"]["sources"][0]["url"], + item["image"]["contentDescription"] + ) + template.add_list_item( + context, + item["token"], + image + ) + end + + template.background_image = background_image + template_json = JSON.parse(template.to_json) + + expect(template_json["template"]["type"]).to eq('ListTemplate1') + expect(template_json["template"]["listItems"].length).to eq(sample_json["template"]["listItems"].length) + end +end diff --git a/spec/list_template2_spec.rb b/spec/list_template2_spec.rb new file mode 100644 index 0000000..c9be3b1 --- /dev/null +++ b/spec/list_template2_spec.rb @@ -0,0 +1,46 @@ +require 'rspec' + +describe 'Request handling' do + it 'validate json format' do + sample_json = JSON.parse(File.read('fixtures/sample-ListTemplate2.json')) + template = AlexaRubykit::DisplayListTemplate2.new + template.token = sample_json["template"]["token"] + template.back_button = (sample_json["template"]["backButton"] == "VISIBLE") + + background_image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["backgroundImage"]["sources"][0]["url"], + sample_json["template"]["backgroundImage"]["contentDescription"] + ) + + for item in sample_json["template"]["listItems"] do + context = AlexaRubykit::DisplayTextContext.new + context.primary_text( + item["textContent"]["primaryText"]["type"], + item["textContent"]["primaryText"]["text"] + ) + context.secondary_text( + item["textContent"]["secondaryText"]["type"], + item["textContent"]["secondaryText"]["text"] + ) + context.tertiary_text( + item["textContent"]["tertiaryText"]["type"], + item["textContent"]["tertiaryText"]["text"] + ) + image = AlexaRubykit::DisplayImage.new( + item["image"]["sources"][0]["url"], + item["image"]["contentDescription"] + ) + template.add_list_item( + context, + item["token"], + image + ) + end + + template.background_image = background_image + template_json = JSON.parse(template.to_json) + + expect(template_json["template"]["type"]).to eq('ListTemplate2') + expect(template_json["template"]["listItems"].length).to eq(sample_json["template"]["listItems"].length) + end +end diff --git a/spec/request_spec.rb b/spec/request_spec.rb index e0bdf37..45021fe 100644 --- a/spec/request_spec.rb +++ b/spec/request_spec.rb @@ -56,4 +56,13 @@ symbol_keys_hash = { test1: 'value', test2: { test3: 'value' } } expect(AlexaRubykit.transform_keys_to_symbols(string_keys_hash)).to eq(symbol_keys_hash) end + + it 'should create device with attributes' do + sample_request = JSON.parse(File.read('fixtures/sample-device-request.json')) + intent_request = AlexaRubykit::build_request(sample_request) + expect(intent_request.device.display_support?).to be_truthy + expect(intent_request.device.audio_player_support?).to be_truthy + expect(intent_request.device.video_app_support?).to be_truthy + expect(intent_request.device.device_id).to eq('amzn1.ask.device.') + end end diff --git a/spec/response_spec.rb b/spec/response_spec.rb index 4e1aecd..b5c506f 100644 --- a/spec/response_spec.rb +++ b/spec/response_spec.rb @@ -1,5 +1,6 @@ require 'rspec' require 'alexa_rubykit/response' +require 'alexa_rubykit/response/display_templates/display_body_template1' describe 'Builds appropriate response objects' do let(:response) { AlexaRubykit::Response.new } @@ -148,5 +149,57 @@ expect(response_object).to include(:directives) expect(response_object[:directives]).to include({'type' => 'Dialog.ConfirmIntent', 'updatedIntent' => nil}) end + + it 'should create a valid response when display template in derective' do + sample_json = JSON.parse(File.read('fixtures/sample-BodyTemplate1.json')) + template = AlexaRubykit::DisplayBodyTemplate1.new + template.token = sample_json["template"]["token"] + template.back_button = (sample_json["template"]["backButton"] == "VISIBLE") + context = AlexaRubykit::DisplayTextContext.new + context.primary_text( + sample_json["template"]["textContent"]["primaryText"]["type"], + sample_json["template"]["textContent"]["primaryText"]["text"] + ) + context.secondary_text( + sample_json["template"]["textContent"]["secondaryText"]["type"], + sample_json["template"]["textContent"]["secondaryText"]["text"] + ) + context.tertiary_text( + sample_json["template"]["textContent"]["tertiaryText"]["type"], + sample_json["template"]["textContent"]["tertiaryText"]["text"] + ) + background_image = AlexaRubykit::DisplayImage.new( + sample_json["template"]["backgroundImage"]["sources"][0]["url"] + ) + template.text_context = context + template.background_image = background_image + + response.add_display_template(template) + response_object = response.build_response_object + expect(response_object).to include(:directives) + template = response_object[:directives][0].to_hash + expect(template[:template][:type]).to eq('BodyTemplate1') + end + + it 'should create a valid response when hint include in derective' do + response.add_hint_directive("some hint") + response_object = response.build_response_object + expect(response_object).to include(:directives) + expect(response_object[:directives]).to include({ + 'type' => 'Hint', + 'hint' => { + 'type' => "PlainText", + 'text' => "some hint" + } + }) + end + + it 'should exclude session flag when end_session is nil' do + response_object = response.build_response_object + expect(response_object.has_key?(:shouldEndSession)).to be true + + response_object = response.build_response_object(nil) + expect(response_object.has_key?(:shouldEndSession)).to be false + end end end \ No newline at end of file diff --git a/spec/video_template_spec.rb b/spec/video_template_spec.rb new file mode 100644 index 0000000..dcf45a8 --- /dev/null +++ b/spec/video_template_spec.rb @@ -0,0 +1,15 @@ +require 'rspec' + +describe 'Request handling' do + it 'validate json format' do + template = AlexaRubykit::DisplayVideoTemplate.new + template.title = "video_title" + template.subtitle = "video_subtitle" + template.video_src = "https://example.com/video.mp4" + + template_json = JSON.parse(template.to_json) + + expect(template_json["type"]).to eq('VideoApp.Launch') + expect(template_json["videoItem"]["source"]).to eq("https://example.com/video.mp4") + end +end