Skip to content

Commit

Permalink
Add full width option for Adaptive card (#6)
Browse files Browse the repository at this point in the history
* Add width option for adaptive card

* Fix style

* Use correct parameters

* Apply suggestions

* Apply Michael suggestions

* Move width to style section
  • Loading branch information
msd117c authored Jan 8, 2024
1 parent 94a9727 commit 8e63882
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 24 deletions.
14 changes: 11 additions & 3 deletions lib/msteams_hermes/components/adaptive_card.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/msteams_hermes/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
##
Expand All @@ -16,5 +17,6 @@ module Style
include MsTeamsHermes::Style::FontSize
include MsTeamsHermes::Style::FontType
include MsTeamsHermes::Style::FontWeight
include MsTeamsHermes::Style::Width
end
end
20 changes: 20 additions & 0 deletions lib/msteams_hermes/style/width.rb
Original file line number Diff line number Diff line change
@@ -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
81 changes: 61 additions & 20 deletions spec/msteams_hermes/components/adaptive_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 3 additions & 1 deletion spec/msteams_hermes/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,7 +53,8 @@
],
actions: nil,
msteams: {
entities: []
entities: [],
width: "default"
}
}
}
Expand Down

0 comments on commit 8e63882

Please sign in to comment.