class Yt::Resource
Provides a base class for YouTube channels, videos, playlists and items. This is an abstract class and should not be instantiated directly.
Attributes
data[R]
@return [Hash] the resource’s data.
Public Class Methods
new(data = {})
click to toggle source
@param [Hash<Symbol, String>] data the options to initialize a resource. @option data [String] :id The unique ID of a YouTube resource.
# File lib/yt/resource.rb, line 7 def initialize(data = {}) @data = data @selected_data_parts = [] end
where(conditions = {})
click to toggle source
@return [Yt::Relation<Yt::Video>] the videos matching the conditions.
# File lib/yt/resource.rb, line 34 def self.where(conditions = {}) @where ||= Relation.new(self) do |options| slicing_conditions_every(50) do |slice_options| get resources_path, where_params(slice_options) end end @where.where conditions end
Private Class Methods
has_attribute(name, options = {}, &block)
click to toggle source
# File lib/yt/resource.rb, line 49 def self.has_attribute(name, options = {}, &block) define_method name do keys = Array(options[:in]) + [name] part = keys.shift value = @data[part] || get_part(part) keys.each{|key| value = value[camelize key]} if value.nil? && options[:default] value = options[:default] end value = type_cast value, options[:type] block_given? ? instance_exec(value, &block) : value end end
Public Instance Methods
id()
click to toggle source
@return [String] the resource’s unique ID.
# File lib/yt/resource.rb, line 13 def id @data[:id] end
inspect()
click to toggle source
@return [String] a representation of the resource instance.
# File lib/yt/resource.rb, line 21 def inspect "#<#{self.class} @id=#{id}>" end
select(*parts)
click to toggle source
Specifies which parts of the resource to fetch when hitting the data API. @param [Array<Symbol>] parts The parts to fetch. @return [Yt::Resource] itself.
# File lib/yt/resource.rb, line 28 def select(*parts) @selected_data_parts = parts self end
Private Instance Methods
camelize(part)
click to toggle source
# File lib/yt/resource.rb, line 45 def camelize(part) part.to_s.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" } end
get_part(required_part)
click to toggle source
# File lib/yt/resource.rb, line 63 def get_part(required_part) resources = Relation.new(self.class, part_params) do |options| get resources_path, resource_params(options) end parts = (@selected_data_parts + [required_part]).uniq if (resource = resources.select(*parts).first) parts.each{|part| @data[part] = resource.data[part]} @data[required_part] else raise NoItemsError end end
part_params()
click to toggle source
# File lib/yt/resource.rb, line 77 def part_params {ids: [id]} end
type_cast(value, type)
click to toggle source
# File lib/yt/resource.rb, line 81 def type_cast(value, type) case [type] when [Time] Time.parse value when [Integer] value.to_i when [Comment] Comment.new id: value['id'], snippet: value['snippet'] else value end end