-
Notifications
You must be signed in to change notification settings - Fork 162
Support for Live Broadcasts #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| require 'yt/collections/base' | ||
| require 'yt/models/video' | ||
|
|
||
| module Yt | ||
| module Collections | ||
| # Provides methods to interact with a collection of YouTube live broadcasts. | ||
| # | ||
| # Resources with live broadcasts are: {Yt::Models::Channel channels} and | ||
| # {Yt::Models::Account accounts}. | ||
| class LiveBroadcasts < Resources | ||
|
|
||
| private | ||
|
|
||
| # @return [Hash] the parameters to submit to YouTube to list live broadcasts. | ||
| # @see https://developers.google.com/youtube/v3/docs/liveBroadcasts/list | ||
| def list_params | ||
| super.tap{|params| params[:params] = live_broadcasts_params} | ||
| end | ||
|
|
||
| def live_broadcasts_params | ||
| if @where_params.blank? | ||
| {broadcastType: "all", mine: true} | ||
| else | ||
| apply_where_params! on_behalf_of_content_owner: @parent.owner_name | ||
| end | ||
| end | ||
|
|
||
| def attributes_for_new_item(data) | ||
| {}.tap do |attributes| | ||
| attributes[:id] = data['id'] | ||
| attributes[:snippet] = data['snippet'] | ||
| attributes[:status] = data['status'] | ||
| attributes[:content_details] = data['contentDetails'] | ||
| end | ||
| end | ||
|
|
||
| def insert_parts | ||
| snippet = {keys: [:title, :description, :scheduled_start_time, :scheduled_end_time, :default_language], sanitize_brackets: true} | ||
| status = {keys: [:privacy_status, :self_declared_made_for_kids]} | ||
| {snippet: snippet, status: status} | ||
| end | ||
| end | ||
|
|
||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| require 'yt/models/base' | ||
|
|
||
| module Yt | ||
| module Models | ||
| # Provides methods to interact with YouTube Content ID live broadcasts. | ||
| # @see https://developers.google.com/youtube/v3/live/docs/liveBroadcasts | ||
| class LiveBroadcast < Resource | ||
|
|
||
| ### SNIPPET ### | ||
|
|
||
| # @!attribute [r] title | ||
| # @return [String] the broadcast’s title. | ||
| delegate :title, to: :snippet | ||
|
|
||
| # @!attribute [r] description | ||
| # @return [String] the broadcast’s description. | ||
| delegate :description, to: :snippet | ||
|
|
||
| # @!attribute [r] scheduledStartTime | ||
| # @return [Time] the broadcast’s scheduled start time. | ||
| delegate :scheduled_start_time, to: :snippet | ||
|
|
||
| # @!attribute [r] scheduledEndTime | ||
| # @return [Time] the broadcast’s scheduled end time. | ||
| delegate :scheduled_end_time, to: :snippet | ||
|
|
||
| # @!attribute [r] published_at | ||
| # @return [Time] the date and time that the broadcast was published. | ||
| delegate :published_at, to: :snippet | ||
|
|
||
| # @return [String] the ID that YouTube assigns and uses to uniquely | ||
| # identify the live_broadcast. | ||
| has_attribute :id | ||
|
|
||
| ### PRIVATE API ### | ||
|
|
||
| # @see https://developers.google.com/youtube/v3/docs/liveBroadcasts/update | ||
| def update_parts | ||
| snippet_keys = [:title, :description, :scheduled_start_time, :scheduled_end_time] | ||
| snippet = {keys: snippet_keys, sanitize_brackets: true} | ||
| status_keys = [:privacy_status, :recording_status, | ||
| :publish_at, :self_declared_made_for_kids] | ||
| {snippet: snippet, status: {keys: status_keys}} | ||
| end | ||
|
|
||
| ### ACTIONS (UPLOAD, UPDATE, DELETE) ### | ||
|
|
||
| # Deletes the live broadcast. | ||
| # @return [Boolean] whether the live broadcast does not exist anymore. | ||
| # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an | ||
| # authenticated Yt::Account with permissions to delete the live broadcast. | ||
| def delete(options = {}) | ||
| do_delete {@id = nil} | ||
| !exists? | ||
| end | ||
|
|
||
| # Updates the attributes of a broadcast. | ||
| # @return [Boolean] whether the broadcast was successfully updated. | ||
| # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an | ||
| # authenticated Yt::Account with permissions to update the broadcast. | ||
| # @param [Hash] attributes the attributes to update. | ||
| # @option attributes [String] :title The new broadcast’s title. | ||
| # Cannot have more than 100 characters. Can include the characters | ||
| # < and >, which are replaced to ‹ › in order to be accepted by YouTube. | ||
| # @option attributes [String] :description The new broadcast’s description. | ||
| # Cannot have more than 5000 bytes. Can include the characters | ||
| # < and >, which are replaced to ‹ › in order to be accepted by YouTube. | ||
| # @option attributes [String] :privacy_status The new broadcast’s privacy | ||
| # status. Must be one of: private, unscheduled, public. | ||
| # @example Update title and description of a broadcast. | ||
| # broadcast.update title: 'New title', description: 'New description' | ||
| # @example Update status of a broadcast. | ||
| # broadcast.update privacy_status: 'public' | ||
| def update(attributes = {}) | ||
| super | ||
| end | ||
|
|
||
| # @private | ||
| def exists? | ||
| !@id.nil? | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| require 'spec_helper' | ||
| require 'yt/collections/live_broadcasts' | ||
| require 'yt/models/live_broadcast' | ||
|
|
||
| describe Yt::Collections::LiveBroadcasts do | ||
| subject(:collection) { Yt::Collections::LiveBroadcasts.new } | ||
| before { expect(collection).to behave } | ||
|
|
||
| describe '#insert' do | ||
| let(:live_broadcast) { Yt::LiveBroadcast.new } | ||
| # TODO: separate stubs to show options translate into do_insert params | ||
| let(:behave) { receive(:do_insert).and_return live_broadcast } | ||
|
|
||
| it { expect(collection.insert).to eq live_broadcast } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| require 'spec_helper' | ||
| require 'yt/models/live_broadcast' | ||
|
|
||
| describe Yt::LiveBroadcast do | ||
| subject(:broadcast) { Yt::LiveBroadcast.new attrs } | ||
|
|
||
|
|
||
| describe '#title' do | ||
| context 'given a snippet with a title' do | ||
| let(:attrs) { {snippet: {"title"=>"Fullscreen"}} } | ||
| it { expect(broadcast.title).to eq 'Fullscreen' } | ||
| end | ||
|
|
||
| context 'given a snippet without a title' do | ||
| let(:attrs) { {snippet: {}} } | ||
| it { expect(broadcast.title).to eq '' } | ||
| end | ||
| end | ||
|
|
||
| describe '#published_at' do | ||
| context 'given a snippet with a timestamp' do # always returned by YouTube | ||
| let(:attrs) { {snippet: {"publishedAt"=>"2014-04-22T19:14:49.000Z"}} } | ||
| it { expect(broadcast.published_at.year).to be 2014 } | ||
| end | ||
| end | ||
|
|
||
| describe '#description' do | ||
| context 'given a snippet with a description' do | ||
| let(:attrs) { {snippet: {"description"=>"The first media company for the connected generation."}} } | ||
| it { expect(broadcast.description).to eq 'The first media company for the connected generation.' } | ||
| end | ||
|
|
||
| context 'given a snippet without a description' do | ||
| let(:attrs) { {snippet: {}} } | ||
| it { expect(broadcast.description).to eq '' } | ||
| end | ||
| end | ||
|
|
||
| describe '#exists?' do | ||
| context 'given a broadcast with an id' do | ||
| let(:attrs) { {id: 'PLSWYkYzOr'} } | ||
| it { expect(broadcast).to exist } | ||
| end | ||
|
|
||
| context 'given a broadcast without an id' do | ||
| let(:attrs) { {} } | ||
| it { expect(broadcast).not_to exist } | ||
| end | ||
| end | ||
|
|
||
| describe '#snippet' do | ||
| context 'given fetching a broadcast returns a snippet' do | ||
| let(:attrs) { {snippet: {"title"=>"Fullscreen"}} } | ||
| it { expect(broadcast.snippet).to be_a Yt::Snippet } | ||
| end | ||
| end | ||
|
|
||
| describe '#status' do | ||
| context 'given fetching a broadcast returns a status' do | ||
| let(:attrs) { {status: {"privacyStatus"=>"public"}} } | ||
| it { expect(broadcast.status).to be_a Yt::Status } | ||
| end | ||
| end | ||
|
|
||
| describe '#update' do | ||
| let(:attrs) { {id: 'PLSWYkYzOr', snippet: {'title'=>'old'}, status: {"privacyStatus"=>"public"}} } | ||
| before { expect(broadcast).to receive(:do_update).and_yield 'snippet'=>{'title'=>'new'} } | ||
|
|
||
| it { expect(broadcast.update title: 'new').to be true } | ||
| it { expect{broadcast.update title: 'new'}.to change{broadcast.title} } | ||
| end | ||
|
|
||
| describe '#delete' do | ||
| let(:attrs) { {id: 'PLSWYkYzOr'} } | ||
|
|
||
| context 'given an existing broadcast' do | ||
| before { expect(broadcast).to receive(:do_delete).and_yield } | ||
|
|
||
| it { expect(broadcast.delete).to be true } | ||
| it { expect{broadcast.delete}.to change{broadcast.exists?} } | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you might have wanted to use
insert_partsininsertmethod in this class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must admit that I did not understand how this worked in other classes.
I looked at other collections and they seemed to use slightly different patterns.
Could you point me to a class that is a good example that I could study?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. @gmarziou
I was wondering if this part could be used and simplify
insertmethod above, how do you think? If you don't mind, could you please try something like following code, for me to learn more?