Class: Onebox::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/onebox/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Matcher

Returns a new instance of Matcher.



5
6
7
8
9
10
11
12
# File 'lib/onebox/matcher.rb', line 5

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

  @options = options
end

Instance Method Details

#has_allowed_iframe_origins?(engine) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/onebox/matcher.rb', line 32

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

#oneboxedObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/onebox/matcher.rb', line 19

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 do |engine|
    (
      engine.respond_to?(:handles_content_type?) &&
        engine.handles_content_type?(@options[:content_type]) || engine === @uri
    ) && has_allowed_iframe_origins?(engine)
  end
end

#ordered_enginesObject



14
15
16
17
# File 'lib/onebox/matcher.rb', line 14

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