From feebae6ae3d1b8e751169fb6b14d07dae87dfd2a Mon Sep 17 00:00:00 2001 From: Arit Amana <32520970+msarit@users.noreply.github.com> Date: Wed, 26 Jan 2022 16:19:27 -0500 Subject: [PATCH] Implement Unified Embeds for Stackery URLs (#16320) * complete implementation and add specs * nudge Travis * optimization --- app/liquid_tags/stackery_tag.rb | 47 ++++++++++++------- app/views/liquids/_stackery.html.erb | 2 +- spec/liquid_tags/stackery_tag_spec.rb | 4 +- .../unified_embed/registry_spec.rb | 5 ++ 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/app/liquid_tags/stackery_tag.rb b/app/liquid_tags/stackery_tag.rb index f439bf34f..333b352d4 100644 --- a/app/liquid_tags/stackery_tag.rb +++ b/app/liquid_tags/stackery_tag.rb @@ -1,44 +1,55 @@ class StackeryTag < LiquidTagBase PARTIAL = "liquids/stackery".freeze + REGISTRY_REGEXP = %r{https://app.stackery.io/editor/design(?\?.*)?} + PARAM_REGEXP = /\A(owner=[\w\-]+)|(repo=[\w\-]+)|(file=.*)|(ref=.*)\Z/ def initialize(_tag_name, input, _parse_context) super - @data = get_data(input.strip) + + stripped_input = strip_tags(input) + unescaped_input = CGI.unescape_html(stripped_input) + @params = parsed_input(unescaped_input) end def render(_context) ApplicationController.render( partial: PARTIAL, locals: { - owner: @data[:owner], - repo: @data[:repo], - ref: @data[:ref] + params: @params }, ) end private - def get_data(input) - items = input.split - owner = items.first - repo = items.second - ref = items.third || "master" + def parsed_input(input) + if input.split.length > 1 + params = input.split + owner = params.first + repo = params.second + ref = params.third || "master" - validate_items(owner, repo) + raise StandardError, I18n.t("liquid_tags.stackery_tag.missing_argument") unless owner && repo - { - owner: owner, - repo: repo, - ref: ref - } + "owner=#{owner}&repo=#{repo}&ref=#{ref}" + else + match = pattern_match_for(input, [REGISTRY_REGEXP]) + raise StandardError, I18n.t("liquid_tags.stackery_tag.missing_argument") unless match + + extract_params(match[:params]) + end end - def validate_items(owner, repo) - return unless owner.blank? || repo.blank? + def extract_params(params) + params = params.delete("?").split("&") + params.select { |param| valid_param(param) }.join("&") + end - raise StandardError, I18n.t("liquid_tags.stackery_tag.missing_argument") + def valid_param(param) + (param =~ PARAM_REGEXP)&.zero? end end Liquid::Template.register_tag("stackery", StackeryTag) + +UnifiedEmbed.register(StackeryTag, regexp: StackeryTag::REGISTRY_REGEXP) diff --git a/app/views/liquids/_stackery.html.erb b/app/views/liquids/_stackery.html.erb index b89d6fed5..7f6d16db6 100644 --- a/app/views/liquids/_stackery.html.erb +++ b/app/views/liquids/_stackery.html.erb @@ -1,6 +1,6 @@ diff --git a/spec/liquid_tags/stackery_tag_spec.rb b/spec/liquid_tags/stackery_tag_spec.rb index e99669a5c..59105a030 100644 --- a/spec/liquid_tags/stackery_tag_spec.rb +++ b/spec/liquid_tags/stackery_tag_spec.rb @@ -17,7 +17,9 @@ RSpec.describe StackeryTag, type: :liquid_tag do it "renders valid input" do template = generate_tag(valid_input) - expected = 'src="//app.stackery.io/editor/design?owner=deeheber&repo=lambda-layer-example&ref=layer-resource' + # rubocop:disable Layout/LineLength + expected = "src=\"//app.stackery.io/editor/design?owner=deeheber&repo=lambda-layer-example&ref=layer-resource\"" + # rubocop:enable Layout/LineLength expect(template.render(nil)).to include(expected) end diff --git a/spec/liquid_tags/unified_embed/registry_spec.rb b/spec/liquid_tags/unified_embed/registry_spec.rb index 3e1026b5d..bd09670e8 100644 --- a/spec/liquid_tags/unified_embed/registry_spec.rb +++ b/spec/liquid_tags/unified_embed/registry_spec.rb @@ -195,6 +195,11 @@ RSpec.describe UnifiedEmbed::Registry do end end + it "returns StackeryTag for a stackery url" do + expect(described_class.find_liquid_tag_for(link: "https://app.stackery.io/editor/design?owner=stackery&repo=quickstart-ruby&file=template.yaml")) + .to eq(StackeryTag) + end + it "returns TweetTag for a tweet url" do expect(described_class.find_liquid_tag_for(link: "https://twitter.com/aritdeveloper/status/1483614684884484099")) .to eq(TweetTag)