From b00f68b7fe923cde6b2a69f53a0f5f44fe138566 Mon Sep 17 00:00:00 2001 From: Arit Amana <32520970+msarit@users.noreply.github.com> Date: Mon, 24 Jan 2022 14:23:16 -0500 Subject: [PATCH] Complete implementation and add specs (#16274) --- app/liquid_tags/slideshare_tag.rb | 23 +++++++++++-------- app/views/liquids/_slideshare.html.erb | 2 +- .../unified_embed/registry_spec.rb | 5 ++++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/liquid_tags/slideshare_tag.rb b/app/liquid_tags/slideshare_tag.rb index 6c36bb37d..5216b5541 100644 --- a/app/liquid_tags/slideshare_tag.rb +++ b/app/liquid_tags/slideshare_tag.rb @@ -1,9 +1,14 @@ class SlideshareTag < LiquidTagBase PARTIAL = "liquids/slideshare".freeze + REGISTRY_REGEXP = %r{https://(?:www.)?slideshare.net/slideshow/embed_code/key/(?\w{12,14})} + VALID_ID_REGEXP = /\A(?\w{12,14})\Z/ + REGEXP_OPTIONS = [REGISTRY_REGEXP, VALID_ID_REGEXP].freeze - def initialize(_tag_name, key, _parse_context) + def initialize(_tag_name, input, _parse_context) super - @key = validate(key.strip) + + stripped_input = strip_tags(input) + @key = parse_input(stripped_input) end def render(_context) @@ -11,21 +16,21 @@ class SlideshareTag < LiquidTagBase partial: PARTIAL, locals: { key: @key, - height: 450 + height: 487 }, ) end private - def validate(key) - unless key.match?(/\A[a-zA-Z0-9]{12,14}\Z/) - raise StandardError, - I18n.t("liquid_tags.slideshare_tag.invalid_slideshare_key") - end + def parse_input(input) + match = pattern_match_for(input, REGEXP_OPTIONS) + raise StandardError, I18n.t("liquid_tags.slideshare_tag.invalid_slideshare_key") unless match - key + match[:id] end end Liquid::Template.register_tag("slideshare", SlideshareTag) + +UnifiedEmbed.register(SlideshareTag, regexp: SlideshareTag::REGISTRY_REGEXP) diff --git a/app/views/liquids/_slideshare.html.erb b/app/views/liquids/_slideshare.html.erb index b86c5e7d2..e76ca7211 100644 --- a/app/views/liquids/_slideshare.html.erb +++ b/app/views/liquids/_slideshare.html.erb @@ -1,5 +1,5 @@