Class: Yt::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/yt/resource.rb

Overview

Provides a base class for YouTube channels, videos, playlists and items. This is an abstract class and should not be instantiated directly.

Direct Known Subclasses

Channel, Playlist, PlaylistItem, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • data (Hash<Symbol, String>) (defaults to: {})

    the options to initialize a resource.

Options Hash (data):

  • :id (String)

    The unique ID of a YouTube resource.



7
8
9
10
# File 'lib/yt/resource.rb', line 7

def initialize(data = {})
  @data = data
  @selected_data_parts = []
end

Instance Attribute Details

#dataHash (readonly)

Returns the resource’s data.

Returns:

  • (Hash)

    the resource’s data.



18
19
20
# File 'lib/yt/resource.rb', line 18

def data
  @data
end

Class Method Details

.where(conditions = {}) ⇒ Yt::Relation<Yt::Video>

Returns the videos matching the conditions.

Returns:

  • (Yt::Relation<Yt::Video>)

    the videos matching the conditions.



34
35
36
37
38
39
40
41
# File 'lib/yt/resource.rb', line 34

def self.where(conditions = {})
  @where ||= Relation.new(self) do |options|
    slicing_conditions_every(50) do |slice_options|
      fetch resources_path, where_params(slice_options)
    end
  end
  @where.where conditions
end

Instance Method Details

#idString

Returns the resource’s unique ID.

Returns:

  • (String)

    the resource’s unique ID.



13
14
15
# File 'lib/yt/resource.rb', line 13

def id
  @data[:id]
end

#inspectString

Returns a representation of the resource instance.

Returns:

  • (String)

    a representation of the resource instance.



21
22
23
# File 'lib/yt/resource.rb', line 21

def inspect
  "#<#{self.class} @id=#{id}>"
end

#select(*parts) ⇒ Yt::Resource

Specifies which parts of the resource to fetch when hitting the data API.

Parameters:

  • parts (Array<Symbol>)

    The parts to fetch.

Returns:



28
29
30
31
# File 'lib/yt/resource.rb', line 28

def select(*parts)
  @selected_data_parts = parts
  self
end