-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 4 commits
8ea71e6
6a80558
430362d
e542ecf
027ffa7
37100fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = { | ||
default: "default", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||
@body = body | ||
raise "AdaptiveCard `body` must be an Array" unless @body.is_a? Array | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or
Suggested change
|
||||||||||
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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or
Suggested change
|
||||||||||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,8 @@ | |
], | ||
actions: nil, | ||
msteams: { | ||
entities: [] | ||
entities: [], | ||
width: "default" | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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)
?There was a problem hiding this comment.
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