Skip to content

Commit

Permalink
Merge pull request #220 from claudiofullscreen/playlist-item-include-…
Browse files Browse the repository at this point in the history
…video

Add .includes(:video) to .playlist_items
  • Loading branch information
claudiofullscreen committed Jul 23, 2015
2 parents 79ee8f7 + 5c15f49 commit 6b149e4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@ For more information about changelogs, check
[Keep a Changelog](http://keepachangelog.com) and
[Vandamme](http://tech-angels.github.io/vandamme).

## 0.25.2 - 2015-07-22

* [FEATURE] Add .includes(:video) to .playlist_items to eager-load video data of a list of playlist items.

## 0.25.1 - 2015-07-06

* [ENHANCEMENT] `Yt::Video.new` accepts embedded video url.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -41,7 +41,7 @@ To install on your system, run

To use inside a bundled Ruby project, add this line to the Gemfile:

gem 'yt', '~> 0.25.1'
gem 'yt', '~> 0.25.2'

Since the gem follows [Semantic Versioning](http://semver.org),
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
Expand Down
18 changes: 17 additions & 1 deletion lib/yt/collections/playlist_items.rb
Expand Up @@ -16,7 +16,23 @@ def insert(attributes = {}, options = {})
def attributes_for_new_item(data)
snippet = data.fetch 'snippet', {}
data['snippet'].reverse_merge! complete: snippet.key?('position')
super(data)
super(data).tap do |attributes|
attributes[:video] = data['video']
end
end

def eager_load_items_from(items)
if included_relationships.include?(:video)
video_ids = items.map{|item| item['snippet']['resourceId']['videoId']}.uniq
conditions = {id: video_ids.join(',')}
conditions[:part] = 'snippet,status,statistics'
videos = Collections::Videos.new(auth: @auth).where conditions
items.each do |item|
video = videos.find{|v| v.id == item['snippet']['resourceId']['videoId']}
item['video'] = video
end
end
super
end

# @return [Hash] the parameters to submit to YouTube to list playlist items.
Expand Down
7 changes: 7 additions & 0 deletions lib/yt/models/playlist_item.rb
Expand Up @@ -88,6 +88,13 @@ def exists?
!@id.nil?
end

# @private
# Override Resource's new to set video if the response includes it
def initialize(options = {})
super options
@video = options[:video] if options[:video]
end

private

def resource_id
Expand Down
2 changes: 1 addition & 1 deletion lib/yt/version.rb
@@ -1,3 +1,3 @@
module Yt
VERSION = '0.25.1'
VERSION = '0.25.2'
end
28 changes: 25 additions & 3 deletions spec/requests/as_account/playlist_spec.rb
Expand Up @@ -20,9 +20,31 @@
expect(playlist.privacy_status).to be_a String
end

it { expect(playlist.playlist_items.first).to be_a Yt::PlaylistItem }
it { expect(playlist.playlist_items.first.snippet).to be_complete }
it { expect(playlist.playlist_items.first.position).not_to be_nil }
describe '.playlist_items' do
let(:item) { playlist.playlist_items.first }

specify 'returns the playlist item with the complete snippet' do
expect(item).to be_a Yt::PlaylistItem
expect(item.snippet).to be_complete
expect(item.position).not_to be_nil
end

specify 'does not eager-load the attributes of the item’s video' do
expect(item.video.instance_variable_defined? :@snippet).to be false
expect(item.video.instance_variable_defined? :@status).to be false
expect(item.video.instance_variable_defined? :@statistics_set).to be false
end
end

describe '.playlist_items.includes(:video)' do
let(:item) { playlist.playlist_items.includes(:video).first }

specify 'eager-loads the snippet, status and statistics of each video' do
expect(item.video.instance_variable_defined? :@snippet).to be true
expect(item.video.instance_variable_defined? :@status).to be true
expect(item.video.instance_variable_defined? :@statistics_set).to be true
end
end
end

context 'given an unknown playlist' do
Expand Down

0 comments on commit 6b149e4

Please sign in to comment.