Reputation: 9415
is there a rails library for taking embed code and returning the url of the video?
for example, if I pass the following:
<iframe width="420" height="315" src="//www.youtube.com/embed/J---aiyznGQ" frameborder="0" allowfullscreen></iframe>
it will return
www.youtube.com/embed/J---aiyznGQ
Upvotes: 0
Views: 1280
Reputation: 26193
Consider the following:
embed_code = '<iframe width="420" height="315" src="//www.youtube.com/embed/J---aiyznGQ" frameborder="0" allowfullscreen></iframe>'
regex = /(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/
youtube_id = embed_code.match(regex)[5]
'www.youtube.com/embed/' + youtube_id
#=> www.youtube.com/embed/J---aiyznGQ
Upvotes: 1
Reputation: 2708
You can get the video id using a regular expression and then use that to generate the url.
Here is a good link for the regex: https://gist.github.com/afeld/1254889
Upvotes: 1