Last active
July 6, 2023 08:21
-
-
Save a-barbieri/a93281cd5c01025664e5cbd781cd917c to your computer and use it in GitHub Desktop.
Rails helper to embed YouTube and Vimeo iframe in view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helpers for better embedding and manipulation of videos | |
# Place this code in app/helpers/videos_helper.rb | |
# Then from any view you can add: | |
# | |
# <%= get_video_iframe('http://the.video.url') %> | |
# | |
# Optionally you can add width and height. | |
# | |
# <%= get_video_iframe('http://the.video.url', '1600px', '900px') %> | |
# | |
# Thanks to https://stackoverflow.com/a/27878890/1498118 | |
module VideosHelper | |
require 'net/http' | |
# Regex to find YouTube's and Vimeo's video ID | |
YOUTUBE_REGEX = %r(^(http[s]*:\/\/)?(www.)?(youtube.com|youtu.be)\/(watch\?v=){0,1}([a-zA-Z0-9_-]{11})) | |
VIMEO_REGEX = %r(^https?:\/\/(?:.*?)\.?(vimeo)\.com\/(\d+).*$) | |
# Finds YouTube's video ID from given URL or [nil] if URL is invalid | |
# The video ID matches the RegEx \[a-zA-Z0-9_-]{11}\ | |
def find_youtube_id url | |
url = sanitize url | |
matches = YOUTUBE_REGEX.match url.to_str | |
if matches | |
matches[6] || matches[5] | |
end | |
end | |
# Get YouTube video iframe from given URL | |
def get_youtube_iframe url, width, height | |
youtube_id = find_youtube_id url | |
result = %(<iframe title="YouTube video player" width="#{width}" | |
height="#{height}" src="//www.youtube.com/embed/#{ youtube_id }" | |
frameborder="0" allowfullscreen></iframe>) | |
result.html_safe | |
end | |
# 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 }" | |
# see -> https://stackoverflow.com/a/4581095/1498118 | |
response = Net::HTTP.get( URI.parse( uri )) | |
json = JSON.parse response | |
json['html'].html_safe | |
end | |
# Main function | |
# Return a video iframe | |
# If the url provided is not a valid YouTube or Vimeo url it returns [nil] | |
def get_video_iframe(url, width = "560px", height = "315px") | |
if find_vimeo_id(url) | |
get_vimeo_iframe(url, width, height) | |
elsif find_youtube_id(url) | |
get_youtube_iframe(url, width, height) | |
end | |
end | |
end |
Dude, been looking everywhere online for something like this. Great job. Works like a charm!
๐
Nice work! Thanks for putting this together!
Thank you!๐
Thank you!
Sankyu Mastah .... :3
OMG, you're the beeest <3 Thank you Thank you <3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! Thank you for sharing