Class: Yt::URL
- Inherits:
-
Object
- Object
- Yt::URL
- Defined in:
- lib/yt/url.rb,
lib/yt/url/version.rb
Overview
Provides methods to identify YouTube resources from names or URLs.
Constant Summary collapse
- PLAYLIST_PATTERNS =
Returns patterns matching URLs of YouTube playlists.
[ %r{^(?:https?://)?(?:www\.)?youtube\.com/playlist/?\?list=(?<id>[a-zA-Z0-9_-]+)}, ]
- VIDEO_PATTERNS =
Returns patterns matching URLs of YouTube videos.
[ %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})}, ]
- CHANNEL_PATTERNS =
Returns patterns matching URLs of YouTube channels.
[ %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_-]+)} ]
- VERSION =
Returns the SemVer-compatible gem version.
'1.0.0'
Instance Method Summary collapse
-
#id ⇒ <String, nil>
The ID of the YouTube resource matching the URL.
-
#initialize(text) ⇒ URL
constructor
A new instance of URL.
-
#kind ⇒ Symbol
Possible values are:
:playlist
,:video
,:channel
, and +:unknown:. -
#resource(options = {}) ⇒ <Yt::Channel>
The resource associated with the URL.
Constructor Details
#initialize(text) ⇒ URL
Returns a new instance of URL.
14 15 16 17 |
# File 'lib/yt/url.rb', line 14 def initialize(text) @text = text.to_s.strip @match = find_pattern_match end |
Instance Method Details
#id ⇒ <String, nil>
Returns the ID of the YouTube resource matching the URL.
28 29 30 |
# File 'lib/yt/url.rb', line 28 def id @match['id'] ||= fetch_id end |
#kind ⇒ Symbol
Possible values are: :playlist
, :video
, :channel
, and +:unknown:.
23 24 25 |
# File 'lib/yt/url.rb', line 23 def kind @match[:kind] end |
#resource(options = {}) ⇒ <Yt::Channel>
Returns the resource associated with the URL.
33 34 35 36 37 38 39 40 |
# File 'lib/yt/url.rb', line 33 def resource( = {}) @resource ||= case kind when :channel then Yt::Channel when :video then Yt::Video when :playlist then Yt::Playlist else raise Yt::NoItemsError end.new .merge(id: id) end |