Module: Googol::Subscriptions

Included in:
YoutubeAccount
Defined in:
lib/googol/youtube_account/subscriptions.rb

Overview

Separate module to group YoutubeAccount methods related to subscriptions.

Instance Method Summary collapse

Instance Method Details

#subscribe_to(channel = {}) ⇒ String

Subscribe a Youtube account to a channel. If the account is already subscribed, return the existing channel subscription ID.

Parameters:

  • channel (Hash) (defaults to: {})

    The channel for the ‘subscribe’ activity

Options Hash (channel):

  • :channel_id (String)

    The ID of the channel to subscribe to

Returns:

  • (String)

    The ID of the new or existing channel subscription

Raises:

  • (Googol::RequestError)

    if the subscription does not go through, unless the error is that the user is already subscribed to the channel.

See Also:



33
34
35
36
37
38
39
40
41
# File 'lib/googol/youtube_account/subscriptions.rb', line 33

def subscribe_to(channel = {})
  subscribe_to! channel
rescue Googol::RequestError => e
  if e.to_s =~ /subscriptionDuplicate/
    find_subscription_by channel
  else
    raise e
  end
end

#subscribe_to!(channel = {}) ⇒ String

Subscribe a Youtube account to a channel. If the account is already subscribed, raise an error.

Parameters:

  • channel (Hash) (defaults to: {})

    The channel for the ‘subscribe’ activity

Options Hash (channel):

  • :channel_id (String)

    The ID of the channel to subscribe to

Returns:

  • (String)

    The ID of the new channel subscription

See Also:



14
15
16
17
18
# File 'lib/googol/youtube_account/subscriptions.rb', line 14

def subscribe_to!(channel = {})
  channel_id = fetch! channel, :channel_id
  youtube_request! path: '/subscriptions?part=snippet', json: true,
    method: :post, body: {snippet: {resourceId: {channelId: channel_id}}}
end

#unsubscribe_from(channel = {}) ⇒ Object

Unsubscribe a Youtube account from a channel. If the account is not subscribed, ignore the request.

Parameters:

  • channel (Hash) (defaults to: {})

    The channel for the ‘subscribe’ activity

Options Hash (channel):

  • :channel_id (String)

    The ID of the channel to unsubscribe from

Raises:

  • (Googol::RequestError)

    if the unsubscription does not go through, unless the error is that the user was not subscribed to the channel.

See Also:



68
69
70
71
72
73
74
75
76
# File 'lib/googol/youtube_account/subscriptions.rb', line 68

def unsubscribe_from(channel = {})
  unsubscribe_from! channel
rescue Googol::RequestError => e
  if e.to_s =~ /subscriptionNotFound/
    true
  else
    raise e
  end
end

#unsubscribe_from!(channel = {}) ⇒ Object

Unsubscribe a Youtube account from a channel. If the account is not subscribed, raise an error.

Parameters:

  • channel (Hash) (defaults to: {})

    The channel for the ‘subscribe’ activity

Options Hash (channel):

  • :channel_id (String)

    The ID of the channel to unsubscribe from

Raises:

See Also:



53
54
55
# File 'lib/googol/youtube_account/subscriptions.rb', line 53

def unsubscribe_from!(channel = {})
  delete_subscription!(find_subscription_by channel)
end