Class: PostForMe::Resources::Media

Inherits:
Object
  • Object
show all
Defined in:
lib/post_for_me/resources/media.rb,
sig/post_for_me/resources/media.rbs

Overview

Media are media assets (images, videos, etc.) that can be attached to posts using the media url. These endpoints are only needed if your media is not already available on a publicly accessible URL. Media assets are stored temporarily and are automatically deleted in the following scenarios:

  • When the associated post is published
  • After 24 hours if not attached to any post
  • When the scheduled post is deleted

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Media

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Media.

Parameters:



97
98
99
# File 'lib/post_for_me/resources/media.rb', line 97

def initialize(client:)
  @client = client
end

Instance Method Details

#create_upload_url(request_options: {}) ⇒ PostForMe::Models::MediaCreateUploadURLResponse

To upload media to attach to your post, make a POST request to the /media/create-upload-url endpoint.

You'll receive the public url of your media item (which can be used when making a post) and will include an upload_url which is a signed URL of the storage location for uploading your file to.

This URL is unique and publicly signed for a short time, so make sure to upload your files in a timely manner.

Example flow using JavaScript and the Fetch API:

Request an upload URL

// Step 1: Request an upload URL from your API
const response = await fetch(
  "https://api.postforme.dev/v1/media/create-upload-url",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  }
);

const { media_url, upload_url } = await response.json();

Upload your file to the signed URL

// Step 2: Upload your file to the signed URL
const file = /* your File or Blob object, e.g., from an <input type="file"> */;
await fetch(upload_url, {
  method: 'PUT',
  headers: {
    'Content-Type': 'image/jpeg'
  },
  body: file
});

Use the media_url when creating your post

```js
// Step 3: Use the `media_url` when creating your post
const response = await fetch('https://api.postforme.dev/v1/social-posts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    social_accounts: ['spc_...', ...],
    caption: 'My caption',
    media: [
      {
        url: media_url
      }
    ]
  })
});
```

Parameters:

Returns:

See Also:



85
86
87
88
89
90
91
92
# File 'lib/post_for_me/resources/media.rb', line 85

def create_upload_url(params = {})
  @client.request(
    method: :post,
    path: "v1/media/create-upload-url",
    model: PostForMe::Models::MediaCreateUploadURLResponse,
    options: params[:request_options]
  )
end