Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/yt/collections/branding_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'yt/collections/base'
require 'yt/models/branding_setting'

module Yt
module Collections
# @private
class BrandingSettings < Base

private

def attributes_for_new_item(data)
{ data: data['brandingSettings'] }
end

# @return [Hash] the parameters to submit to YouTube to get the
# branding_settings of a resource, for instance a channel.
# @see https://developers.google.com/youtube/v3/docs/channels#resource
def list_params
super.tap do |params|
params[:path] = "/youtube/v3/#{@parent.kind.pluralize}"
params[:params] = branding_settings_params
end
end

def branding_settings_params
{ max_results: 50, part: 'brandingSettings', id: @parent.id }
end
end
end
end
45 changes: 45 additions & 0 deletions lib/yt/collections/channel_sections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'yt/collections/base'
require 'yt/models/channel_section'

module Yt
module Collections
# Provides methods for a collection of YouTube channel sections.
#
# Resources with channel_sections is {Yt::Models::Channel channels}.
class ChannelSections < Base

def attributes_for_new_item(data)
{}.tap do |attributes|
attributes[:id] = data['id']
attributes[:snippet] = data['snippet']
attributes[:content_details] = data['contentDetails']
end
end

# @return [Hash] the parameters to submit to YouTube to list channel sections.
# @see https://developers.google.com/youtube/v3/docs/channelSections/list
def list_params
super.tap do |params|
params[:params] = channel_sections_params
params[:path] = '/youtube/v3/channelSections'
end
end

def channel_sections_params
{}.tap do |params|
params[:part] = 'snippet'
params.merge! @parent.channel_sections_params if @parent
# TODO: when to mine, when to on_behalf_of_content_owner
# if @parent.auth
# if @parent.auth.owner_name
# params[:on_behalf_of_content_owner] = @parent.auth.owner_name
# else
# params[:mine] = true
# end
# end
params
end
end
end
end
end
29 changes: 29 additions & 0 deletions lib/yt/models/branding_setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'yt/models/base'

module Yt
module Models
# @private
# Encapsulates branding settings about the resource, such as trailer on channel
# @see https://developers.google.com/youtube/v3/docs/channels#resource-representation
class BrandingSetting < Base
attr_reader :data

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

has_attribute :channel, default: {}

def unsubscribed_trailer
channel['unsubscribedTrailer']
end

has_attribute :image, default: {}

def banner_external_url
image['bannerExternalUrl']
end
end
end
end

26 changes: 26 additions & 0 deletions lib/yt/models/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ def unsubscribe!
# explicitly select the option to keep all subscriptions private.
has_many :subscribed_channels

# @!attribute [r] channel_sections
# @return [Yt::Collections::ChannelSections] the channel’s channel sections.
has_many :channel_sections

### ANALYTICS ###

# @macro reports
Expand Down Expand Up @@ -232,6 +236,18 @@ def subscriber_count_visible?
statistics_set.hidden_subscriber_count == false
end

### BRANDING SETTINGS ###

has_one :branding_setting

# @!attribute [r] unsubscribed_trailer
# @return [String] the channel’s trailer video id.
delegate :unsubscribed_trailer, to: :branding_setting

# @!attribute [r] banner_external_url
# @return [String] the channel’s banner image URL.
delegate :banner_external_url, to: :branding_setting

### CONTENT OWNER DETAILS ###

has_one :content_owner_detail
Expand Down Expand Up @@ -289,6 +305,16 @@ def initialize(options = {})
if options[:content_owner_details]
@content_owner_detail = ContentOwnerDetail.new data: options[:content_owner_details]
end
if options[:branding_settings]
@branding_setting = BrandingSetting.new data: options[:branding_settings]
end
end

# @private
# Used for `has_many :channel_sections` to return all youtube#channelSection items
# of the channel.
def channel_sections_params
{channel_id: id}
end

# @private
Expand Down
25 changes: 25 additions & 0 deletions lib/yt/models/channel_section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'yt/models/base'

module Yt
module Models
class ChannelSection < Base
attr_reader :data

# @private
def initialize(options = {})
@id = options[:id]
@data = options[:snippet]
end

has_attribute :type
has_attribute :channel_id
has_attribute :position, type: Integer

### ID ###

# @!attribute [r] id
# @return [String] the ID that YouTube uses to identify each resource.
attr_reader :id
end
end
end
2 changes: 1 addition & 1 deletion lib/yt/models/group_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def initialize(options = {})
has_attribute :published_at, type: Time
end
end
end
end
13 changes: 13 additions & 0 deletions spec/models/channel_section_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'
require 'yt/models/channel_section'

describe Yt::ChannelSection do
subject(:channel_section) { Yt::ChannelSection.new attrs }

describe '#position' do
context 'given fetching a channel_section returns a snippet' do
let(:attrs) { {snippet: {"title"=>"New Section", "position"=>0}} }
it { expect(channel_section.position).to eq 0 }
end
end
end
19 changes: 19 additions & 0 deletions spec/models/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,23 @@
it { expect(channel.status).to be_a Yt::Status }
end
end

describe '#branding_setting' do
context 'given fetching a channel returns a branding_setting' do
let(:attrs) { {branding_settings: {"channel"=>{"unsubscribedTrailer"=>"abcdefghijk"}}} }
it { expect(channel.branding_setting).to be_a Yt::BrandingSetting }
end
end

describe '#unsubscribed_trailer' do
context 'given a branding_settings with a unsubscribed trailer' do
let(:attrs) { {branding_settings: {"channel"=>{"unsubscribedTrailer"=>"abcdefghijk"}}} }
it { expect(channel.unsubscribed_trailer).to eq 'abcdefghijk' }
end

context 'given a branding_settings without a unsubscribed trailer' do
let(:attrs) { {branding_settings: {"channel"=>{}}} }
it { expect(channel.unsubscribed_trailer).to be_nil }
end
end
end