Skip to content

Commit

Permalink
test out #include instance method on collections
Browse files Browse the repository at this point in the history
  • Loading branch information
stavro committed Jul 24, 2014
1 parent dfa5c2f commit 97a9e9b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/yt/collections/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Base
def initialize(options = {})
@parent = options[:parent]
@auth = options[:auth]
@extra_params = {}
@extra_parts = []
end

def self.of(parent)
Expand All @@ -24,6 +26,12 @@ def where(conditions = {})
@extra_params = conditions
self
end

def include(*parts)
@items = []
@extra_parts = parts
self
end
end
end
end
2 changes: 1 addition & 1 deletion lib/yt/collections/claims.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def list_params

def claims_params
{onBehalfOfContentOwner: @parent.owner_name}.tap do |params|
(@extra_params ||= {}).each do |key, value|
@extra_params.each do |key, value|
params[key.to_s.camelize :lower] = value
end
end
Expand Down
20 changes: 16 additions & 4 deletions lib/yt/collections/videos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ class Videos < Base
# the items returned by asking YouTube for a list of videos.
# @see https://developers.google.com/youtube/v3/docs/videos#resource
def new_item(data)
Yt::Video.new id: data['id']['videoId'], snippet: data['snippet'], auth: @auth
puts data.inspect
Yt::Video.new id: data['id']['videoId'], snippet: data['snippet'], statistics: data['statistics'], auth: @auth
end

# @return [Hash] the parameters to submit to YouTube to list videos.
# @see https://developers.google.com/youtube/v3/docs/search/list
def list_params
super.tap do |params|
params[:params] = @parent.videos_params.merge videos_params
params[:path] = '/youtube/v3/search'
params[:path] = videos_path
end
end

Expand All @@ -43,11 +44,22 @@ def add_offset_to(items)
end

def videos_params
params = {type: :video, maxResults: 50, part: 'snippet', order: 'date'}
params = {type: :video, maxResults: 50, part: videos_parts, order: 'date'}
params[:publishedBefore] = @published_before if @published_before
@extra_params ||= {}
params.merge @extra_params
end

def videos_path
if @extra_params.empty? || @extra_params.key?(:id)
'/youtube/v3/videos'
else
'/youtube/v3/search'
end
end

def videos_parts
[:snippet].concat(@extra_parts).uniq.join(',')
end
end
end
end
1 change: 1 addition & 0 deletions lib/yt/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize(options = {})
@auth = options[:auth]
@snippet = Snippet.new(data: options[:snippet]) if options[:snippet]
@status = Status.new(data: options[:status]) if options[:status]
@statistics_set = StatisticsSet.new(data: options[:statistics]) if options[:statistics]
end

def kind
Expand Down

0 comments on commit 97a9e9b

Please sign in to comment.