Class: X::Media

Inherits:
Resource show all
Extended by:
BatchFinders
Defined in:
x-objects/lib/x/objects/media.rb

Overview

A photo, video, or animated GIF attached to a post, which its media key identifies

Constant Summary collapse

FIELDS =

Every public media field

A minor release may add to it the fields the API adds, so that a lookup asks for them too; see Resource#hydrated? for what that means for a resource looked up with a list of fields of its own.

%w[alt_text duration_ms height media_key preview_image_url public_metrics type url variants width].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from X::Resource

Instance Attribute Details

#alt_text ⇒ String? (readonly)

The alternative text

Examples:

Get the alternative text

media.alt_text

Returns:

  • (String, nil) —

    the alternative text



222
# File 'x-objects/lib/x/objects/media.rb', line 222

attribute :alt_text

#duration_ms ⇒ Integer? (readonly)

The duration of a video in milliseconds

Examples:

Get the duration

media.duration_ms

Returns:

  • (Integer, nil) —

    the duration in milliseconds



230
# File 'x-objects/lib/x/objects/media.rb', line 230

attribute :duration_ms, :integer

#height ⇒ Integer? (readonly)

The height in pixels

Examples:

Get the height

media.height

Returns:

  • (Integer, nil) —

    the height in pixels



238
# File 'x-objects/lib/x/objects/media.rb', line 238

attribute :height, :integer

#media_key ⇒ String (readonly)

The media key

Examples:

Get the media key

media.media_key

Returns:

  • (String) —

    the media key



179
# File 'x-objects/lib/x/objects/media.rb', line 179

attribute :media_key

#preview_image_url ⇒ String? (readonly)

The URL of a video or GIF preview image

Examples:

Get the preview image URL

media.preview_image_url

Returns:

  • (String, nil) —

    the preview image URL



214
# File 'x-objects/lib/x/objects/media.rb', line 214

attribute :preview_image_url

#public_metrics ⇒ Hash? (readonly)

The public metrics

Examples:

Get the public metrics

media.public_metrics

Returns:

  • (Hash, nil) —

    the public metrics



262
# File 'x-objects/lib/x/objects/media.rb', line 262

attribute :public_metrics, :object

#type ⇒ String? (readonly)

The media type: photo, video, or animated_gif

Examples:

Get the type

media.type

Returns:

  • (String, nil) —

    the media type



198
# File 'x-objects/lib/x/objects/media.rb', line 198

attribute :type

#url ⇒ String? (readonly)

The URL of a photo

Examples:

Get the URL

media.url

Returns:

  • (String, nil) —

    the URL



206
# File 'x-objects/lib/x/objects/media.rb', line 206

attribute :url

#variants ⇒ Array<Hash> (readonly)

The video variants with their bit rates, content types, and URLs

Examples:

Get the variants

media.variants

Returns:

  • (Array<Hash>) —

    the variants, empty if there are none



254
# File 'x-objects/lib/x/objects/media.rb', line 254

attribute :variants, :objects

#view_count ⇒ Integer? (readonly)

The number of views

Examples:

Get the view count

media.view_count

Returns:

  • (Integer, nil) —

    the view count



270
# File 'x-objects/lib/x/objects/media.rb', line 270

attribute :view_count, :integer, key: %w[public_metrics view_count]

#width ⇒ Integer? (readonly)

The width in pixels

Examples:

Get the width

media.width

Returns:

  • (Integer, nil) —

    the width in pixels



246
# File 'x-objects/lib/x/objects/media.rb', line 246

attribute :width, :integer

Class Method Details

.default_params ⇒ Hash{String => Array<String>}

The default query parameters requesting every public field

Examples:

Get the default fields

X::Media.default_params["media.fields"]

Returns:

  • (Hash{String => Array<String>}) —

    the default query parameters



62
63
64
# File 'x-objects/lib/x/objects/media.rb', line 62

def self.default_params
  {"media.fields" => FIELDS}
end

.find(media, client:, **params) {|problem| ... } ⇒ Media?

Look up media by media key

Examples:

Look up what an upload returned

X::Media.find(uploaded, client: client)

Parameters:

  • media (#media_key, String, Media) —

    the media key, what an upload returned, or media

  • client (Object) —

    the client used to make the request

  • params (Hash) —

    query parameters merged over the default parameters

Yield Parameters:

  • problem (Problem) —

    each problem the API reported

Returns:

  • (Media, nil) —

    the media, or nil if it was not found



100
# File 'x-objects/lib/x/objects/media.rb', line 100

def self.find(media, client:, **params) = super(key_of(media), client:, **params) #: Media?

.find!(media, client:, **params) ⇒ Media

Look up media by media key, which must exist

The error it raises names the media key looked up, whether a media key, what an upload returned, or media was given.

Examples:

Look up what an upload returned

X::Media.find!(uploaded, client: client)

Parameters:

  • media (#media_key, String, Media) —

    the media key, what an upload returned, or media

  • client (Object) —

    the client used to make the request

  • params (Hash) —

    query parameters merged over the default parameters

Returns:

Raises:



115
# File 'x-objects/lib/x/objects/media.rb', line 115

def self.find!(media, client:, **params) = super(key_of(media), client:, **params) #: Media

.find_all(media, client:, concurrency: BatchFinders::DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Media>

Look up many media by media key, in parallel batches

Examples:

Look up what the uploads returned

X::Media.find_all(uploads, client: client)

Parameters:

  • media (Array<#media_key, String, Media>) —

    the media keys, what the uploads returned, or media

  • client (Object) —

    the client used to make the requests

  • params (Hash) —

    query parameters merged over the default parameters, and the concurrency of the batches

Yield Parameters:

  • problem (Problem) —

    each problem the API reported

Returns:

  • (Array<Media>) —

    the media that was found



148
# File 'x-objects/lib/x/objects/media.rb', line 148

def self.find_all(media, client:, concurrency: BatchFinders::DEFAULT_CONCURRENCY, **params) = super(media.map { |value| key_of(value) }, client:, concurrency:, **params) #: Array[Media]

.from_id(media, client: nil) ⇒ Media

Refer to media by media key without a request

Examples:

Refer to what an upload returned

X::Media.from_id(uploaded, client: client).hydrate.url

Parameters:

  • media (#media_key, String, Media) —

    the media key, what an upload returned, or media

  • client (Object, nil) (defaults to: nil) —

    the client used to fetch the media

Returns:

  • (Media) —

    a stub that hydrates to the full media



125
# File 'x-objects/lib/x/objects/media.rb', line 125

def self.from_id(media, client: nil) = super #: Media

Instance Method Details

#id ⇒ String

The identifier of the media, which is its media key

Media is looked up by its media key, so that is what identifies it here, where the identifier of uploaded media, and the one a post or a direct message attaches media by, is the number media_id reads. Both X::Media and the uploaded media of x-uploader answer media_key and media_id alike.

Examples:

Get the media key

media.id # => "3_1880028106020515840"

Returns:

  • (String) —

    the media key



# File 'x-objects/lib/x/objects/media.rb', line 161

#media_id ⇒ Integer

The numeric identifier of the media, which its media key names

A media key names the identifier after the number of the type of the media and an underscore. It is the identifier an upload returns, and the one a post or a direct message attaches media by.

Examples:

Get the identifier the media key names

X::Media.from_id("3_1880028106020515840").media_id # => 1880028106020515840

Returns:

  • (Integer) —

    the media identifier



190
# File 'x-objects/lib/x/objects/media.rb', line 190

def media_id = Integer(MediaIds.media_key_id(self), 10)