Class: KapsoClientRuby::Resources::Media
- Inherits:
-
Object
- Object
- KapsoClientRuby::Resources::Media
- Defined in:
- lib/kapso_client_ruby/resources/media.rb
Instance Method Summary collapse
-
#delete(media_id:, phone_number_id: nil) ⇒ Object
Delete media.
-
#download(media_id:, phone_number_id: nil, headers: {}, auth: :auto, as: :binary) ⇒ Object
Download media content.
-
#get(media_id:, phone_number_id: nil) ⇒ Object
Get media metadata.
-
#info(media_id:, phone_number_id: nil) ⇒ Object
Get media info including size, type, and download URL.
-
#initialize(client) ⇒ Media
constructor
A new instance of Media.
-
#save_to_file(media_id:, filepath:, phone_number_id: nil, headers: {}, auth: :auto) ⇒ Object
Save media to file.
-
#upload(phone_number_id:, type:, file:, filename: nil, messaging_product: 'whatsapp', upload_strategy: nil) ⇒ Object
Upload media file.
Constructor Details
#initialize(client) ⇒ Media
Returns a new instance of Media.
8 9 10 |
# File 'lib/kapso_client_ruby/resources/media.rb', line 8 def initialize(client) @client = client end |
Instance Method Details
#delete(media_id:, phone_number_id: nil) ⇒ Object
Delete media
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kapso_client_ruby/resources/media.rb', line 72 def delete(media_id:, phone_number_id: nil) # phone_number_id is required for Kapso proxy if @client.kapso_proxy? && phone_number_id.nil? raise ArgumentError, 'phone_number_id is required when using Kapso proxy' end query_params = {} query_params[:phone_number_id] = phone_number_id if phone_number_id response = @client.request(:delete, media_id, query: query_params, response_type: :json) Types::GraphSuccessResponse.new(response) end |
#download(media_id:, phone_number_id: nil, headers: {}, auth: :auto, as: :binary) ⇒ Object
Download media content
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/kapso_client_ruby/resources/media.rb', line 87 def download(media_id:, phone_number_id: nil, headers: {}, auth: :auto, as: :binary) # First get the media metadata to get the download URL = get(media_id: media_id, phone_number_id: phone_number_id) download_url = .url # Determine authentication strategy use_auth = case auth when :auto # Auto-detect: use auth for graph.facebook.com URLs, no auth for CDNs download_url.include?('graph.facebook.com') when :always true when :never false else raise ArgumentError, 'auth must be :auto, :always, or :never' end # Prepare headers download_headers = headers.dup # Make the download request if use_auth response = @client.fetch(download_url, headers: download_headers) else response = @client.raw_request(:get, download_url, headers: download_headers) end unless response.success? raise Errors::GraphApiError.new( message: "Failed to download media: #{response.status}", http_status: response.status, raw_response: response.body ) end # Return response based on requested format case as when :binary response.body when :response response when :base64 require 'base64' Base64.strict_encode64(response.body) else raise ArgumentError, 'as must be :binary, :response, or :base64' end end |
#get(media_id:, phone_number_id: nil) ⇒ Object
Get media metadata
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/kapso_client_ruby/resources/media.rb', line 57 def get(media_id:, phone_number_id: nil) # phone_number_id is required for Kapso proxy if @client.kapso_proxy? && phone_number_id.nil? raise ArgumentError, 'phone_number_id is required when using Kapso proxy' end query_params = {} query_params[:phone_number_id] = phone_number_id if phone_number_id response = @client.request(:get, media_id, query: query_params, response_type: :json) Types::MediaMetadataResponse.new(response) end |
#info(media_id:, phone_number_id: nil) ⇒ Object
Get media info including size, type, and download URL
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/kapso_client_ruby/resources/media.rb', line 153 def info(media_id:, phone_number_id: nil) = get(media_id: media_id, phone_number_id: phone_number_id) { id: .id, url: .url, mime_type: .mime_type, sha256: .sha256, file_size: .file_size.to_i, messaging_product: .messaging_product } end |
#save_to_file(media_id:, filepath:, phone_number_id: nil, headers: {}, auth: :auto) ⇒ Object
Save media to file
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/kapso_client_ruby/resources/media.rb', line 139 def save_to_file(media_id:, filepath:, phone_number_id: nil, headers: {}, auth: :auto) content = download( media_id: media_id, phone_number_id: phone_number_id, headers: headers, auth: auth, as: :binary ) File.binwrite(filepath, content) filepath end |
#upload(phone_number_id:, type:, file:, filename: nil, messaging_product: 'whatsapp', upload_strategy: nil) ⇒ Object
Upload media file
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/kapso_client_ruby/resources/media.rb', line 13 def upload(phone_number_id:, type:, file:, filename: nil, messaging_product: 'whatsapp', upload_strategy: nil) validate_media_type(type) # Build multipart form data form_data = { 'messaging_product' => messaging_product, 'type' => type } # Handle file parameter - can be File, IO, or file path string file_obj = case file when String # Assume it's a file path File.open(file, 'rb') when File, IO, StringIO file else raise ArgumentError, 'file must be a File, IO object, or file path string' end # Determine filename and content type if filename.nil? && file.is_a?(String) filename = File.basename(file) end content_type = determine_content_type(file_obj, filename, type) form_data['file'] = Faraday::UploadIO.new(file_obj, content_type, filename) form_data['upload_strategy'] = upload_strategy if upload_strategy # Set multipart content type header headers = { 'Content-Type' => 'multipart/form-data' } response = @client.request(:post, "#{phone_number_id}/media", body: form_data, headers: headers, response_type: :json) # Close file if we opened it file_obj.close if file.is_a?(String) && file_obj.respond_to?(:close) Types::MediaUploadResponse.new(response) end |