From 8e6388288672bbef8f51624e5a76a8c0694bc9ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20S=C3=A1nchez?= Date: Mon, 8 Jan 2024 09:29:39 +0100 Subject: [PATCH] Add full width option for Adaptive card (#6) * Add width option for adaptive card * Fix style * Use correct parameters * Apply suggestions * Apply Michael suggestions * Move width to style section --- .../components/adaptive_card.rb | 14 +++- lib/msteams_hermes/style.rb | 2 + lib/msteams_hermes/style/width.rb | 20 +++++ .../components/adaptive_card_spec.rb | 81 ++++++++++++++----- spec/msteams_hermes/message_spec.rb | 4 +- 5 files changed, 97 insertions(+), 24 deletions(-) create mode 100644 lib/msteams_hermes/style/width.rb diff --git a/lib/msteams_hermes/components/adaptive_card.rb b/lib/msteams_hermes/components/adaptive_card.rb index 4e7d8fa..62af657 100644 --- a/lib/msteams_hermes/components/adaptive_card.rb +++ b/lib/msteams_hermes/components/adaptive_card.rb @@ -1,20 +1,22 @@ # frozen_string_literal: true require "msteams_hermes/components/base" +require "msteams_hermes/style" module MsTeamsHermes ## # Module containing Microsoft's components representations ## module Components + include MsTeamsHermes::Style ## # A class representing Microsoft's AdaptiveCard object # https://adaptivecards.io/explorer/AdaptiveCard.html ## class AdaptiveCard < Base - attr_reader :body, :actions, :entities + attr_reader :body, :actions, :entities, :width - def initialize(body:, actions: nil, entities: []) + def initialize(body:, actions: nil, entities: [], width: Style::Width::DEFAULT) @body = body raise "AdaptiveCard `body` must be an Array" unless @body.is_a? Array @@ -23,6 +25,9 @@ def initialize(body:, actions: nil, entities: []) @entities = entities raise "AdaptiveCard `entities` must be an Array" unless @entities.nil? || @entities.is_a?(Array) + + @width = width + raise "AdaptiveCard `width` must be one of the available Width options" unless Style::Width.all.include?(width) end def to_hash @@ -31,7 +36,10 @@ def to_hash version: "1.5", body: body.map(&:to_hash), actions: actions&.map(&:to_hash), - msteams: { entities: entities.map(&:to_hash) } + msteams: { + entities: entities.map(&:to_hash), + width: width + } } end end diff --git a/lib/msteams_hermes/style.rb b/lib/msteams_hermes/style.rb index e9b9ce0..198aee2 100644 --- a/lib/msteams_hermes/style.rb +++ b/lib/msteams_hermes/style.rb @@ -5,6 +5,7 @@ require "msteams_hermes/style/font_size" require "msteams_hermes/style/font_type" require "msteams_hermes/style/font_weight" +require "msteams_hermes/style/width" module MsTeamsHermes ## @@ -16,5 +17,6 @@ module Style include MsTeamsHermes::Style::FontSize include MsTeamsHermes::Style::FontType include MsTeamsHermes::Style::FontWeight + include MsTeamsHermes::Style::Width end end diff --git a/lib/msteams_hermes/style/width.rb b/lib/msteams_hermes/style/width.rb new file mode 100644 index 0000000..c8425b3 --- /dev/null +++ b/lib/msteams_hermes/style/width.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module MsTeamsHermes + ## + # Module containing Microsoft's style representations + ## + module Style + ## + # Module containing available width options for the AdaptiveCard component + ## + module Width + DEFAULT = "default" + FULL = "full" + + def self.all + [DEFAULT, FULL] + end + end + end +end diff --git a/spec/msteams_hermes/components/adaptive_card_spec.rb b/spec/msteams_hermes/components/adaptive_card_spec.rb index db64f24..f9b0a68 100644 --- a/spec/msteams_hermes/components/adaptive_card_spec.rb +++ b/spec/msteams_hermes/components/adaptive_card_spec.rb @@ -3,6 +3,7 @@ require "msteams_hermes/components/adaptive_card" require "msteams_hermes/components/fact_set" require "msteams_hermes/actions/open_url" +require "msteams_hermes/style" RSpec.describe MsTeamsHermes::Components::AdaptiveCard do describe "#initialize" do @@ -20,35 +21,75 @@ end describe "#to_hash" do - subject(:component) { MsTeamsHermes::Components::AdaptiveCard.new(body: [fact_set], actions: [action]) } + subject(:component) do + MsTeamsHermes::Components::AdaptiveCard.new(body: [fact_set], actions: [action], width: width) + end let(:fact_set) { MsTeamsHermes::Components::FactSet.new(facts: [fact]) } let(:action) { MsTeamsHermes::Actions::OpenUrl.new(url: action_url) } let(:fact) { { title: "foo", value: "bar" } } let(:action_url) { "any_url" } - it "renders the hash object" do - hash = { - type: "AdaptiveCard", - version: "1.5", - body: [ - { - type: "FactSet", - facts: [fact] + context "initialized with full width" do + let(:width) { MsTeamsHermes::Style::Width::FULL } + + it "renders the hash object with full width parameter" do + hash = { + type: "AdaptiveCard", + version: "1.5", + body: [ + { + type: "FactSet", + facts: [fact] + } + ], + actions: [ + { + type: "Action.OpenUrl", + url: action_url, + title: nil, + tooltip: nil + } + ], + msteams: { + entities: [], + width: "full" } - ], - actions: [ - { - type: "Action.OpenUrl", - url: action_url, - title: nil, - tooltip: nil + } + + expect(component.to_hash).to eq hash + end + end + + context "initialized with the default width" do + let(:width) { MsTeamsHermes::Style::Width::DEFAULT } + + it "renders the hash object with default width parameter" do + hash = { + type: "AdaptiveCard", + version: "1.5", + body: [ + { + type: "FactSet", + facts: [fact] + } + ], + actions: [ + { + type: "Action.OpenUrl", + url: action_url, + title: nil, + tooltip: nil + } + ], + msteams: { + entities: [], + width: "default" } - ], - msteams: { entities: [] } - } + } - expect(component.to_hash).to eq hash + expect(component.to_hash).to eq hash + end end end end diff --git a/spec/msteams_hermes/message_spec.rb b/spec/msteams_hermes/message_spec.rb index 958c9d9..275f017 100644 --- a/spec/msteams_hermes/message_spec.rb +++ b/spec/msteams_hermes/message_spec.rb @@ -3,6 +3,7 @@ require "msteams_hermes/message" require "msteams_hermes/components/adaptive_card" require "msteams_hermes/components/fact_set" +require "msteams_hermes/style" RSpec.describe MsTeamsHermes::Message do describe "#initialize" do @@ -52,7 +53,8 @@ ], actions: nil, msteams: { - entities: [] + entities: [], + width: "default" } } }