class OEmbed::Providers

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

Constants

ClearspringWidgets

Provider for clearspring.com

CodePen

Provider for codepen.io

Embedly

Provider for Embedly.com, which is a provider aggregator. See OEmbed::Providers::Embedly.urls for a full list of supported url schemas. embed.ly/docs/endpoints/1/oembed

You can append your Embed.ly API key to the provider so that all requests are signed

OEmbed::Providers::Embedly.endpoint += "?key=#{my_embedly_key}"

If you don't yet have an API key you'll need to sign up here: embed.ly/pricing

FacebookPost

Provider for Facebook posts See developers.facebook.com/docs/plugins/oembed See developers.facebook.com/docs/graph-api/reference/v8.0/oembed-post

FacebookVideo

Provider for Facebook videos See developers.facebook.com/docs/plugins/oembed See developers.facebook.com/docs/graph-api/reference/v8.0/oembed-video

Flickr

Provider for flickr.com

Giphy

Provider for Giphy

Hulu

Provider for hulu.com

Imgur

Provider for imgur.com

Instagram

Provider for instagram.com See developers.facebook.com/docs/instagram/oembed/

Kickstarter

Provider for Kickstarter

MlgTv

provider for mlg-tv tv.majorleaguegaming.com/oembed

MovieClips

Provider for movieclips.com

MyOpera

Provider for my.opera.com my.opera.com/devblog/blog/2008/12/02/embedding-my-opera-content-oembed

NFBCanada

Provider for nfb.ca

Noembed

Provider for noembed.com, which is a provider aggregator. See OEmbed::Providers::Noembed.urls for a full list of supported url schemas. noembed.com/#supported-sites

OohEmbed

Provider for oohembed.com, which is a provider aggregator. See OEmbed::Providers::OohEmbed.urls for a full list of supported url schemas. Embed.ly has taken over the oohembed.com domain and as of July 20 all oohEmbed request will require you use an API key. For details on the transition see blog.embed.ly/oohembed

PollEverywhere

Provider for polleverywhere.com

Qik

Provider for qik.com qik.com/blog/qik-embraces-oembed-for-embedding-videos/

Revision3

Provider for revision3.com

Scribd

Provider for scribd.com

Skitch

Provider for skitch.com skitch.com/oembed/%3C/endpoint

Slideshare

Provider for slideshare.net www.slideshare.net/developers/oembed

SoundCloud

Provider for soundcloud.com developers.soundcloud.com/docs/oembed

SpeakerDeck

Provider for speakerdeck.com speakerdeck.com/faq#oembed

Spotify

Provider for spotify.com twitter.com/nicklas2k/status/330094611202723840 blog.embed.ly/post/45149936446/oembed-for-spotify

Ted

Provider for TED

TikTok

Provider for TikTok See developers.tiktok.com/doc/Embed

Tumblr

Provider for tumblr.com

TwentyThree

Provider for 23hq.com

Twitter

Provider for twitter.com dev.twitter.com/rest/reference/get/statuses/oembed

Viddler

Provider for viddler.com developers.viddler.com/documentation/services/oembed/

Vimeo

Provider for vimeo.com developer.vimeo.com/apis/oembed

Vine

Provider for vine.co dev.twitter.com/web/vine/oembed

Yfrog

Provider for yfrog code.google.com/p/imageshackapi/wiki/OEMBEDSupport

Youtube

Provider for youtube.com apiblog.youtube.com/2009/10/oembed-support.html

Options:

  • To get the iframe embed code

    OEmbed::Providers::Youtube.endpoint += "?iframe=1"
    
  • To get the flash/object embed code

    OEmbed::Providers::Youtube.endpoint += "?iframe=0"
    
  • To require https embed code

    OEmbed::Providers::Youtube.endpoint += "?scheme=https"
    

Public Class Methods

fallback() click to toggle source

Returns an array of all registerd fallback Provider instances.

# File lib/oembed/providers.rb, line 77
def fallback
  @@fallback
end
find(url) click to toggle source

Returns a Provider instance whose url scheme matches the given url. Skips any Provider with missing required_query_params.

# File lib/oembed/providers.rb, line 83
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
get(url, options = {}) click to toggle source

Finds the appropriate Provider for this url and returns an OEmbed::Response, using Provider#get.

# File lib/oembed/providers.rb, line 112
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
raw(url, options = {}) click to toggle source

Finds the appropriate Provider for this url and return the raw response. @deprecated Note: This method will be made private in the future.

# File lib/oembed/providers.rb, line 98
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
register(*providers) click to toggle source

Given one ore more Provider instances, register their url schemes for future get calls.

# File lib/oembed/providers.rb, line 23
def register(*providers)
  providers.each do |provider|
    provider.urls.each do |url|
      @@urls[url] ||= []
      @@urls[url] << provider
    end
  end
end
register_all(*including_sub_type, access_tokens: {}) click to toggle source

Register all Providers built into this gem. The including_sub_type parameter should be one of the following values:

  • :aggregators: also register provider aggregator endpoints, like Embedly

The access_tokens keys can be one of the following:

# File lib/oembed/providers.rb, line 50
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
register_fallback(*providers) click to toggle source

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

A common example:

OEmbed::Providers.register_fallback(OEmbed::ProviderDiscovery, OEmbed::Providers::Noembed)
# File lib/oembed/providers.rb, line 72
def register_fallback(*providers)
  @@fallback += providers
end
unregister(*providers) click to toggle source

Given one ore more Provider instances, un-register their url schemes. Future get calls will not use these Providers.

# File lib/oembed/providers.rb, line 34
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
unregister_all() click to toggle source

Unregister all currently-registered Provider instances.

# File lib/oembed/providers.rb, line 59
def unregister_all
  @@urls = {}
  @@fallback = []
end
urls() click to toggle source

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

# File lib/oembed/providers.rb, line 17
def urls
  @@urls
end

Private Class Methods

add_official_provider(provider_class, sub_type=nil, access_token: nil) click to toggle source

Takes an OEmbed::Provider instance and registers it so that when we call the register_all method, they all register. The sub_type can be be any value used to uniquely group providers. Official sub_types are:

  • nil: a normal provider

  • :aggregators: an endpoint for an OEmbed aggregator

:access_token takes a Hash with the following required keys:

  • :name: A Symbol: the name of access token, to be used with `register_all`

  • :method: A Symbol: the name of the required_query_params for the access token.

# File lib/oembed/providers.rb, line 135
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
set_access_tokens(access_tokens) click to toggle source

Takes a Hash of tokens, and calls the setter method for all providers that use the given tokens. Also supports “OEMBED_*_TOKEN” environment variables. Currently supported tokens:

# File lib/oembed/providers.rb, line 157
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