Skip to content

Commit b93bef2

Browse files
authored
Add channel_sections of channel (#452)
* Add branding_setting.unsubscribed_trailer * Add channel_sections under channel * Add model spec for branding_setting * Add test for channel_section model
1 parent 38549ee commit b93bef2

File tree

8 files changed

+188
-1
lines changed

8 files changed

+188
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'yt/collections/base'
2+
require 'yt/models/branding_setting'
3+
4+
module Yt
5+
module Collections
6+
# @private
7+
class BrandingSettings < Base
8+
9+
private
10+
11+
def attributes_for_new_item(data)
12+
{ data: data['brandingSettings'] }
13+
end
14+
15+
# @return [Hash] the parameters to submit to YouTube to get the
16+
# branding_settings of a resource, for instance a channel.
17+
# @see https://developers.google.com/youtube/v3/docs/channels#resource
18+
def list_params
19+
super.tap do |params|
20+
params[:path] = "/youtube/v3/#{@parent.kind.pluralize}"
21+
params[:params] = branding_settings_params
22+
end
23+
end
24+
25+
def branding_settings_params
26+
{ max_results: 50, part: 'brandingSettings', id: @parent.id }
27+
end
28+
end
29+
end
30+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'yt/collections/base'
2+
require 'yt/models/channel_section'
3+
4+
module Yt
5+
module Collections
6+
# Provides methods for a collection of YouTube channel sections.
7+
#
8+
# Resources with channel_sections is {Yt::Models::Channel channels}.
9+
class ChannelSections < Base
10+
11+
def attributes_for_new_item(data)
12+
{}.tap do |attributes|
13+
attributes[:id] = data['id']
14+
attributes[:snippet] = data['snippet']
15+
attributes[:content_details] = data['contentDetails']
16+
end
17+
end
18+
19+
# @return [Hash] the parameters to submit to YouTube to list channel sections.
20+
# @see https://developers.google.com/youtube/v3/docs/channelSections/list
21+
def list_params
22+
super.tap do |params|
23+
params[:params] = channel_sections_params
24+
params[:path] = '/youtube/v3/channelSections'
25+
end
26+
end
27+
28+
def channel_sections_params
29+
{}.tap do |params|
30+
params[:part] = 'snippet'
31+
params.merge! @parent.channel_sections_params if @parent
32+
# TODO: when to mine, when to on_behalf_of_content_owner
33+
# if @parent.auth
34+
# if @parent.auth.owner_name
35+
# params[:on_behalf_of_content_owner] = @parent.auth.owner_name
36+
# else
37+
# params[:mine] = true
38+
# end
39+
# end
40+
params
41+
end
42+
end
43+
end
44+
end
45+
end

lib/yt/models/branding_setting.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'yt/models/base'
2+
3+
module Yt
4+
module Models
5+
# @private
6+
# Encapsulates branding settings about the resource, such as trailer on channel
7+
# @see https://developers.google.com/youtube/v3/docs/channels#resource-representation
8+
class BrandingSetting < Base
9+
attr_reader :data
10+
11+
def initialize(options = {})
12+
@data = options[:data]
13+
end
14+
15+
has_attribute :channel, default: {}
16+
17+
def unsubscribed_trailer
18+
channel['unsubscribedTrailer']
19+
end
20+
21+
has_attribute :image, default: {}
22+
23+
def banner_external_url
24+
image['bannerExternalUrl']
25+
end
26+
end
27+
end
28+
end
29+

lib/yt/models/channel.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ def unsubscribe!
126126
# explicitly select the option to keep all subscriptions private.
127127
has_many :subscribed_channels
128128

129+
# @!attribute [r] channel_sections
130+
# @return [Yt::Collections::ChannelSections] the channel’s channel sections.
131+
has_many :channel_sections
132+
129133
### ANALYTICS ###
130134

131135
# @macro reports
@@ -232,6 +236,18 @@ def subscriber_count_visible?
232236
statistics_set.hidden_subscriber_count == false
233237
end
234238

239+
### BRANDING SETTINGS ###
240+
241+
has_one :branding_setting
242+
243+
# @!attribute [r] unsubscribed_trailer
244+
# @return [String] the channel’s trailer video id.
245+
delegate :unsubscribed_trailer, to: :branding_setting
246+
247+
# @!attribute [r] banner_external_url
248+
# @return [String] the channel’s banner image URL.
249+
delegate :banner_external_url, to: :branding_setting
250+
235251
### CONTENT OWNER DETAILS ###
236252

237253
has_one :content_owner_detail
@@ -289,6 +305,16 @@ def initialize(options = {})
289305
if options[:content_owner_details]
290306
@content_owner_detail = ContentOwnerDetail.new data: options[:content_owner_details]
291307
end
308+
if options[:branding_settings]
309+
@branding_setting = BrandingSetting.new data: options[:branding_settings]
310+
end
311+
end
312+
313+
# @private
314+
# Used for `has_many :channel_sections` to return all youtube#channelSection items
315+
# of the channel.
316+
def channel_sections_params
317+
{channel_id: id}
292318
end
293319

294320
# @private

lib/yt/models/channel_section.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'yt/models/base'
2+
3+
module Yt
4+
module Models
5+
class ChannelSection < Base
6+
attr_reader :data
7+
8+
# @private
9+
def initialize(options = {})
10+
@id = options[:id]
11+
@data = options[:snippet]
12+
end
13+
14+
has_attribute :type
15+
has_attribute :channel_id
16+
has_attribute :position, type: Integer
17+
18+
### ID ###
19+
20+
# @!attribute [r] id
21+
# @return [String] the ID that YouTube uses to identify each resource.
22+
attr_reader :id
23+
end
24+
end
25+
end

lib/yt/models/group_info.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ def initialize(options = {})
1313
has_attribute :published_at, type: Time
1414
end
1515
end
16-
end
16+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'spec_helper'
2+
require 'yt/models/channel_section'
3+
4+
describe Yt::ChannelSection do
5+
subject(:channel_section) { Yt::ChannelSection.new attrs }
6+
7+
describe '#position' do
8+
context 'given fetching a channel_section returns a snippet' do
9+
let(:attrs) { {snippet: {"title"=>"New Section", "position"=>0}} }
10+
it { expect(channel_section.position).to eq 0 }
11+
end
12+
end
13+
end

spec/models/channel_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,23 @@
141141
it { expect(channel.status).to be_a Yt::Status }
142142
end
143143
end
144+
145+
describe '#branding_setting' do
146+
context 'given fetching a channel returns a branding_setting' do
147+
let(:attrs) { {branding_settings: {"channel"=>{"unsubscribedTrailer"=>"abcdefghijk"}}} }
148+
it { expect(channel.branding_setting).to be_a Yt::BrandingSetting }
149+
end
150+
end
151+
152+
describe '#unsubscribed_trailer' do
153+
context 'given a branding_settings with a unsubscribed trailer' do
154+
let(:attrs) { {branding_settings: {"channel"=>{"unsubscribedTrailer"=>"abcdefghijk"}}} }
155+
it { expect(channel.unsubscribed_trailer).to eq 'abcdefghijk' }
156+
end
157+
158+
context 'given a branding_settings without a unsubscribed trailer' do
159+
let(:attrs) { {branding_settings: {"channel"=>{}}} }
160+
it { expect(channel.unsubscribed_trailer).to be_nil }
161+
end
162+
end
144163
end

0 commit comments

Comments
 (0)