Class: SnapchatApi::Resources::Media

Inherits:
Base
  • Object
show all
Defined in:
lib/snapchat_api/resources/media.rb

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from SnapchatApi::Resources::Base

Instance Method Details

#create(ad_account_id:, params: {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/snapchat_api/resources/media.rb', line 28

def create(ad_account_id:, params: {})
  media_data = {
    media: [params]
  }

  response = client.request(:post, "adaccounts/#{}/media", media_data)
  response.body["media"].first["media"]
end

#get(media_id:) ⇒ Object



23
24
25
26
# File 'lib/snapchat_api/resources/media.rb', line 23

def get(media_id:)
  response = client.request(:get, "media/#{media_id}")
  response.body["media"].first["media"]
end

#list_all(ad_account_id:, params: {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/snapchat_api/resources/media.rb', line 7

def list_all(ad_account_id:, params: {})
  params[:limit] ||= 50

  media_items = []
  next_link = "adaccounts/#{}/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

#upload(media_id:, file_path:, params: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/snapchat_api/resources/media.rb', line 37

def upload(media_id:, file_path:, params: {})
  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