Complete Implementation and add specs (#16095)

This commit is contained in:
Arit Amana 2022-01-14 09:33:35 -05:00 committed by GitHub
parent 3253ba2c7a
commit 0c6ea5d9e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 21 deletions

View file

@ -2,11 +2,11 @@ require "uri"
class DotnetFiddleTag < LiquidTagBase
PARTIAL = "liquids/dotnetfiddle".freeze
LINK_REGEXP = %r{\A(https)://(dotnetfiddle\.net)/(Widget)/[a-zA-Z0-9\-/]*\z}
REGISTRY_REGEXP = %r{https://dotnetfiddle.net(?:/Widget)?/(?<id>[\w\-]+)}
def initialize(_tag_name, link, _parse_context)
super
@link = parse_link(link)
@link = parse_link(strip_tags(link))
end
def render(_context)
@ -22,28 +22,20 @@ class DotnetFiddleTag < LiquidTagBase
private
def parse_link(link)
stripped_link = ActionController::Base.helpers.strip_tags(link)
the_link = stripped_link.split.first
raise StandardError, "Invalid DotnetFiddle URL" unless valid_link?(the_link)
match = pattern_match_for(link, [REGISTRY_REGEXP])
raise StandardError, "Invalid DotnetFiddle URL" unless match
insert_widget(the_link)
insert_widget(link, match)
end
def insert_widget(link)
def insert_widget(link, match)
uri = URI(link)
if "Widget".in? uri.path
link
else
# URI.path gives us strings like "/abcde", use substring to get rid of forward slash
# otherwise join won't work
URI.join("https://dotnetfiddle.net", "/Widget/", uri.path[1..]).to_s
end
end
return link if uri.path.include?("Widget")
def valid_link?(link)
link_no_space = link.delete(" ")
link_no_space.match?(LINK_REGEXP)
"https://dotnetfiddle.net/Widget/#{match[:id]}"
end
end
Liquid::Template.register_tag("dotnetfiddle", DotnetFiddleTag)
UnifiedEmbed.register(DotnetFiddleTag, regexp: DotnetFiddleTag::REGISTRY_REGEXP)

View file

@ -36,7 +36,7 @@ class YoutubeTag < LiquidTagBase
def parse_id_or_url(input)
match = pattern_match_for(input, REGEXP_OPTIONS)
raise StandardError, "Invalid YouTube ID" unless match
raise StandardError, "Invalid YouTube ID or URL" unless match
video_id = match[:video_id]
time_parameter = match[:time_parameter]

View file

@ -98,6 +98,11 @@ RSpec.describe UnifiedEmbed::Registry do
.to eq(CodepenTag)
end
it "returns DotnetFiddleTag for a dotnetfiddle url" do
expect(described_class.find_liquid_tag_for(link: "https://dotnetfiddle.net/PmoDip"))
.to eq(DotnetFiddleTag)
end
it "returns InstagramTag for a valid instagram url" do
valid_instagram_url_formats.each do |url|
expect(described_class.find_liquid_tag_for(link: url))

View file

@ -41,8 +41,8 @@ RSpec.describe YoutubeTag, type: :liquid_tag do
end
# rubocop:enable Style/StringLiterals
it "raises an error for invalid IDs" do
expect { generate_new_liquid(invalid_id).render }.to raise_error("Invalid YouTube ID")
it "raises an error for invalid IDs or URLs" do
expect { generate_new_liquid(invalid_id).render }.to raise_error("Invalid YouTube ID or URL")
end
end
end