Class: Shrine::Storage::YouTube
- Inherits:
-
Object
- Object
- Shrine::Storage::YouTube
- 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
-
#default_privacy ⇒ Object
readonly
Returns the value of attribute default_privacy.
-
#original_storage ⇒ Object
readonly
Returns the value of attribute original_storage.
-
#upload_options ⇒ Object
readonly
Returns the value of attribute upload_options.
-
#youtube ⇒ Object
readonly
Returns the value of attribute youtube.
Instance Method Summary collapse
- #channel_id ⇒ Object
- #clear! ⇒ Object
- #delete(id) ⇒ Object
- #exists?(id) ⇒ Boolean
-
#initialize(original_storage:, client_id:, client_secret:, refresh_token:, channel_id: nil, default_privacy: :private, upload_options: {}, client_options: {}, request_options: {}) ⇒ YouTube
constructor
A new instance of YouTube.
- #update(id, metadata: {}) ⇒ Object
- #upload(io, id, metadata = {}, **passed_upload_options) ⇒ Object
- #url(id, type: nil, **options) ⇒ Object
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: , request_options: ) @channel_id = channel_id @default_privacy = default_privacy = @original_storage = original_storage end |
Instance Attribute Details
#default_privacy ⇒ Object (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_storage ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute upload_options.
15 16 17 |
# File 'lib/shrine/storage/you_tube.rb', line 15 def end |
#youtube ⇒ Object (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_id ⇒ Object
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
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
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, = {}, **) snippet = { title: ['filename'], channel_id: channel_id } snippet.update() snippet.update() 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, **) case type && type.to_sym when :original original_storage.url(id, **) when :embed "https://youtube.com/embed/#{id}" when :short "https://youtu.be/#{id}" else "https://youtube.com/watch?v=#{id}" end end |