Sha256: b603fcb73910722525604516cb98bd7ec099c10e9fcb745c6d0b158e922ca009

Contents?: true

Size: 867 Bytes

Versions: 23

Compression:

Stored size: 867 Bytes

Contents

# frozen_string_literal: true

module Onebox
  class Matcher
    def initialize(link, options = {})
      @url = link
      @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
      uri = URI(@url)
      return unless uri.port.nil? || Onebox.options.allowed_ports.include?(uri.port)
      return unless uri.scheme.nil? || Onebox.options.allowed_schemes.include?(uri.scheme)
      ordered_engines.find { |engine| engine === uri && has_allowed_iframe_origins?(engine) }
    rescue URI::InvalidURIError
      nil
    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

23 entries across 23 versions & 1 rubygems

Version Path
onebox-2.1.2 lib/onebox/matcher.rb
onebox-2.1.1 lib/onebox/matcher.rb
onebox-2.1.0 lib/onebox/matcher.rb