Module: X::AccountUploader

Extended by:
AccountUploader
Included in:
AccountUploader
Defined in:
lib/x/account_uploader.rb

Overview

Uploads profile images and banners to the X API v1.1

Constant Summary collapse

V1_BASE_URL =

Base URL for X API v1.1 account endpoints

"https://api.x.com/1.1/".freeze
SUPPORTED_EXTENSIONS =

Supported image extensions for profile uploads

%w[gif jpg jpeg png].freeze
MIME_TYPE_MAP =

Mapping of file extensions to MIME types

{"gif" => "image/gif", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "png" => "image/png"}.freeze

Instance Method Summary collapse

Instance Method Details

#update_profile_banner(client:, file_path:, width: nil, height: nil, offset_left: nil, offset_top: nil, boundary: SecureRandom.hex) ⇒ Hash?

Update the authenticating user’s profile banner

Examples:

Update profile banner from a file

AccountUploader.update_profile_banner(client: client, file_path: "banner.png")

Update profile banner with dimensions

AccountUploader.update_profile_banner(client: client, file_path: "banner.png", width: 1500, height: 500)

Parameters:

  • client (Client)

    the X API client

  • file_path (String)

    the path to the image file

  • width (Integer, nil) (defaults to: nil)

    the width of the banner

  • height (Integer, nil) (defaults to: nil)

    the height of the banner

  • offset_left (Integer, nil) (defaults to: nil)

    the left offset of the banner

  • offset_top (Integer, nil) (defaults to: nil)

    the top offset of the banner

  • boundary (String) (defaults to: SecureRandom.hex)

    the multipart boundary

Returns:

  • (Hash, nil)

    nil on success (204 No Content)

Raises:

  • (RuntimeError)

    if the file does not exist

  • (InvalidMediaType)

    if the file type is not supported



66
67
68
69
70
71
# File 'lib/x/account_uploader.rb', line 66

def update_profile_banner(client:, file_path:, width: nil, height: nil, offset_left: nil, offset_top: nil,
  boundary: SecureRandom.hex)
  validate_file!(file_path)
  upload_profile_banner_binary(client:, content: File.binread(file_path), width:, height:, offset_left:,
    offset_top:, boundary:)
end

#update_profile_image(client:, file_path:, boundary: SecureRandom.hex) ⇒ Hash?

Update the authenticating user’s profile image

Examples:

Update profile image from a file

AccountUploader.update_profile_image(client: client, file_path: "avatar.png")

Parameters:

  • client (Client)

    the X API client

  • file_path (String)

    the path to the image file

  • boundary (String) (defaults to: SecureRandom.hex)

    the multipart boundary

Returns:

  • (Hash, nil)

    the updated user object

Raises:

  • (RuntimeError)

    if the file does not exist

  • (InvalidMediaType)

    if the file type is not supported



29
30
31
32
# File 'lib/x/account_uploader.rb', line 29

def update_profile_image(client:, file_path:, boundary: SecureRandom.hex)
  validate_file!(file_path)
  upload_profile_image_binary(client:, content: File.binread(file_path), boundary:)
end

#upload_profile_banner_binary(client:, content:, width: nil, height: nil, offset_left: nil, offset_top: nil, boundary: SecureRandom.hex) ⇒ Hash?

Update the authenticating user’s profile banner from binary content

Examples:

Update profile banner from binary content

AccountUploader.upload_profile_banner_binary(client: client, content: image_data)

Parameters:

  • client (Client)

    the X API client

  • content (String)

    the binary image content

  • width (Integer, nil) (defaults to: nil)

    the width of the banner

  • height (Integer, nil) (defaults to: nil)

    the height of the banner

  • offset_left (Integer, nil) (defaults to: nil)

    the left offset of the banner

  • offset_top (Integer, nil) (defaults to: nil)

    the top offset of the banner

  • boundary (String) (defaults to: SecureRandom.hex)

    the multipart boundary

Returns:

  • (Hash, nil)

    nil on success (204 No Content)



86
87
88
89
90
91
# File 'lib/x/account_uploader.rb', line 86

def upload_profile_banner_binary(client:, content:, width: nil, height: nil, offset_left: nil, offset_top: nil,
  boundary: SecureRandom.hex)
  body = construct_banner_body(content:, width:, height:, offset_left:, offset_top:, boundary:)
  headers = {"Content-Type" => "multipart/form-data; boundary=#{boundary}"}
  v1_client(client).post("account/update_profile_banner.json", body, headers:)
end

#upload_profile_image_binary(client:, content:, boundary: SecureRandom.hex) ⇒ Hash?

Update the authenticating user’s profile image from binary content

Examples:

Update profile image from binary content

AccountUploader.upload_profile_image_binary(client: client, content: image_data)

Parameters:

  • client (Client)

    the X API client

  • content (String)

    the binary image content

  • boundary (String) (defaults to: SecureRandom.hex)

    the multipart boundary

Returns:

  • (Hash, nil)

    the updated user object



43
44
45
46
47
# File 'lib/x/account_uploader.rb', line 43

def upload_profile_image_binary(client:, content:, boundary: SecureRandom.hex)
  body = construct_multipart_body(field_name: "image", content:, boundary:)
  headers = {"Content-Type" => "multipart/form-data; boundary=#{boundary}"}
  v1_client(client).post("account/update_profile_image.json", body, headers:)
end