Home
Installing
Rails 2.x
Install the ruby-embed plugin with:
./script/plugin install git://github.com/judofyr/ruby-oembed.git
Don’t forget to require ruby-embed before using it:
require "oembed"
Rails 3
The plugin doesn’t load in Rails 3 (3.0.0.beta). Here are a few steps to make it work.
- Same steps as above, download and require the plugin.
- Install the json gem, not sure why this is needed – doesn’t Rails 3 has some build-in JSON parser – but it seems to do the trick (don’t forget sudo if needed):
gem install json - Add this gem as a requirement to your Gemfile:
gem "json" - Tell Rails the HTML returned by oEmbed is html_safe or Rails will escape the embedded HTML. This is safer but not what we want in this case:
res.html -> res.html.html_safe
Using
Register all default providers at once
You can find all the default providers included in the plugin at the bottom of the providers.rb file.
OEmbed::Providers.register_all
res = OEmbed::Providers.get("http://www.flickr.com/photos/uberlife/3307766074/")
res.html
Specifying a maximum width or height
A slightly more complex example to retrieve an image from Flickr with a maximum width and height. Flickr will return an image fitting in these limitations or nothing at all when the limitations are too small. Note: not all services support these options but Flickr does.
flickr = OEmbed::Provider.new("http://www.flickr.com/services/oembed/", :json)
flickr << "http://*.flickr.com/*"
res = flickr.get("http://www.flickr.com/photos/uberlife/3307766074/", {:maxwidth => 100, :maxheight => 100})
res.html
