class AlexaRuby::Card

Class for Amazon Alexa app cards

Attributes

obj[R]

Public Class Methods

new(params) click to toggle source

Initialize new card object

@param params [Hash] card parameters:

type [String] card type, can be "Simple", "Standard" or "LinkAccount"
title [String] card title
content [String] card content (line breaks must be already included)
small_image_url [String] an URL for small card image
large_image_url [String] an URL for large card image

@return [Hash] ready to use card object @raise [ArgumentError] if card type is unknown

# File lib/alexa_ruby/response/card.rb, line 16
def initialize(params)
  @obj = {}
  @obj[:type] = params[:type] || 'Simple'
  raise ArgumentError, 'Unknown card type' unless valid_type?
  @params = params
  build
end

Private Instance Methods

add_content() click to toggle source

Add content to card body. Content must be already prepared and contain needed line breaks. rn or n can be used for line breaks

# File lib/alexa_ruby/response/card.rb, line 57
def add_content
  type =
    case @obj[:type]
    when 'Simple'
      :content
    when 'Standard'
      :text
    end
  @obj[type] = @params[:content]
end
add_image(type, image_url) click to toggle source

Add image to object

@param type [Symbol] image type @param image_url [String] image URL @raise [ArgumentError] if card URL doesn't starts with HTTPS

# File lib/alexa_ruby/response/card.rb, line 85
def add_image(type, image_url)
  if invalid_url?(image_url)
    raise ArgumentError, 'Card image URL must be a valid ' \
                          'SSL-enabled (HTTPS) endpoint'
  end
  @obj[:image][type] = image_url
end
add_images() click to toggle source

Add images to card

# File lib/alexa_ruby/response/card.rb, line 69
def add_images
  @obj[:image] = {}

  if @params[:small_image_url]
    add_image(:smallImageUrl, @params[:small_image_url])
  end

  return unless @params[:large_image_url]
  add_image(:largeImageUrl, @params[:large_image_url])
end
add_title() click to toggle source

Add card title to object

@raise [ArgumentError] if not title found

# File lib/alexa_ruby/response/card.rb, line 49
def add_title
  raise ArgumentError, 'Card need a title' if @params[:title].nil?
  @obj[:title] = @params[:title]
end
build() click to toggle source

Build card object

@return [Hash] ready to use card object

# File lib/alexa_ruby/response/card.rb, line 36
def build
  return if @obj[:type] == 'LinkAccount'
  add_title
  add_content unless @params[:content].nil?
  return unless @obj[:type] == 'Standard'
  return if @params[:small_image_url].nil? &&
            @params[:large_image_url].nil?
  add_images
end
invalid_url?(url) click to toggle source

Check if given URL isn't an SSL-enabled endpoint

@param url [String] some URL @return [Boolean]

# File lib/alexa_ruby/response/card.rb, line 97
def invalid_url?(url)
  Addressable::URI.parse(url).scheme != 'https'
end
valid_type?() click to toggle source

Check if card type is valid

@return [Boolean]

# File lib/alexa_ruby/response/card.rb, line 29
def valid_type?
  %w[Simple Standard LinkAccount].include? @obj[:type]
end