Implement GitPitchTag (#4942)

This commit is contained in:
Jared Harbison 2019-12-19 19:59:15 -05:00 committed by Mac Siri
parent 1b2cdfb0c4
commit 07fd2281cd
4 changed files with 95 additions and 0 deletions

View file

@ -0,0 +1,38 @@
class GitPitchTag < LiquidTagBase
PARTIAL = "liquids/gitpitch".freeze
URL_REGEXP = /(http|https):\/\/gitpitch.com\/[a-zA-Z0-9\-\/]*/.freeze
def initialize(tag_name, link, tokens)
super
@link = parse_link(link)
end
def render(_context)
ActionController::Base.new.render_to_string(
partial: PARTIAL,
locals: {
link: @link
},
)
end
private
def parse_link(link)
stripped_link = ActionController::Base.helpers.strip_tags(link)
the_link = stripped_link.split(" ").first
raise_error unless valid_link?(the_link)
the_link
end
def valid_link?(link)
link_no_space = link.delete(" ")
(link_no_space =~ URL_REGEXP)&.zero?
end
def raise_error
raise StandardError, "Invalid GitPitch URL"
end
end
Liquid::Template.register_tag("gitpitch", GitPitchTag)

View file

@ -0,0 +1,5 @@
<iframe
height="450"
src="<%= link %>"
loading="lazy">
</iframe>

View file

@ -167,6 +167,11 @@
</code></p>
</dd>
</dl>
<h3><strong>GitPitch Embed</strong></h3>
<p>All you need is the GitPitch link:</p>
<code>
{% gitpitch https://gitpitch.com/gitpitch/in-60-seconds %}
</code>
<h3><strong>Video Embed</strong></h3>
<p>All you need is the <code>id</code> from the URL.
<ul>

View file

@ -0,0 +1,47 @@
require "rails_helper"
RSpec.describe GitPitchTag, type: :liquid_tag do
describe "#link" do
let(:valid_links) do
[
"https://gitpitch.com/gitpitch/in-60-seconds",
"https://gitpitch.com/gitpitch/what-is-gitpitch",
"https://gitpitch.com/gitpitch/demo-deck",
]
end
let(:bad_links) do
[
"//pastebin.com/raw/b77FrXUA#gist.github.com",
"https://gitpitch.com/gitpitch/github.com@evil.com",
"https://gitpitch.github.com.evil.com",
"https://github.com/string/string/raw/string/file",
]
end
def generate_tag(link)
Liquid::Template.register_tag("gitpitch", GitPitchTag)
Liquid::Template.parse("{% gitpitch #{link} %}")
end
def generate_script(link)
html = <<~HTML
<iframe height="450" src="#{link}" loading="lazy"></iframe>
HTML
html.tr("\n", " ").delete(" ")
end
it "rejects invalid gitpitch url" do
expect do
generate_new_liquid("really_long_invalid_link")
end.to raise_error(StandardError)
end
it "accepts valid gitpitch url" do
valid_links.each do |link|
liquid = generate_tag(link)
expect(liquid.render.tr("\n", " ").delete(" ")).to eq(generate_script(link))
end
end
end
end