Sha256: c09781e12d46881631fcc69752bea9b82c6f2066addac49e5141217f05ac1acd

Contents?: true

Size: 880 Bytes

Versions: 5

Compression:

Stored size: 880 Bytes

Contents

# frozen_string_literal: true

module Onebox
  class Matcher
    def initialize(url, options = {})
      begin
        @uri = URI(url)
      rescue URI::InvalidURIError
      end

      @options = options
    end

    def ordered_engines
      @ordered_engines ||= Engine.engines.sort_by do |e|
        e.respond_to?(:priority) ? e.priority : 100
      end
    end

    def oneboxed
      return if @uri.nil?
      return if @uri.port && !Onebox.options.allowed_ports.include?(@uri.port)
      return if @uri.scheme && !Onebox.options.allowed_schemes.include?(@uri.scheme)
      ordered_engines.find { |engine| engine === @uri && has_allowed_iframe_origins?(engine) }
    end

    def has_allowed_iframe_origins?(engine)
      allowed_regexes = @options[:allowed_iframe_regexes] || []
      engine.iframe_origins.all? { |o| allowed_regexes.any? { |r| o =~ r } }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
onebox-2.2.19 lib/onebox/matcher.rb
onebox-2.2.18 lib/onebox/matcher.rb
onebox-2.2.17 lib/onebox/matcher.rb
onebox-2.2.16 lib/onebox/matcher.rb
onebox-2.2.15 lib/onebox/matcher.rb