Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full width option for Adaptive card #6

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/msteams_hermes/components/adaptive_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ module Components
# 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: [])
WIDTH = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work? To incorporate a "private" structure into this class such that any outside caller would still be able to
AdaptiveCard.new(body: xyz, with: default)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd encourage you to use a module to encapsulate said constants, instead of a hash.

https://stackoverflow.com/questions/75759/how-to-implement-enums-in-ruby

default: "default",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ruby, when you want to create a constant, ruby encourages to capitalize them.

full: "full"
}.freeze

def initialize(body:, actions: nil, entities: [], width: WIDTH[:default])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to also use a guard to protect against someone using a not accepted constant/string.

raise "You can only use constants defined in  <MSTAdaptiveCardWidth>" unless MSTAdaptiveCardWidth.all.include? width

@body = body
raise "AdaptiveCard `body` must be an Array" unless @body.is_a? Array

Expand All @@ -23,6 +28,8 @@ def initialize(body:, actions: nil, entities: [])

@entities = entities
raise "AdaptiveCard `entities` must be an Array" unless @entities.nil? || @entities.is_a?(Array)

@width = width
end

def to_hash
Expand All @@ -31,7 +38,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
84 changes: 63 additions & 21 deletions spec/msteams_hermes/components/adaptive_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,77 @@
end

describe "#to_hash" do
subject(:component) { MsTeamsHermes::Components::AdaptiveCard.new(body: [fact_set], actions: [action]) }

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 "with full width" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context "with full width" do
context "with full width set to `true`" do

subject(:component) do
MsTeamsHermes::Components::AdaptiveCard.new(body: [fact_set], actions: [action],
width: MsTeamsHermes::Components::AdaptiveCard::WIDTH[:full])
end

it "renders the hash object with full width" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it "renders the hash object with full width" do
it "renders the hash object including the width field set to `full`" 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 "with not full width" do
Copy link
Contributor

@huNt-FMJ huNt-FMJ Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context "with not full width" do
context "with full_width set to `false`" do

or

Suggested change
context "with not full width" do
context "with default width" do

subject(:component) do
MsTeamsHermes::Components::AdaptiveCard.new(body: [fact_set], actions: [action],
width: MsTeamsHermes::Components::AdaptiveCard::WIDTH[:default])
end

it "renders the hash object with default width" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it "renders the hash object with default width" do
it "renders the hash object including a width entry showing `default`" do

or

Suggested change
it "renders the hash object with default width" do
it "renders the hash object including a default width" 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
3 changes: 2 additions & 1 deletion spec/msteams_hermes/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
],
actions: nil,
msteams: {
entities: []
entities: [],
width: "default"
}
}
}
Expand Down
Loading