Class: X::Media
- Extended by:
- BatchFinders, Resources::BatchFinders
- Defined in:
- x-resources/lib/x/resources/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
-
#alt_text ⇒ String?
readonly
The alternative text.
-
#duration_ms ⇒ Integer?
readonly
The duration of a video in milliseconds.
-
#height ⇒ Integer?
readonly
The height in pixels.
-
#media_key ⇒ String
readonly
The media key.
-
#preview_image_url ⇒ String?
readonly
The URL of a video or GIF preview image.
-
#public_metrics ⇒ Hash?
readonly
The public metrics.
-
#type ⇒ String?
readonly
The media type: photo, video, or animated_gif.
-
#url ⇒ String?
readonly
The URL of a photo.
-
#variants ⇒ Array<Hash>
readonly
The video variants with their bit rates, content types, and URLs.
-
#view_count ⇒ Integer?
readonly
The number of views.
-
#width ⇒ Integer?
readonly
The width in pixels.
Class Method Summary collapse
-
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every public field.
-
.find(media, client:, **params) {|problem| ... } ⇒ Media?
Look up media by media key.
-
.find!(media, client:, **params) ⇒ Media
Look up media by media key, which must exist.
-
.find_all(media, client:, concurrency: BatchFinders::DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Media>
Look up many media by media key, in parallel batches.
-
.from_id(media, client: nil) ⇒ Media
Refer to media by media key without a request.
-
.hydrate_all(resources, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Resource>
extended
from Resources::BatchFinders
Replace the resources that are not hydrated with the full resources.
Instance Method Summary collapse
-
#id ⇒ String
The identifier of the media, which is its media key.
-
#media_id ⇒ Integer
The numeric identifier of the media, which its media key names.
Constructor Details
This class inherits a constructor from X::Resource
Instance Attribute Details
#alt_text ⇒ String? (readonly)
The alternative text
227 |
# File 'x-resources/lib/x/resources/media.rb', line 227 attribute :alt_text |
#duration_ms ⇒ Integer? (readonly)
The duration of a video in milliseconds
235 |
# File 'x-resources/lib/x/resources/media.rb', line 235 attribute :duration_ms, :integer |
#height ⇒ Integer? (readonly)
The height in pixels
243 |
# File 'x-resources/lib/x/resources/media.rb', line 243 attribute :height, :integer |
#media_key ⇒ String (readonly)
The media key
184 |
# File 'x-resources/lib/x/resources/media.rb', line 184 attribute :media_key |
#preview_image_url ⇒ String? (readonly)
The URL of a video or GIF preview image
219 |
# File 'x-resources/lib/x/resources/media.rb', line 219 attribute :preview_image_url |
#public_metrics ⇒ Hash? (readonly)
The public metrics
267 |
# File 'x-resources/lib/x/resources/media.rb', line 267 attribute :public_metrics, :object |
#type ⇒ String? (readonly)
The media type: photo, video, or animated_gif
203 |
# File 'x-resources/lib/x/resources/media.rb', line 203 attribute :type |
#url ⇒ String? (readonly)
The URL of a photo
211 |
# File 'x-resources/lib/x/resources/media.rb', line 211 attribute :url |
#variants ⇒ Array<Hash> (readonly)
The video variants with their bit rates, content types, and URLs
259 |
# File 'x-resources/lib/x/resources/media.rb', line 259 attribute :variants, :objects |
#view_count ⇒ Integer? (readonly)
The number of views
275 |
# File 'x-resources/lib/x/resources/media.rb', line 275 attribute :view_count, :integer, key: %w[public_metrics view_count] |
#width ⇒ Integer? (readonly)
The width in pixels
251 |
# File 'x-resources/lib/x/resources/media.rb', line 251 attribute :width, :integer |
Class Method Details
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every public field
67 68 69 |
# File 'x-resources/lib/x/resources/media.rb', line 67 def self.default_params {"media.fields" => FIELDS} end |
.find(media, client:, **params) {|problem| ... } ⇒ Media?
Look up media by media key
105 |
# File 'x-resources/lib/x/resources/media.rb', line 105 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.
120 |
# File 'x-resources/lib/x/resources/media.rb', line 120 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
153 |
# File 'x-resources/lib/x/resources/media.rb', line 153 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
130 |
# File 'x-resources/lib/x/resources/media.rb', line 130 def self.from_id(media, client: nil) = super #: Media |
.hydrate_all(resources, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Resource> Originally defined in module Resources::BatchFinders
Replace the resources that are not hydrated with the full resources
A resource that hydrate would look up is looked up, which is a stub and also a resource a response included without every field, so what comes back is hydrated throughout. A resource that was not found is dropped, as is nil, which a reference to no resource reads as, such as the author of a post whose response named none, and a hydrated resource is kept as it is, so resources that are all hydrated need no lookup. What was found is stored in each original, so hydrating one of them afterwards costs no request, unless params override a default field or expansion parameter: what such a lookup found is not the full resource, so it is returned without being stored, and hydrating an original fetches the full resource. A resource that holds what hydrate returns, because hydrate or an earlier hydrate_all stored it, is replaced with that, and looked up again by no request, since the API bills each resource a lookup returns.
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-uploads answer media_key and media_id alike.
|
|
# File 'x-resources/lib/x/resources/media.rb', line 166
|
#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.
195 |
# File 'x-resources/lib/x/resources/media.rb', line 195 def media_id = Integer(MediaIds.media_key_id(self), 10) |