class OEmbed::Providers

at once.
Allows OEmbed to perform tasks across several, registered, Providers

def add_official_provider(provider_class, sub_type=nil, access_token: nil)

* :method: A Symbol: the name of the required_query_params for the access token.
* :name: A Symbol: the name of access token, to be used with `register_all`
:access_token takes a Hash with the following required keys:
* :aggregators: an endpoint for an OEmbed aggregator
* nil: a normal provider
used to uniquely group providers. Official sub_types are:
The sub_type can be be any value
the register_all method, they all register.
Takes an OEmbed::Provider instance and registers it so that when we call
def add_official_provider(provider_class, sub_type=nil, access_token: nil)
  raise TypeError, "Expected OEmbed::Provider instance but was #{provider_class.class}" \
    unless provider_class.is_a?(OEmbed::Provider)
  @@to_register[sub_type.to_s] ||= []
  @@to_register[sub_type.to_s] << provider_class
  if access_token.is_a?(Hash) && access_token[:name] && access_token[:method]
    setter_method = "#{access_token[:method]}="
    raise TypeError, "Expected OEmbed::Provider instance to respond to the given access_token method #{setter_method}" \
      unless provider_class.respond_to?(setter_method)
    @@access_token_setters[access_token[:name]] ||= []
    @@access_token_setters[access_token[:name]] << provider_class.method(setter_method)
  end
end

def fallback

Returns an array of all registerd fallback Provider instances.
def fallback
  @@fallback
end

def find(url)

Skips any Provider with missing required_query_params.
Returns a Provider instance whose url scheme matches the given url.
def find(url)
  @@urls.keys.each do |url_regexp|
    next unless url_regexp.match?(url)
    matching_provider = @@urls[url_regexp].detect { |p| p.include?(url) }
    # If we've found a matching provider, return it right away!
    return matching_provider if matching_provider
  end
  nil
end

def get(url, options = {})

using Provider#get.
Finds the appropriate Provider for this url and returns an OEmbed::Response,
def get(url, options = {})
  provider = find(url)
  if provider
    provider.get(url, options)
  else
    fallback.each do |p|
      return p.get(url, options) rescue OEmbed::Error
    end
    raise(OEmbed::NotFound)
  end
end

def raw(url, options = {})

Deprecated:
  • *Note*: This method will be made private in the future.
def raw(url, options = {})
  provider = find(url)
  if provider
    provider.raw(url, options)
  else
    fallback.each do |p|
      return p.raw(url, options) rescue OEmbed::Error
    end
    raise(OEmbed::NotFound)
  end
end

def register(*providers)

future get calls.
Given one ore more Provider instances, register their url schemes for
def register(*providers)
  providers.each do |provider|
    provider.urls.each do |url|
      @@urls[url] ||= []
      @@urls[url] << provider
    end
  end
end

def register_all(*including_sub_type, access_tokens: {})

* :facebook: See https://developers.facebook.com/docs/instagram/oembed#access-tokens
The access_tokens keys can be one of the following:
* :aggregators: also register provider aggregator endpoints, like Embedly
The including_sub_type parameter should be one of the following values:
Register all Providers built into this gem.
def register_all(*including_sub_type, access_tokens: {})
  register(*@@to_register[""])
  including_sub_type.each do |sub_type|
    register(*@@to_register[sub_type.to_s])
  end
  set_access_tokens(access_tokens)
end

def register_fallback(*providers)

OEmbed::Providers.register_fallback(OEmbed::ProviderDiscovery, OEmbed::Providers::Noembed)
A common example:

will be called (in order) with the URL.
any of the registerd url patters the fallback providers
When the raw or get methods are called, if the URL doesn't match
Use this method to register fallback providers.
Takes an array of Provider instances or ProviderDiscovery
def register_fallback(*providers)
  @@fallback += providers
end

def set_access_tokens(access_tokens)

* facebook: See https://developers.facebook.com/docs/instagram/oembed#access-tokens
Currently supported tokens:
Also supports "OEMBED_*_TOKEN" environment variables.
for all providers that use the given tokens.
Takes a Hash of tokens, and calls the setter method
def set_access_tokens(access_tokens)
  access_tokens.each do |token_name, token_value|
    token_name = token_name.to_sym
    next unless @@access_token_setters.has_key?(token_name)
    @@access_token_setters[token_name].each do |token_setter_method|
      token_setter_method.call(token_value)
    end
  end
end

def unregister(*providers)

Future get calls will not use these Providers.
Given one ore more Provider instances, un-register their url schemes.
def unregister(*providers)
  providers.each do |provider|
    provider.urls.each do |url|
      if @@urls[url].is_a?(Array)
        @@urls[url].delete(provider)
        @@urls.delete(url) if @@urls[url].empty?
      end
    end
  end
end

def unregister_all

Unregister all currently-registered Provider instances.
def unregister_all
  @@urls = {}
  @@fallback = []
end

def urls

that support that scheme.
all registered Provider instances and values are an Array of Providers
A Hash of all url schemes, where the keys represent schemes supported by
def urls
  @@urls
end