Skip to content

Commit

Permalink
@wip Adding snippet to subscription
Browse files Browse the repository at this point in the history
Code needs to be cleaned because:

- channelId refers to the subscriber
- YouTube says channelTitle is returned but it's always nil
- might or might not be a good idea to inherit Subscription from Resource
  • Loading branch information
claudiob committed Jul 7, 2014
1 parent 61daa67 commit a25424a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/yt/collections/subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def delete_all(params = {}, options = {})
# subscriptions to a channel.
# @see https://developers.google.com/youtube/v3/docs/subscriptions#resource
def new_item(data)
Yt::Subscription.new id: data['id'], auth: @auth
Yt::Subscription.new id: data['id'], snippet: data['snippet'], auth: @auth
end

# @note Google API must have some caching layer by which if we try to
Expand Down
4 changes: 2 additions & 2 deletions lib/yt/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def username

private

# @return [Hash] the parameters to submit to YouTube to update a playlist.
# @return [Hash] the parameters to submit to YouTube to update a resource.
# @see https://developers.google.com/youtube/v3/docs/playlists/update
# @see https://developers.google.com/youtube/v3/docs/videos/update
def update_params
Expand All @@ -42,7 +42,7 @@ def update_params
end
end

# @return [Hash] the parameters to submit to YouTube to delete a playlist.
# @return [Hash] the parameters to submit to YouTube to delete a resource.
# @see https://developers.google.com/youtube/v3/docs/playlists/delete
def delete_params
super.tap{|params| params[:params] = {id: @id}}
Expand Down
10 changes: 9 additions & 1 deletion lib/yt/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ module Models
# @see https://developers.google.com/youtube/v3/docs/subscriptions
class Subscription < Base
# @return [String] the ID that uniquely identify a YouTube subscription.
attr_reader :id
attr_reader :id, :auth

has_one :snippet
# @note: Snippet also includes channel_id and channel_title, but it may
# be confusing to expose them, since they refer to the channel that
# created the subscription, not the channel it subscribed to.
delegate :title, :description, :thumbnail_url, :published_at,
to: :snippet

def initialize(options = {})
@id = options[:id]
@auth = options[:auth]
@snippet = Snippet.new(data: options[:snippet]) if options[:snippet]
end

def delete(options = {})
Expand Down
7 changes: 7 additions & 0 deletions spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
subject(:subscription) { Yt::Subscription.new id: id }
let(:msg) { {response_body: {error: {errors: [{reason: reason}]}}}.to_json }

describe '#snippet' do
context 'given fetching a subscription returns a snippet' do
let(:attrs) { {snippet: {"title"=>"Fullscreen"}} }
it { expect(subscription.snippet).to be_a Yt::Snippet }
end
end

describe '#exists?' do
context 'given a subscription with an id' do
let(:id) { 'CBl6OoF0BpiV' }
Expand Down
20 changes: 20 additions & 0 deletions spec/requests/as_account/subscription_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# encoding: UTF-8

require 'spec_helper'
require 'yt/models/subscription'

describe Yt::Subscription, :device_app do
subject(:subscription) { Yt::Subscription.new id: id, auth: $account }

context 'given one of my subscriptions' do
let(:id) { '5GRxNxk3IHLSRtWOUojJ3SwPvDooXERDBk1D2gNfcQU' } # TODO !!!! MAKE AN ENV !!!!!

it 'returns valid snippet data' do
expect(subscription.snippet).to be_a Yt::Snippet
expect(subscription.title).to be_a String
expect(subscription.description).to be_a Yt::Description
expect(subscription.thumbnail_url).to be_a String
expect(subscription.published_at).to be_a Time
end
end
end

0 comments on commit a25424a

Please sign in to comment.