Implement Blogcast liquid tag (#1891)

This commit is contained in:
Miguel Piedrafita 2019-02-28 21:51:20 +01:00 committed by Mac Siri
parent d33c607211
commit eb37d75c85
3 changed files with 65 additions and 0 deletions

View file

@ -0,0 +1,38 @@
class BlogcastTag < LiquidTagBase
def initialize(tag_name, id, tokens)
super
@id = parse_id(id)
end
def render(_context)
html = <<-HTML
<div class="ltag_blogcast">
<iframe frameborder="0"
scrolling="no"
id="blogcast_#{@id}"
mozallowfullscreen="true"
src="https://blogcast.host/embed/#{@id}"
style="width:100%;height:132px;overflow:hidden;"
webkitallowfullscreen="true"></iframe>
</div>
HTML
finalize_html(html)
end
private
def parse_id(input)
input_no_space = input.delete(" ")
if valid_id?(input_no_space)
input_no_space
else
raise StandardError, "Invalid Blogcast Id"
end
end
def valid_id?(id)
(id =~ /\A\d{1,9}\Z/i)&.zero?
end
end
Liquid::Template.register_tag("blogcast", BlogcastTag)

View file

@ -226,6 +226,12 @@
<pre>
{% spotify <span style="color: #1DB954;">spotify:episode:5V4XZWqZQJvbddd31n56mf</span> %}
</pre>
<h3><strong>Blogcast Tag</strong></h3>
<p>All you need is the article id code from the embed code:</p>
<pre>
{% blogcast <span style="color: orange;">1234</span> %}
</pre>
<h3><strong>Parsing Liquid Tags as a Code Example</strong></h3>
<p>To parse Liquid tags as code, simply wrap it with a single backtick or triple backticks.</p>

View file

@ -0,0 +1,21 @@
require "rails_helper"
RSpec.describe BlogcastTag, type: :liquid_template do
describe "#id" do
let(:valid_id) { "1234" }
let(:invalid_id) { "inv@lid" }
def generate_tag(id)
Liquid::Template.register_tag("blogcast", BlogcastTag)
Liquid::Template.parse("{% blogcast #{id} %}")
end
it "rejects invalid ids" do
expect { generate_tag(invalid_id) }.to raise_error(StandardError)
end
it "accepts a valid id" do
expect { generate_tag(valid_id) }.not_to raise_error
end
end
end