将vimeo播放器嵌入到Rails中? [英] Embed vimeo player in Rails?

查看:130
本文介绍了将vimeo播放器嵌入到Rails中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户提交一个vimeo网址并将其嵌入到播放器中。有没有一个插件或创业板这样做?



谢谢

老问题,但我发现自己在相同的情况。



如果您不想为视频检索Vimeo iframe,则可以使用以下代码。无需添加宝石。



添加助手



只需创建一个文件 app /helpers/videos_helper.rb 和下面几行。
$ b

  module VideosHelper 

require'net / http'

VIMEO_REGEX =%r(^ https?:\ / \ /(?:.*?)\。?( vimeo)\ .com \ /(\ d +)。* $)

#如果URL无效,则从给定的URL查找Vimeo的视频ID或[nil]
def find_vimeo_id url
url =清理url
匹配= VIMEO_REGEX.match url.to_str
匹配[2]如果匹配
结束

#从给定URL获取Vimeo视频iframe
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。解析响应
json [ 'html']。html_safe
end

end



获取iframe in view



在您的视图中,只需调用 get_vimeo_iframe()即可打印iframe。
$ b

 <%= get_vimeo_iframe('http://your.vimeo.url')%> 

或者如果您想要的话可​​以添加宽度和高度

 <%= get_vimeo_iframe('http://your.vimeo.url','800px','450px')%> 

如果您想要我创建 gist 让您也可以询问 YouTube iframe(不仅仅是Vimeo)。

I want to allow users to submit a vimeo url and embed that in a player. Is there a plugin or gem that does that?

Thanks

解决方案

Old question but I found myself in the same situation.

If you wan't to retrieve Vimeo iframe for your video you can use the following code. No need to add a gem.

Add helper

Just create a file app/helpers/videos_helper.rb and the following lines.

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 iframe in view

In your view just call get_vimeo_iframe() to print the iframe.

<%= get_vimeo_iframe('http://your.vimeo.url') %>

Or if you want you can add width and height

<%= get_vimeo_iframe('http://your.vimeo.url', '800px', '450px') %>

If you want I created this gist which lets you ask also for YouTube iframe (not just Vimeo).

这篇关于将vimeo播放器嵌入到Rails中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆