lib/ralyxa/card.rb in ralyxa-1.3.0 vs lib/ralyxa/card.rb in ralyxa-1.4.0
- old
+ new
@@ -1,58 +1,70 @@
require_relative './errors'
module Ralyxa
class Card
- SIMPLE_CARD_TYPE = "Simple"
- STANDARD_CARD_TYPE = "Standard"
+ LINK_ACCOUNT_CARD_TYPE = "LinkAccount"
+ SIMPLE_CARD_TYPE = "Simple"
+ STANDARD_CARD_TYPE = "Standard"
- def initialize(title, body, image_url)
- @title = title
- @body = body
- @image_url = image_url
+ def initialize(options)
+ @options = options
end
def self.as_hash(title, body, image_url = nil)
- new(title, body, image_url).to_h
+ new(title: title, body: body, image_url: image_url).to_h
end
+ def self.link_account
+ new(link_account: true).to_h
+ end
+
def to_h
Hash.new.tap do |card|
set_type(card)
- card[:title] = @title
- set_body(card)
- set_image(card) if @image_url
+ set_title(card) if @options[:title]
+ set_body(card) if @options[:body]
+ set_image(card) if @options[:image_url]
end
end
private
def set_type(card)
- card[:type] = SIMPLE_CARD_TYPE if simple?
- card[:type] = STANDARD_CARD_TYPE if standard?
+ return card[:type] = LINK_ACCOUNT_CARD_TYPE if link_account?
+ card[:type] = SIMPLE_CARD_TYPE if simple?
+ card[:type] = STANDARD_CARD_TYPE if standard?
end
+ def set_title(card)
+ card[:title] = @options[:title]
+ end
+
def set_body(card)
- card[:content] = @body if simple?
- card[:text] = @body if standard?
+ card[:content] = @options[:body] if simple?
+ card[:text] = @options[:body] if standard?
end
def set_image(card)
- 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)
+ raise UnsecureUrlError.new("Card images must be available at an SSL-enabled (HTTPS) endpoint. Your current image url is: #{ @options[:image_url] }") unless secure?
card[:image] = Hash.new
- card[:image][:smallImageUrl] = @image_url
- card[:image][:largeImageUrl] = @image_url
+ card[:image][:smallImageUrl] = @options[:image_url]
+ card[:image][:largeImageUrl] = @options[:image_url]
end
+ def link_account?
+ !!@options[:link_account]
+ end
+
def simple?
- !@image_url
+ !@options[:image_url]
end
def standard?
- !!@image_url
+ !!@options[:image_url]
end
- def secure?(url)
- URI.parse(url).scheme == "https"
+ def secure?
+ URI.parse(@options[:image_url]).scheme == "https"
end
end
end
\ No newline at end of file