module Onebox::Engine

Constants

DEFAULT

Attributes

errors[R]
options[R]
timeout[R]
uri[R]
url[R]

Public Class Methods

all_iframe_origins() click to toggle source
# File lib/onebox/engine.rb, line 15
def self.all_iframe_origins
  engines.flat_map { |e| e.iframe_origins }.uniq.compact
end
engines() click to toggle source
# File lib/onebox/engine.rb, line 9
def self.engines
  constants.select do |constant|
    constant.to_s =~ /Onebox$/
  end.map(&method(:const_get))
end
included(object) click to toggle source
# File lib/onebox/engine.rb, line 5
def self.included(object)
  object.extend(ClassMethods)
end
new(url, timeout = nil) click to toggle source
# File lib/onebox/engine.rb, line 43
def initialize(url, timeout = nil)
  @errors = {}
  @options = DEFAULT
  class_name = self.class.name.split("::").last.to_s

  # Set the engine options extracted from global options.
  self.options = Onebox.options[class_name] || {}

  @url = url
  @uri = URI(url)
  if always_https?
    @uri.scheme = 'https'
    @url = @uri.to_s
  end
  @timeout = timeout || Onebox.options.timeout
end
origins_to_regexes(origins) click to toggle source
# File lib/onebox/engine.rb, line 19
def self.origins_to_regexes(origins)
  return /.*/ if origins.include?("*")
  origins.map do |origin|
    escaped_origin = Regexp.escape(origin)
    if origin.start_with?("*.", "https://*.", "http://*.")
      escaped_origin = escaped_origin.sub("\\*", '\S*')
    end

    Regexp.new("\\A#{escaped_origin}", 'i')
  end
end

Public Instance Methods

options=(opt) click to toggle source
# File lib/onebox/engine.rb, line 36
def options=(opt)
  return @options if opt.nil? # make sure options provided
  opt = opt.to_h if opt.instance_of?(OpenStruct)
  @options.merge!(opt)
  @options
end
placeholder_html() click to toggle source

Some oneboxes create iframes or other complicated controls. If you're using a live editor with HTML preview, rendering those complicated controls can be slow or cause flickering.

This method allows engines to produce a placeholder such as static image frame of a video.

By default it just calls `to_html` unless implemented.

# File lib/onebox/engine.rb, line 74
def placeholder_html
  to_html
end
to_html() click to toggle source

raises error if not defined in onebox engine. This is the output method for an engine.

# File lib/onebox/engine.rb, line 62
def to_html
  fail NoMethodError, "Engines need to implement this method"
end

Private Instance Methods

always_https?() click to toggle source
# File lib/onebox/engine.rb, line 96
def always_https?
  self.class.always_https?
end
data() click to toggle source

raises error if not defined in onebox engine in each onebox, returns hash of desired onebox content

# File lib/onebox/engine.rb, line 88
def data
  fail NoMethodError, "Engines need this method defined"
end
raw() click to toggle source

raises error if not defined in onebox engine in each onebox, uses either Nokogiri or StandardEmbed to get raw HTML from url

# File lib/onebox/engine.rb, line 82
def raw
  fail NoMethodError, "Engines need to implement this method"
end