Complete implementation and add specs (#16274)

This commit is contained in:
Arit Amana 2022-01-24 14:23:16 -05:00 committed by GitHub
parent 3f2653a14d
commit b00f68b7fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 10 deletions

View file

@ -1,9 +1,14 @@
class SlideshareTag < LiquidTagBase
PARTIAL = "liquids/slideshare".freeze
REGISTRY_REGEXP = %r{https://(?:www.)?slideshare.net/slideshow/embed_code/key/(?<id>\w{12,14})}
VALID_ID_REGEXP = /\A(?<id>\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)

View file

@ -1,5 +1,5 @@
<iframe
src="//www.slideshare.net/slideshow/embed_code/key/<%= key %>"
src="https://www.slideshare.net/slideshow/embed_code/key/<%= key %>"
alt="<%= key %> on slideshare.net"
width="100%"
height="<%= height %>"

View file

@ -173,6 +173,11 @@ RSpec.describe UnifiedEmbed::Registry do
end
end
it "returns SlideshareTag for a slideshare url" do
expect(described_class.find_liquid_tag_for(link: "https://www.slideshare.net/slideshow/embed_code/key/d5rGkEgXFDRN17"))
.to eq(SlideshareTag)
end
it "returns SoundcloudTag for a soundcloud url" do
expect(described_class.find_liquid_tag_for(link: "https://soundcloud.com/before-30-tv/stranger-moni-lati-lo-1"))
.to eq(SoundcloudTag)