-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows us to extract embed codes, their uuids and their types when given a block of Govspeak.
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Govspeak | ||
class EmbedExtractor | ||
def initialize(document) | ||
@document = document | ||
end | ||
|
||
def content_references | ||
@content_references ||= @document.scan(EmbeddedContent::EMBED_REGEX).map { |match| | ||
EmbeddedContent.new(document_type: match[1], content_id: match[2], embed_code: match[0]) | ||
}.uniq | ||
end | ||
|
||
def content_ids | ||
@content_ids ||= content_references.map(&:content_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Govspeak | ||
class EmbeddedContent | ||
SUPPORTED_DOCUMENT_TYPES = %w[contact content_block_email_address].freeze | ||
UUID_REGEX = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/ | ||
EMBED_REGEX = /({{embed:(#{SUPPORTED_DOCUMENT_TYPES.join('|')}):#{UUID_REGEX}}})/ | ||
|
||
attr_reader :document_type, :content_id, :embed_code | ||
|
||
def initialize(document_type:, content_id:, embed_code:) | ||
@document_type = document_type | ||
@content_id = content_id | ||
@embed_code = embed_code | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require "test_helper" | ||
|
||
class EmbedExtractorTest < Minitest::Test | ||
extend Minitest::Spec::DSL | ||
|
||
describe "EmbedExtractor" do | ||
subject { Govspeak::EmbedExtractor.new(document) } | ||
|
||
describe "when there is no embedded content" do | ||
let(:document) { "foo" } | ||
|
||
describe "#content_references" do | ||
it "returns an empty array" do | ||
assert_equal [], subject.content_references | ||
end | ||
end | ||
|
||
describe "#content_ids" do | ||
it "returns an empty array" do | ||
assert_equal [], subject.content_ids | ||
end | ||
end | ||
end | ||
|
||
describe "when there is embedded content" do | ||
let(:contact_uuid) { SecureRandom.uuid } | ||
let(:content_block_email_address_uuid) { SecureRandom.uuid } | ||
|
||
let(:document) do | ||
""" | ||
{{embed:contact:#{contact_uuid}}} | ||
{{embed:content_block_email_address:#{content_block_email_address_uuid}}} | ||
""" | ||
end | ||
|
||
describe "#content_references" do | ||
it "returns all references" do | ||
result = subject.content_references | ||
|
||
assert_equal 2, result.count | ||
|
||
assert_equal "contact", result[0].document_type | ||
assert_equal contact_uuid, result[0].content_id | ||
assert_equal "{{embed:contact:#{contact_uuid}}}", result[0].embed_code | ||
|
||
assert_equal "content_block_email_address", result[1].document_type | ||
assert_equal content_block_email_address_uuid, result[1].content_id | ||
assert_equal "{{embed:content_block_email_address:#{content_block_email_address_uuid}}}", result[1].embed_code | ||
end | ||
end | ||
|
||
describe "#content_ids" do | ||
it "returns all uuids as an array" do | ||
assert_equal [contact_uuid, content_block_email_address_uuid], subject.content_ids | ||
end | ||
end | ||
end | ||
end | ||
end |