百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

ruby-oembed

> 编程语言
开源

oEmbed 适用于 Ruby

361 stars0 点赞5 次浏览
访问官网GitHub

工具介绍

oEmbed 适用于 Ruby

ruby-oembed

An oEmbed consumer library written in Ruby, letting you easily get embeddable HTML representations of supported web pages, based on their URLs. See oembed.com for more about the protocol.

Installation

gem install ruby-oembed

Get Started

Built-in Providers

The easiest way to use this library is to make use of the built-in providers.

ruby
OEmbed::Providers.register_all
resource = OEmbed::Providers.get('http://www.youtube.com/watch?v=2BYXBC8WQ5k')
resource.video? #=> true
resource.thumbnail_url #=> "http://i3.ytimg.com/vi/2BYXBC8WQ5k/hqdefault.jpg"
resource.html #=> 
    
    
    
    
  
HTML

Providers requiring an access token

Some built-in providers require authorization in order to work. These providers won't be registered unless an access token is provided. You can either pass access tokens to the register_app method.

ruby
OEmbed::Providers.register_all(access_tokens: { facebook: @my_facebook_token })

Or you can provide access tokens via environment variable

ruby
ENV['OEMBED_FACEBOOK_TOKEN'] #=> 'my-access-token'
OEmbed::Providers.register_all

Currently supported access tokens

access_token environment variable Associated Providers
:facebook OEMBED_FACEBOOK_TOKEN FacebookPost, FacebookVideo, Instagram

Custom Providers

If you'd like to use a provider that isn't included in the library, it's easy to create one. Just provide the oEmbed API endpoint and URL scheme(s).

ruby
my_provider = OEmbed::Provider.new("http://my.cool-service.com/api/oembed_endpoint.{format}")
my_provider  OEmbed::Response
resource.provider.name #=> "My Cool Service"

OEmbed::Providers.register(my_provider)
resource = OEmbed::Providers.get("http://a.cool-service.com/video/2") #=> OEmbed::Response

Fallback Providers

Last but not least, ruby-oembed supports Noembed, Embedly, provider discovery. The first two are provider aggregators. Each supports a wide array of websites ranging from Amazon.com to xkcd. The later is part of the oEmbed specification that allows websites to advertise support for the oEmbed protocol.

ruby
OEmbed::Providers.register_fallback(
  OEmbed::ProviderDiscovery,
  OEmbed::Providers::Noembed
)
OEmbed::Providers.get('https://www.xkcd.com/802/') #=> OEmbed::Response

Formatters

This library works wonderfully on its own, but can get a speed boost by using 3rd party libraries to parse oEmbed data. To use a 3rd party Formatter, just be sure to require the library before ruby-oembed (or include them in your Gemfile before ruby-oembed).

ruby
require 'json'
require 'xmlsimple'
require 'oembed'

OEmbed::Formatter::JSON.backend #=> OEmbed::Formatter::JSON::Backends::JSONGem
OEmbed::Formatter::XML.backend  #=> OEmbed::Formatter::XML::Backends::XmlSimple

The following, optional, backends are currently supported:

  • The JSON implementation for Ruby
  • Rails' ActiveSupport::JSON (confirmed to work with Rails 3.0.x and should work with Rails 2.0+)
  • XmlSimple

Lend a Hand

Code for the ruby-oembed library is hosted on GitHub.

bash
# Get the code.
git clone git://github.com/ruby-oembed/ruby-oembed.git
cd ruby-oembed
# Install all development-related gems.
gem install bundler
bundle install
# Run the tests.
bundle exec rake
# or run the test continually
bundle exec guard

If you encounter any bug, feel free to create an Issue.

We gladly accept pull requests! Just fork the library and commit your changes along with relevant tests. Once you're happy with the changes, send a pull request.

We do our best to keep our tests green

Contributors

Thanks to all who have made contributions to this gem, both large and small.

License

This code is free to use under the terms of the MIT license.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyoembedrubyrubygem

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言