【发布时间】:2011-01-12 10:59:36
【问题描述】:
我想允许用户提交一个 vimeo url 并将其嵌入到播放器中。有没有这样的插件或gem?
谢谢
【问题讨论】:
标签: ruby-on-rails flash embed
我想允许用户提交一个 vimeo url 并将其嵌入到播放器中。有没有这样的插件或gem?
谢谢
【问题讨论】:
标签: ruby-on-rails flash embed
老问题,但我发现自己处于同样的情况。
如果您不想为您的视频检索 Vimeo iframe,您可以使用以下代码。无需添加宝石。
只需创建一个文件app/helpers/videos_helper.rb 和以下行。
module VideosHelper
require 'net/http'
VIMEO_REGEX = %r(^https?:\/\/(?:.*?)\.?(vimeo)\.com\/(\d+).*$)
# Finds Vimeo's video ID from given URL or [nil] if URL is invalid
def find_vimeo_id url
url = sanitize url
matches = VIMEO_REGEX.match url.to_str
matches[2] if matches
end
# Get Vimeo video iframe from given URL
def get_vimeo_iframe url, width, height
vimeo_id = find_vimeo_id url
uri = "https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/#{ vimeo_id }&width=#{ width }&height=#{ height }"
response = Net::HTTP.get( URI.parse( uri ))
json = JSON.parse response
json['html'].html_safe
end
end
在您看来,只需调用 get_vimeo_iframe() 即可打印 iframe。
<%= get_vimeo_iframe('http://your.vimeo.url') %>
或者如果你愿意,你可以添加宽度和高度
<%= get_vimeo_iframe('http://your.vimeo.url', '800px', '450px') %>
如果你愿意,我创建了这个 gist,它还可以让你请求 YouTube iframe(不仅仅是 Vimeo)。
【讨论】:
Vimeo 详细介绍了如何使用其 API 进行嵌入: http://www.vimeo.com/api/docs/oembed
在他们的“非官方下载”页面下,有这个 gem: http://github.com/matthooks/vimeo
【讨论】: