Class: SnapchatApi::Resources::Media
- Defined in:
- lib/snapchat_api/resources/media.rb
Constant Summary collapse
- CHUNKED_UPLOAD_THRESHOLD =
32_000_000- CHUNK_SIZE =
25 * 1024 * 1024
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #create(ad_account_id:, params: {}) ⇒ Object
- #get(media_id:) ⇒ Object
- #list_all(ad_account_id:, params: {}) ⇒ Object
- #preview(media_id:) ⇒ Object
- #thumbnail(media_id:) ⇒ Object
- #upload(media_id:, file_path:, params: {}) ⇒ Object
- #upload_chunked(media_id:, file_path:, params: {}) ⇒ Object
Methods inherited from Base
Constructor Details
This class inherits a constructor from SnapchatApi::Resources::Base
Instance Method Details
#create(ad_account_id:, params: {}) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/snapchat_api/resources/media.rb', line 31 def create(ad_account_id:, params: {}) media_data = { media: [params] } response = client.request(:post, "adaccounts/#{ad_account_id}/media", media_data) response.body["media"].first["media"] end |
#get(media_id:) ⇒ Object
26 27 28 29 |
# File 'lib/snapchat_api/resources/media.rb', line 26 def get(media_id:) response = client.request(:get, "media/#{media_id}") response.body["media"].first["media"] end |
#list_all(ad_account_id:, params: {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/snapchat_api/resources/media.rb', line 10 def list_all(ad_account_id:, params: {}) params[:limit] ||= 50 media_items = [] next_link = "adaccounts/#{ad_account_id}/media?limit=#{params[:limit]}" loop do response = client.request(:get, next_link) next_link = response.body["paging"]["next_link"] media_items.concat(response.body["media"].map { |el| el["media"] }) break if next_link.nil? end media_items end |
#preview(media_id:) ⇒ Object
99 100 101 102 |
# File 'lib/snapchat_api/resources/media.rb', line 99 def preview(media_id:) response = client.request(:get, "media/#{media_id}/preview") response.body end |
#thumbnail(media_id:) ⇒ Object
104 105 106 107 |
# File 'lib/snapchat_api/resources/media.rb', line 104 def thumbnail(media_id:) response = client.request(:get, "media/#{media_id}/thumbnail") response.body end |
#upload(media_id:, file_path:, params: {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/snapchat_api/resources/media.rb', line 40 def upload(media_id:, file_path:, params: {}) return upload_chunked(media_id: media_id, file_path: file_path, params: params) if File.size(file_path) > CHUNKED_UPLOAD_THRESHOLD mime_type = MIME::Types.type_for(file_path).first.content_type upload_params = { file: Faraday::UploadIO.new(file_path, mime_type, File.basename(file_path)) } response = client.request(:post, "media/#{media_id}/upload", upload_params, {"Content-Type" => "multipart/form-data"}) response.body["result"] end |
#upload_chunked(media_id:, file_path:, params: {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/snapchat_api/resources/media.rb', line 53 def upload_chunked(media_id:, file_path:, params: {}) mime_type = MIME::Types.type_for(file_path).first.content_type file_name = File.basename(file_path) file_size = File.size(file_path) number_of_parts = (file_size.to_f / CHUNK_SIZE).ceil init_response = client.request( :post, "media/#{media_id}/multipart-upload-v2?action=INIT", { file_name: file_name, file_size: file_size.to_s, number_of_parts: number_of_parts.to_s }, {"Content-Type" => "multipart/form-data"} ) upload_id = init_response.body["upload_id"] add_path = init_response.body["add_path"] finalize_path = init_response.body["finalize_path"] File.open(file_path, "rb") do |file| part_number = 1 while (chunk = file.read(CHUNK_SIZE)) client.request( :post, add_path, { file: Faraday::UploadIO.new(StringIO.new(chunk), mime_type, file_name), part_number: part_number.to_s, upload_id: upload_id }, {"Content-Type" => "multipart/form-data"} ) part_number += 1 end end response = client.request( :post, finalize_path, {upload_id: upload_id, file_name: file_name}, {"Content-Type" => "multipart/form-data"} ) response.body["result"] end |