Skip to content

Commit 922c1f2

Browse files
committed
Add support for ingesting etag data from API endpoints
Most of the YouTube API endpoints that return resources expose an etag property. It is highly desireable for consumers to be able to have access to this property in order to perform conditional requests (using If-Match / If-None-Match) or to verify that resources have changed.
1 parent 16944ff commit 922c1f2

File tree

7 files changed

+25
-4
lines changed

7 files changed

+25
-4
lines changed

lib/yt/actions/list.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def resource_class
6363
# Can be overwritten by subclasses that initialize instance with
6464
# a different set of parameters.
6565
def new_item(data)
66-
resource_class.new attributes_for_new_item(data)
66+
resource_class.new attributes_for_new_item(data).merge(etag: data['etag'])
6767
end
6868

6969
# @private

lib/yt/collections/assets.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def insert(attributes = {})
1717

1818
def new_item(data)
1919
klass = (data["kind"] == "youtubePartner#assetSnippet") ? Yt::AssetSnippet : Yt::Asset
20-
klass.new attributes_for_new_item(data)
20+
klass.new attributes_for_new_item(data).merge(etag: data['etag'])
2121
end
2222

2323
# @return [Hash] the parameters to submit to YouTube to list assets

lib/yt/collections/resources.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def insert(attributes = {}, options = {}) #
1919
private
2020

2121
def attributes_for_new_item(data)
22-
{id: data['id'], snippet: data['snippet'], status: data['status'], auth: @auth}
22+
{id: data['id'], snippet: data['snippet'], status: data['status'], auth: @auth, etag: data['etag']}
2323
end
2424

2525
def resources_params

lib/yt/models/resource.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Yt
66
module Models
77
class Resource < Base
88
# @private
9-
attr_reader :auth
9+
attr_reader :auth, :etag
1010

1111
### ID ###
1212

@@ -56,6 +56,7 @@ def initialize(options = {})
5656
@id = options[:id]
5757
end
5858
@auth = options[:auth]
59+
@etag = options[:etag]
5960
@snippet = Snippet.new(data: options[:snippet]) if options[:snippet]
6061
@status = Status.new(data: options[:status]) if options[:status]
6162
end

spec/models/channel_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
describe Yt::Channel do
55
subject(:channel) { Yt::Channel.new attrs }
66

7+
describe '#etag' do
8+
context 'given the API response includes an etag' do
9+
let(:attrs) { { etag: '12345' } }
10+
it { expect(channel.etag).to eq '12345' }
11+
end
12+
end
13+
714
describe '#title' do
815
context 'given a snippet with a title' do
916
let(:attrs) { {snippet: {"title"=>"Fullscreen"}} }

spec/models/playlist_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
describe Yt::Playlist do
55
subject(:playlist) { Yt::Playlist.new attrs }
66

7+
describe '#etag' do
8+
context 'given the API response includes an etag' do
9+
let(:attrs) { { etag: '12345' } }
10+
it { expect(playlist.etag).to eq '12345' }
11+
end
12+
end
713

814
describe '#title' do
915
context 'given a snippet with a title' do

spec/models/video_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
describe Yt::Video do
55
subject(:video) { Yt::Video.new attrs }
66

7+
describe '#etag' do
8+
context 'given the API response includes an etag' do
9+
let(:attrs) { { etag: '12345' } }
10+
it { expect(video.etag).to eq '12345' }
11+
end
12+
end
13+
714
describe '#snippet' do
815
context 'given fetching a video returns a snippet' do
916
let(:attrs) { {snippet: {"title"=>"Fullscreen Creator Platform"}} }

0 commit comments

Comments
 (0)