Implement Replit Unified Embed (#15992)

* complete implementation and specs

* add error spec

* write clearer spec
This commit is contained in:
Arit Amana 2022-01-07 15:00:06 -05:00 committed by GitHub
parent 232301e720
commit 11b7255439
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 12 deletions

View file

@ -1,31 +1,33 @@
class ReplitTag < LiquidTagBase
PARTIAL = "liquids/replit".freeze
def initialize(_tag_name, id, _parse_context)
REGISTRY_REGEXP = %r{https?://replit.com/(?<address>@\w{2,15}/[a-zA-Z0-9\-]{0,60})(?:#[\w.]+)?}
VALID_ADDRESS = %r{(?<address>@\w{2,15}/[a-zA-Z0-9\-]{0,60})(?:#[\w.]+)?}
REGEXP_OPTIONS = [REGISTRY_REGEXP, VALID_ADDRESS].freeze
def initialize(_tag_name, input, _parse_context)
super
@id = parse_id(id)
@address = parse_input(strip_tags(input))
end
def render(_context)
ApplicationController.render(
partial: PARTIAL,
locals: {
id: @id
address: @address
},
)
end
private
def parse_id(input)
input_no_space = input.delete(" ")
raise StandardError, "Invalid replit Id" unless valid_id?(input_no_space)
def parse_input(input)
match = pattern_match_for(input, REGEXP_OPTIONS)
raise StandardError, "Invalid Replit URL or @user/slug" unless match
input_no_space
end
def valid_id?(id)
id =~ %r{\A@\w{2,15}/[a-zA-Z0-9\-]{0,60}\Z}
match[:address]
end
end
Liquid::Template.register_tag("replit", ReplitTag)
UnifiedEmbed.register(ReplitTag, regexp: ReplitTag::REGISTRY_REGEXP)

View file

@ -1,3 +1,3 @@
<div class="ltag__replit">
<iframe frameborder="0" height="550px" src="<%= "https://repl.it/#{id}?lite=true" %>" loading="lazy"></iframe>
<iframe frameborder="0" height="550px" src="<%= "https://repl.it/#{address}?lite=true" %>" loading="lazy"></iframe>
</div>

View file

@ -3,6 +3,7 @@ require "rails_helper"
RSpec.describe ReplitTag, type: :liquid_tag do
describe "#id" do
let(:replit_id) { "@WigWog/PositiveFineOpensource" }
let(:invalid_replit_id) { "@Cant-Have-Dashes/PositiveFineOpensource" }
def generate_new_liquid(id)
Liquid::Template.register_tag("replit", ReplitTag)
@ -17,6 +18,11 @@ RSpec.describe ReplitTag, type: :liquid_tag do
it "renders iframe" do
liquid = generate_new_liquid(replit_id)
expect(liquid.render).to include("<iframe")
expect(liquid.render).to include("https://repl.it/#{replit_id}?lite=true")
end
it "raises an error for invalid replit id" do
expect { generate_new_liquid(invalid_replit_id).render }.to raise_error("Invalid Replit URL or @user/slug")
end
end
end

View file

@ -25,6 +25,11 @@ RSpec.describe UnifiedEmbed::Registry do
"instagram.com/p/CXgzXWXroHK/",
]
valid_replit_url_formats = [
"https://replit.com/@msarit/Shell-Challenge#index.html",
"https://replit.com/@msarit/Shell-Challenge",
]
valid_spotify_url_formats = [
"https://open.spotify.com/track/64csu9GsP563GpjaSvU17w?si=eaac508fe9394a93",
"https://open.spotify.com/track/64csu9GsP563GpjaSvU17w",
@ -111,6 +116,13 @@ RSpec.describe UnifiedEmbed::Registry do
.to eq(RedditTag)
end
it "returns ReplitTag for a valid replit url" do
valid_replit_url_formats.each do |url|
expect(described_class.find_liquid_tag_for(link: url))
.to eq(ReplitTag)
end
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)