Skip to content
A quick way to transform all links inside a string w/ their oEmbed ( information
Find file
New pull request
Pull request Compare This branch is 42 commits behind netshade:master.
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This is the oembed_links gem.  It allows you to easily parse text and
query configured providers for embedding information on the links
inside the text. A sample configuration file for configuring the
library has been included (oembed_links_example.yml), though you
may also configure the library programmatically (see rdocs).

You must have the JSON gem installed to use oembed_links.
If you have the libxml-ruby gem installed, oembed_links will use that;
it will fall back to hpricot if that is installed, and finally REXML
if you have nothing else.  

To get started quickly (in irb):

require 'oembed_links'
OEmbed.register({:method => "NetHTTP"},
                {:flickr => "",
                 :vimeo => "{format}"},
                {:flickr => { :format => "xml", :schemes => ["*"]},
                 :vimeo => { :format => "json", :schemes => ["*"]}})

# Simple transformation
OEmbed.transform("This is my flickr URL and all I did was show the URL straight to the picture")

# More complex transformation
OEmbed.transform("This is my flickr URL and this is a vimeo URL wow neat") do |r, url| { |a| "It's unlikely flickr or vimeo will give me audio" } { |p| "<img src='#{p["url"]}' alt='Sweet, a photo named #{p["title"]}' />" }
  r.from?(:vimeo) { |v| "<div class='vimeo'>#{v['html']}</div>" }

# Transformation to drive Amazon links to our department affiliate code and help us buy some laptops (hint)
OEmbed.transform("Here is a link to amazon wow") do |res, url|
    res.matches?(/amazon/) { |d|
      unless url =~ /(&|\?)tag=[^&]+/i
        url += ((url.index("?")) ? "&" : "?")
        url += "tag=wwwindystarco-20"
        <div style="text-align:center;">
          <a href='#{url}' target='_blank'>
            <img src='#{d['thumbnail_url']}' border='0' /><br />
            #{d['title']} #{"<br />by #{d['author']}" if d['author']}

To get started quickly in Rails:

Copy the included oembed_links_example.yml file to RAILS_ROOT/config/oembed_links.yml,
add a dependency to the gem in your environment.rb ( config.gem "oembed_links" )
and start your server.  That's it.  If you'd like to transform the oembedded content via
templates, you can do so using the following syntax:

OEmbed.transform(text_to_transform) do |res, url| => "oembed/video")
  res.from?(:a_provider, :template => "a_provider/oembed")
  res.matches?(/some_regex/, :template => "shared/oembed_link")
  res.any?(:template => "shared/oembed_link")

This presumes you have a directory in your Rails views directory called "oembed", and a file
of the form "video.html.erb" or "video.rhtml".  Haml templates are also supported (Erubis too).  If you are
not using Rails, you may still use the template functionality, but you must specify the full path
to the template.  In either case, you will not have access to any special "template" functionality
(such as Rails' link_to, image_tag, etc.); the templates are processed outside the Rails app Template
processing functionality. See the RDocs for OEmbed::TemplateResolver for more information.

See the rdocs for much more complete examples.  The specs directory has some examples of programmatic
use, but the test to code ratio is slim atm.

Thanks to the Indianapolis Star I&D department for open sourcing this; most notably Chris Vannoy for giving the okay.

CZ - chris.zelenak!at!!

Something went wrong with that request. Please try again.