Class: WhatsappSdk::Api::Medias
- Defined in:
- lib/whatsapp_sdk/api/medias.rb
Defined Under Namespace
Classes: FileNotFoundError, InvalidMediaTypeError
Instance Method Summary collapse
-
#delete(media_id:) ⇒ Boolean
Delete a Media by ID.
-
#download(url:, file_path:, media_type:) ⇒ Boolean
Download Media by URL.
-
#get(media_id:) ⇒ Resource::Media
Get Media by ID.
- #media(media_id:) ⇒ Object
-
#upload(sender_id:, file_path:, type:, headers: {}) ⇒ Api::Responses::IdResponse
Upload a media.
Methods inherited from Request
#download_file, #initialize, #send_request
Constructor Details
This class inherits a constructor from WhatsappSdk::Api::Request
Instance Method Details
#delete(media_id:) ⇒ Boolean
Delete a Media by ID.
111 112 113 114 115 116 117 118 |
# File 'lib/whatsapp_sdk/api/medias.rb', line 111 def delete(media_id:) response = send_request( http_method: "delete", endpoint: "/#{media_id}" ) Api::Responses::SuccessResponse.success_response?(response: response) end |
#download(url:, file_path:, media_type:) ⇒ Boolean
Download Media by URL.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/whatsapp_sdk/api/medias.rb', line 60 def download(url:, file_path:, media_type:) # Allow download of unsupported media types, since Cloud API may decide to let it through. # https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/discussions/127 # raise InvalidMediaTypeError.new(media_type: media_type) unless valid_media_type?(media_type) content_type_header = map_media_type_to_content_type_header(media_type) response = download_file(url: url, file_path: file_path, content_type_header: content_type_header) return true if response.code.to_i == 200 begin body = JSON.parse(response.body) rescue JSON::ParserError body = { "message" => response.body } end raise Api::Responses::HttpResponseError.new(http_status: response.code, body: body) end |
#get(media_id:) ⇒ Resource::Media
Get Media by ID.
38 39 40 41 42 43 44 45 |
# File 'lib/whatsapp_sdk/api/medias.rb', line 38 def get(media_id:) response = send_request( http_method: "get", endpoint: "/#{media_id}" ) Resource::Media.from_hash(response) end |
#media(media_id:) ⇒ Object
47 48 49 50 |
# File 'lib/whatsapp_sdk/api/medias.rb', line 47 def media(media_id:) warn "[DEPRECATION] `media` is deprecated. Please use `get` instead." get(media_id: media_id) end |
#upload(sender_id:, file_path:, type:, headers: {}) ⇒ Api::Responses::IdResponse
Upload a media. see the official documentation developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/whatsapp_sdk/api/medias.rb', line 87 def upload(sender_id:, file_path:, type:, headers: {}) raise FileNotFoundError.new(file_path: file_path) unless File.file?(file_path) params = { messaging_product: "whatsapp", file: Faraday::FilePart.new(file_path, type), type: type } response = send_request( http_method: "post", endpoint: "#{sender_id}/media", params: params, headers: headers, multipart: true ) Api::Responses::IdResponse.new(response["id"]) end |