docs/urls.html
---
title: "Yt::URL"
h2: "URLs"
---
<p>
<code>Yt::URL</code> is a convenience class to identify YouTube resources based on their URL patterns:
</p>
<dl>
<dd><a class="anchor" id="snippet"></a><div class="highlight"><pre>
{% include doc.html library="yt-url" instance="URL#initialize" %}{% include example.html object='url = <span class="no">Yt</span><span class="o">::</span><span class="no">URL</span>' method='new' params=' <span class="s1">"youtu.be/gknzFj_0vvY"</span>' %}
{% include doc.html library="yt-url" instance="URL#kind" %}{% include example.html object='url' method='kind' result=':video' %}
{% include doc.html library="yt-url" instance="URL#id" %}{% include example.html object='url' method='id' result='"gknzFj_0vvY"' %}
{% include doc.html library="yt-url" instance="URL#resource" %}{% include example.html object='url' method='resource' result='#<Yt::Video @id=gknzFj_0vvY>' %}</pre>
</div></dd>
</dl>
<hr />
<h4>Configuration</h4>
<p>
<code>Yt::URL</code> <strong>is not part of the <code>yt</code> library</strong>, but comes from the <a href="https://github.com/claudiob/yt-url">yt-url</a> extension.
</p>
<p>
To use in any bundled Ruby project, remember to add the line <code>gem 'yt-url', '~> 0.0.0'</code> to its Gemfile.
</p>
<hr />
<h4>Authentication</h4>
<p>
Using <code>Yt::URL</code> does not require authentication since it is based on matching a set of hard-coded patterns:
</p>
{% highlight ruby %}
Yt::URL::PLAYLIST_PATTERNS # => [
# %r{^(?:https?://)?(?:www\.)?youtube\.com/playlist/?\?list=(?<id>[a-zA-Z0-9_-]+)},
# ]
Yt::URL::VIDEO_PATTERNS # => [
# %r{^(?:https?://)?(?:www\.)?youtube\.com/watch\?v=(?<id>[a-zA-Z0-9_-]{11})},
# %r{^(?:https?://)?(?:www\.)?youtu\.be/(?<id>[a-zA-Z0-9_-]{11})},
# %r{^(?:https?://)?(?:www\.)?youtube\.com/embed/(?<id>[a-zA-Z0-9_-]{11})},
# %r{^(?:https?://)?(?:www\.)?youtube\.com/v/(?<id>[a-zA-Z0-9_-]{11})},
# ]
Yt::URL::CHANNEL_PATTERNS # => [
# %r{^(?:https?://)?(?:www\.)?youtube\.com/channel/(?<id>UC[a-zA-Z0-9_-]{22})},
# %r{^(?:https?://)?(?:www\.)?youtube\.com/(?<format>c/|user/)?(?<name>[a-zA-Z0-9_-]+)}
# ]
{% endhighlight %}