Sha256: 572f5052797c710bcf22e0e861f2cf8cf69ddbd057583324a3ce499ab9580039

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

module Dugway
  module Filters
    module InstantCheckoutFilter
      DIV_STYLES = [
        "border-radius: 4px",
        "font-size: 20px",
        "font-weight: bold"
      ].freeze

      LINK_STYLES = [
        "border-radius: 4px",
        "height: 100%",
        "display: flex",
        "padding: 10px",
        "align-items: center",
        "justify-content: center"
      ].freeze

      def instant_checkout_button(account, a=nil, b=nil)
        account = @context.registers[:account]
        theme, height = sanitize_options(a, b)

        return nil unless account.instant_checkout?

        div_styles = generate_div_styles(theme, height)
        link_styles = generate_link_styles(theme)

        %(<div id="instant-checkout-button" style="#{ div_styles }"><a href="https://www.bigcartel.com/resources/help/article/apple-pay" style="#{ link_styles }">Instant Checkout</a></div>)
      end

      private

      def sanitize_options(a, b)
        theme = height = nil

        [a, b].each do |value|
          theme = value if /\A(dark|light(?:-outline)?)\z/.match(value)
          height = value if /\A\d+(px|em|\%)\z/.match(value)
        end

        return theme, height
      end

      def generate_div_styles(theme, height)
        styles = DIV_STYLES.dup
        styles << "border: 1px solid black" if theme == "light-outline"
        styles << "height: #{ height }" if height
        styles << colors(theme)
        styles.join("; ")
      end

      def generate_link_styles(theme)
        styles = LINK_STYLES.dup
        styles << colors(theme)
        styles.join("; ")
      end

      def colors(theme)
        if theme =~ /\Alight/
          "background-color: white; color: black"
        else
          "background-color: black; color: white"
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dugway-1.0.14 lib/dugway/liquid/filters/instant_checkout_filter.rb
dugway-1.0.13 lib/dugway/liquid/filters/instant_checkout_filter.rb
dugway-1.0.12 lib/dugway/liquid/filters/instant_checkout_filter.rb
dugway-1.0.10 lib/dugway/liquid/filters/instant_checkout_filter.rb
dugway-1.0.9 lib/dugway/liquid/filters/instant_checkout_filter.rb
dugway-1.0.8 lib/dugway/liquid/filters/instant_checkout_filter.rb