Class: Onebox::Engine::YoutubeOnebox
- Inherits:
-
Object
- Object
- Onebox::Engine::YoutubeOnebox
- Includes:
- Onebox::Engine, StandardEmbed
- Defined in:
- lib/onebox/engine/youtube_onebox.rb
Constant Summary collapse
- TIMESTR_REGEX =
Regex to parse strings like ā1h3m2sā. Also accepts bare numbers (which are seconds).
/(\d+h)?(\d+m)?(\d+s?)?/
Constants included from Onebox::Engine
Instance Attribute Summary
Attributes included from Onebox::Engine
Instance Method Summary collapse
- #embed_params ⇒ Object
- #placeholder_html ⇒ Object
- #to_html ⇒ Object
-
#video_id ⇒ Object
Try to get the video ID.
Methods included from StandardEmbed
Methods included from Onebox::Engine
engines, included, #initialize, #options, #options=
Instance Method Details
#embed_params ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/onebox/engine/youtube_onebox.rb', line 63 def p = {'feature' => 'oembed', 'wmode' => 'opaque'} p['list'] = params['list'] if params['list'] # Parse timestrings, and assign the result as a start= parameter start = nil if params['start'] start = params['start'] elsif params['t'] start = params['t'] elsif uri.fragment && uri.fragment.start_with?('t=') # referencing uri is safe here because any throws were already caught by video_id returning nil # remove the t= from the start start = uri.fragment[2..-1] end p['start'] = parse_timestring(start) if start p['end'] = parse_timestring params['end'] if params['end'] # Official workaround for looping videos # https://developers.google.com/youtube/player_parameters#loop # use params.include? so that you can just add "&loop" if params.include? 'loop' p['loop'] = 1 p['playlist'] = video_id end URI.encode_www_form(p) end |
#placeholder_html ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/onebox/engine/youtube_onebox.rb', line 35 def placeholder_html if video_id "<img src='https://i1.ytimg.com/vi/#{video_id}/hqdefault.jpg' width='480' height='270'>" else to_html end end |
#to_html ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/onebox/engine/youtube_onebox.rb', line 43 def to_html if video_id # Avoid making HTTP requests if we are able to get the video ID from the # URL. html = "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/#{video_id}?#{}\" frameborder=\"0\" allowfullscreen></iframe>" else # for channel pages html = Onebox::Engine::WhitelistedGenericOnebox.new(@url, @cache, @timeout).to_html return nil unless html html = html.gsub /http:/, 'https:' html = html.gsub /"\/\//, '"https://' html = html.gsub /'\/\//, "'https://" end html end |
#video_id ⇒ Object
Try to get the video ID. Works for URLs of the form:
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/onebox/engine/youtube_onebox.rb', line 13 def video_id if uri.host =~ /youtu.be/ # A slash, then capture all non-slash characters remaining match = uri.path.match(/\/([^\/]+)/) return match[1] if match && match[1] end if uri.path =~ /\/embed\// # A slash, then embed, then anotther slash, then capture all remaining non-slash characters match = uri.path.match(/\/embed\/([^\/]+)/) return match[1] if match && match[1] end if params['v'] return params['v'] end nil rescue return nil end |