class Yt::Models::ContentDetail

@private Encapsulates information about the video content, including the length of the video and an indication of whether captions are available. @see developers.google.com/youtube/v3/docs/videos#resource

Attributes

data[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/yt/models/content_detail.rb, line 12
def initialize(options = {})
  @data = options[:data]
end

Public Instance Methods

length() click to toggle source

@return [<String>] the length of the video as an ISO 8601 time, HH:MM:SS.

# File lib/yt/models/content_detail.rb, line 33
def length
  hh, mm, ss = duration / 3600, duration / 60 % 60, duration % 60
  [hh, mm, ss].map{|t| t.to_s.rjust(2,'0')}.join(':')
end
youtube_rating() click to toggle source
# File lib/yt/models/content_detail.rb, line 28
def youtube_rating
  content_rating['ytRating']
end

Private Instance Methods

to_seconds(iso8601_duration) click to toggle source

@return [Integer] the duration of the resource as reported by YouTube. @see developers.google.com/youtube/v3/docs/videos

According to YouTube documentation, the value is an ISO 8601 duration in the format PT#M#S, in which the letters PT indicate that the value specifies a period of time, and the letters M and S refer to length in minutes and seconds, respectively. The # characters preceding the M and S letters are both integers that specify the number of minutes (or seconds) of the video. For example, a value of PT15M51S indicates that the video is 15 minutes and 51 seconds long.

The ISO 8601 duration standard, though, is not always respected by the results returned by YouTube API. For instance: video 2XwmldWC_Ls reports a duration of 'P1W2DT6H21M32S', which is to be interpreted as 1 week, 2 days, 6 hours, 21 minutes, 32 seconds. Mixing weeks with other time units is not strictly part of ISO 8601; in this context, weeks will be interpreted as “the duration of 7 days”. Similarly, a day will be interpreted as “the duration of 24 hours”. Video tPEE9ZwTmy0 reports a duration of 'PT2S'. Skipping time units such as minutes is not part of the standard either; in this context, it will be interpreted as “0 minutes and 2 seconds”.

# File lib/yt/models/content_detail.rb, line 61
def to_seconds(iso8601_duration)
  match = iso8601_duration.match %r{^P(?:|(?<weeks>\d*?)W)(?:|(?<days>\d*?)D)(?:|T(?:|(?<hours>\d*?)H)(?:|(?<min>\d*?)M)(?:|(?<sec>\d*?)S))$}
  weeks = (match[:weeks] || '0').to_i
  days = (match[:days] || '0').to_i
  hours = (match[:hours] || '0').to_i
  minutes = (match[:min] || '0').to_i
  seconds = (match[:sec]).to_i
  (((((weeks * 7) + days) * 24 + hours) * 60) + minutes) * 60 + seconds
end