Class: Whatsapp::Webhook::Message::Media

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby/whatsapp/webhook/message/media.rb

Overview

Shared shape for the media-backed message types (image, video, audio, document, sticker) — a media asset id you can resolve via Media, plus its mime type and content hash.

Direct Known Subclasses

Document, Sticker, Video

Instance Attribute Summary collapse

Attributes inherited from Base

#context, #from, #id, #referral, #timestamp, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

common_attributes

Constructor Details

#initialize(media_id:, mime_type:, sha256:, caption: nil, **base_attributes) ⇒ Media

Returns a new instance of Media.



26
27
28
29
30
31
32
33
# File 'lib/ruby/whatsapp/webhook/message/media.rb', line 26

def initialize(media_id:, mime_type:, sha256:, caption: nil, **base_attributes)
  super(**base_attributes)

  @media_id = media_id
  @mime_type = mime_type
  @sha256 = sha256
  @caption = caption
end

Instance Attribute Details

#captionString?

Returns:

  • (String, nil)


24
25
26
# File 'lib/ruby/whatsapp/webhook/message/media.rb', line 24

def caption
  @caption
end

#media_idString?

Returns The media asset id (resolve via Media#url).

Returns:

  • (String, nil)

    The media asset id (resolve via Media#url).



12
13
14
# File 'lib/ruby/whatsapp/webhook/message/media.rb', line 12

def media_id
  @media_id
end

#mime_typeString?

Returns:

  • (String, nil)


16
17
18
# File 'lib/ruby/whatsapp/webhook/message/media.rb', line 16

def mime_type
  @mime_type
end

#sha256String?

Returns:

  • (String, nil)


20
21
22
# File 'lib/ruby/whatsapp/webhook/message/media.rb', line 20

def sha256
  @sha256
end

Class Method Details

.media_attributes(data, key) ⇒ Hash

Extracts the media-specific fields nested under the given key (e.g. "image", "video"), for subclasses to merge with their own extra attributes before calling new.

Parameters:

  • data (Hash)

    The raw message hash.

  • key (String)

    The type-specific nested key (e.g. "image").

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
# File 'lib/ruby/whatsapp/webhook/message/media.rb', line 42

def media_attributes(data, key)
  {
    media_id: data.dig(key, "id"),
    mime_type: data.dig(key, "mime_type"),
    sha256: data.dig(key, "sha256"),
    caption: data.dig(key, "caption"),
  }
end