Class: Shrine::Storage::YouTube

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shrine/storage/you_tube.rb,
lib/shrine/storage/you_tube/version.rb

Defined Under Namespace

Classes: UserChannelNotFound, VideoNotFound

Constant Summary collapse

VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_storage:, client_id:, client_secret:, refresh_token:, channel_id: nil, default_privacy: :private, upload_options: {}, client_options: {}, request_options: {}) ⇒ YouTube

Returns a new instance of YouTube.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shrine/storage/you_tube.rb', line 17

def initialize(
  original_storage:,
  client_id:,
  client_secret:,
  refresh_token:,
  channel_id: nil,
  default_privacy: :private,
  upload_options: {},
  client_options: {},
  request_options: {}
)
  @youtube = youtube_service(
    client_id: client_id,
    client_secret: client_secret,
    refresh_token: refresh_token,
    client_options: client_options,
    request_options: request_options
  )

  @channel_id = channel_id
  @default_privacy = default_privacy
  @upload_options = upload_options
  @original_storage = original_storage
end

Instance Attribute Details

#default_privacyObject (readonly)

Returns the value of attribute default_privacy.



15
16
17
# File 'lib/shrine/storage/you_tube.rb', line 15

def default_privacy
  @default_privacy
end

#original_storageObject (readonly)

Returns the value of attribute original_storage.



15
16
17
# File 'lib/shrine/storage/you_tube.rb', line 15

def original_storage
  @original_storage
end

#upload_optionsObject (readonly)

Returns the value of attribute upload_options.



15
16
17
# File 'lib/shrine/storage/you_tube.rb', line 15

def upload_options
  @upload_options
end

#youtubeObject (readonly)

Returns the value of attribute youtube.



15
16
17
# File 'lib/shrine/storage/you_tube.rb', line 15

def youtube
  @youtube
end

Instance Method Details

#channel_idObject



45
46
47
# File 'lib/shrine/storage/you_tube.rb', line 45

def channel_id
  @channel_id ||= find_user_channel
end

#clear!Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/shrine/storage/you_tube.rb', line 87

def clear!
  while uploads_playlist_count > 0
    uploads_playlist_items.items.each do |item|
      video_id = item.snippet.resource_id.video_id
      youtube.delete_video(video_id)
    end
  end

  original_storage.clear!
end

#delete(id) ⇒ Object



68
69
70
71
72
# File 'lib/shrine/storage/you_tube.rb', line 68

def delete(id)
  return unless exists?(id)
  youtube.delete_video(id)
  original_storage.delete(id)
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/shrine/storage/you_tube.rb', line 64

def exists?(id)
  youtube.list_videos('id', id: id).page_info.total_results == 1
end

#update(id, metadata: {}) ⇒ Object

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/shrine/storage/you_tube.rb', line 98

def update(id, metadata: {})
  snippet = .delete('youtube')
  return unless snippet

  video_query = youtube.list_videos('snippet', id: id)
  raise VideoNotFound unless video_query.page_info.total_results == 1

  existing_snippet = video_query.items.first.snippet.to_h

  video_data = Google::Apis::YoutubeV3::Video.new(id: id, snippet: existing_snippet.merge(snippet))
  updated_video = youtube.update_video('snippet', video_data)
  updated_video.to_h
end

#upload(io, id, metadata = {}, **passed_upload_options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shrine/storage/you_tube.rb', line 49

def upload(io, id,  = {}, **passed_upload_options)
  snippet = { title: ['filename'], channel_id: channel_id }
  snippet.update(upload_options)
  snippet.update(passed_upload_options)
  snippet.update(.delete('youtube') || {})

  video_data = Google::Apis::YoutubeV3::Video.new(snippet: snippet)
  uploaded_video = upload_video(io, video_data)

  id.replace(uploaded_video.id)
  original_storage.upload(io, id, )

  uploaded_video.to_h
end

#url(id, type: nil, **options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/shrine/storage/you_tube.rb', line 74

def url(id, type: nil, **options)
  case type && type.to_sym
  when :original
    original_storage.url(id, **options)
  when :embed
    "https://youtube.com/embed/#{id}"
  when :short
    "https://youtu.be/#{id}"
  else
    "https://youtube.com/watch?v=#{id}"
  end
end