Class: PostForMe::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/post_for_me/client.rb,
sig/post_for_me/client.rbs

Constant Summary collapse

DEFAULT_MAX_RETRIES =

Default max number of retries to attempt after a failed retryable request.

Returns:

  • (2)
2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

Returns:

  • (Float)
60.0
DEFAULT_INITIAL_RETRY_DELAY =

Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.

Returns:

  • (Float)
0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

Returns:

  • (Float)
8.0

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS, Internal::Transport::BaseClient::PostForMe

Instance Attribute Summary collapse

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(api_key: ENV["POST_FOR_ME_API_KEY"], base_url: ENV["POST_FOR_ME_BASE_URL"], max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with the API.

"https://api.example.com/v2/". Defaults to ENV["POST_FOR_ME_BASE_URL"]

Parameters:

  • api_key (String, nil) (defaults to: ENV["POST_FOR_ME_API_KEY"])

    Defaults to ENV["POST_FOR_ME_API_KEY"]

  • base_url (String, nil) (defaults to: ENV["POST_FOR_ME_BASE_URL"])

    Override the default base URL for the API, e.g.,

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/post_for_me/client.rb', line 154

def initialize(
  api_key: ENV["POST_FOR_ME_API_KEY"],
  base_url: ENV["POST_FOR_ME_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  base_url ||= "https://api.postforme.dev"

  if api_key.nil?
    raise ArgumentError.new("api_key is required, and can be set via environ: \"POST_FOR_ME_API_KEY\"")
  end

  headers = {}
  custom_headers_env = ENV["POST_FOR_ME_CUSTOM_HEADERS"]
  unless custom_headers_env.nil?
    parsed = {}
    custom_headers_env.split("\n").each do |line|
      colon = line.index(":")
      unless colon.nil?
        parsed[line[0...colon].strip] = line[(colon + 1)..].strip
      end
    end
    headers = parsed.merge(headers)
  end

  @api_key = api_key.to_s

  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
    headers: headers
  )

  @media = PostForMe::Resources::Media.new(client: self)
  @social_posts = PostForMe::Resources::SocialPosts.new(client: self)
  @social_post_results = PostForMe::Resources::SocialPostResults.new(client: self)
  @social_accounts = PostForMe::Resources::SocialAccounts.new(client: self)
  @social_account_feeds = PostForMe::Resources::SocialAccountFeeds.new(client: self)
  @webhooks = PostForMe::Resources::Webhooks.new(client: self)
  @social_post_previews = PostForMe::Resources::SocialPostPreviews.new(client: self)
end

Instance Attribute Details

#api_keyString (readonly)

Returns:

  • (String)


19
20
21
# File 'lib/post_for_me/client.rb', line 19

def api_key
  @api_key
end

#mediaPostForMe::Resources::Media (readonly)

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


30
31
32
# File 'lib/post_for_me/client.rb', line 30

def media
  @media
end

#social_account_feedsPostForMe::Resources::SocialAccountFeeds (readonly)

The social account feed is every post made for the social account, including posts not made through our API. Use this endpoint to get the platform details for any post made under the connected account. To use this endpoint accounts must be connected with the "feeds" permission.

Details will include:

  • Post information including caption, url, media, etc..
  • When passing expand=metrics, Metrics information including views, likes, follows, etc..
  • For Facebook feeds with expand=metrics, page size is capped by the server-side FacebookFeedMetricsLimitCap setting (default: 10) to protect API memory usage.

Note: Currently the following platforms are supported:

  • Instagram, may take up to 48 hours for some metrics to be avaialbe
  • Facebook
  • TikTok, consumer API exposes less analytics for more details connect through TikTok Business
  • TikTok Business,
  • Youtube
  • Threads
  • X (Twitter)
  • Bluesky, Bluesky does not expose views or impressions through their API.
  • Pinterest
  • LinkedIn, metrics are only available for company pages. LinkedIn has currently stopped giving permission for personal page analytics, we are on the waitlist for when they resume.


92
93
94
# File 'lib/post_for_me/client.rb', line 92

def 
  @social_account_feeds
end

#social_accountsPostForMe::Resources::SocialAccounts (readonly)

Social accounts represent platform-specific accounts (e.g. Twitter, LinkedIn, Facebook) that are used for publishing posts. Each social account has a unique id that can be referenced when creating or scheduling posts to specify which platforms the content should be published to.



60
61
62
# File 'lib/post_for_me/client.rb', line 60

def social_accounts
  @social_accounts
end

#social_post_previewsPostForMe::Resources::SocialPostPreviews (readonly)

Social Post Previews allow you to see what a Social Post will create for each account in the post.



129
130
131
# File 'lib/post_for_me/client.rb', line 129

def social_post_previews
  @social_post_previews
end

#social_post_resultsPostForMe::Resources::SocialPostResults (readonly)

Post results represent the outcome of publishing content to various social media platforms. They provide comprehensive information including:

  • Publication status (success/failure)
  • Any errors or issues encountered during posting
  • Platform url to view the published post


53
54
55
# File 'lib/post_for_me/client.rb', line 53

def social_post_results
  @social_post_results
end

#social_postsPostForMe::Resources::SocialPosts (readonly)

Posts represent content that can be published across multiple social media platforms. Each post can have platform-specific content variations, allowing customization for different platforms and accounts. Content can be defined at three levels:

  1. Default content for all platforms
  2. Platform-specific content overrides
  3. Account-specific content overrides

The system will use the most specific content override available when publishing to each platform and account.



44
45
46
# File 'lib/post_for_me/client.rb', line 44

def social_posts
  @social_posts
end

#webhooksPostForMe::Resources::Webhooks (readonly)

Webhooks enable you to subscribe to certain events. This involves Post for Me making a POST request to the URL of any webhooks you create. Only the events you subscribe to will be sent to your webhook URL.

Payload

When an event happens that your webhook is subscribed to, we will make a POST request with the following JSON body

    {
        "event_type": "",
        "data": {}
    }

The event_type will be the event that triggered the webhook POST, data will be the resulting entity from the event

Security

To verify the POST to your webhook URL is from us we will include a secret in the header "Post-For-Me-Webhook-Secret". When you create a webhook you will receive the secret in the response.

Retries

If your server fails to respond with a 2XX code, requests to it will be retried with exponential backoff around 8 times over the course of just over a day.



124
125
126
# File 'lib/post_for_me/client.rb', line 124

def webhooks
  @webhooks
end