Feature/add parler.io liquid tag for audio embed (#828)

* initial parler liquid tag

* fix eqaulity in test

* update parler tag to parse out url since it is coming in as <ahref>
This commit is contained in:
Kyle Galbraith 2018-10-16 10:54:02 -10:00 committed by Ben Halpern
parent 3bf42edd17
commit f9c19f6b81
2 changed files with 67 additions and 0 deletions

35
app/liquid_tags/parler_tag.rb Executable file
View file

@ -0,0 +1,35 @@
class ParlerTag < LiquidTagBase
def initialize(tag_name, id, tokens)
super
@id = parse_id(id)
@width = 710
@height = 120
end
def render(_context)
html = <<-HTML
<iframe
width="#{@width}"
height="#{@height}"
src="https://api.parler.io/ss/player?url=#{@id}">
</iframe>
HTML
finalize_html(html)
end
private
def parse_id(input)
input_no_space = input.delete(" ")
input_no_space = input_no_space.scan(/\bhttps?:\/\/[a-z.\/0-9-]+\b/).first
raise StandardError, "Invalid Parler URL" unless valid_id?(input_no_space)
input_no_space
end
def valid_id?(id)
puts id
id =~ /\A(https:\/\/www.parler.io\/audio\/\d{1,11}\/[a-zA-Z0-9]{11,40}.[0-9a-zA-Z-]{11,36}.mp3)\Z/
end
end
Liquid::Template.register_tag("parler", ParlerTag)

View file

@ -0,0 +1,32 @@
require "rails_helper"
RSpec.describe ParlerTag, type: :liquid_template do
describe "#id" do
let(:valid_id) { "https://www.parler.io/audio/73240183203/d53cff009eac2ab1bc9dd8821a638823c39cbcea.7dd28611-b7fc-4cf8-9977-b6e3aaf644a1.mp3" }
let(:invalid_id) { "https://www.google.com" }
def generate_new_liquid(id)
Liquid::Template.register_tag("parler", ParlerTag)
Liquid::Template.parse("{% parler #{id} %}")
end
def generate_iframe(_id)
"<iframe "\
"width=\"710\" "\
"height=\"120\" "\
"src=\"https://api.parler.io/ss/player?url=#{valid_id}\"> "\
"</iframe>"
end
it "accepts a valid Parler URL" do
liquid = generate_new_liquid(valid_id)
puts liquid
expect(liquid.render).to eq(generate_iframe(valid_id))
end
it "raises an error for invalid IDs" do
expect { generate_new_liquid(invalid_id).render }.to raise_error("Invalid Parler URL")
end
end
end