Class: Fopost::Rails::CreatePostJob

Inherits:
ApplicationJob show all
Defined in:
lib/fopost/rails/create_post_job.rb

Overview

Create a post — and optionally send it — off the request cycle.

Fopost::Rails::CreatePostJob.perform_later(
content: 'Shipping today.',
accounts: ,
publish: true
)

workspace_id falls back to config.default_workspace_id. options is merged into the create call, so anything the SDK takes (labels, title, settings, …) passes straight through.

Constant Summary

Constants inherited from ApplicationJob

ApplicationJob::MAX_RETRY_WAIT, ApplicationJob::RETRY_AFTER_KEY

Instance Method Summary collapse

Instance Method Details

#perform(content:, accounts:, workspace_id: nil, status: 'draft', schedule_at: nil, publish: false, options: {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fopost/rails/create_post_job.rb', line 19

def perform(content:, accounts:, workspace_id: nil, status: 'draft', schedule_at: nil,
            publish: false, options: {})
  workspace = workspace_id || Fopost::Rails.config.default_workspace_id
  if workspace.nil? || workspace.to_s.empty?
    raise ArgumentError,
          'fopost: pass workspace_id: or set config.default_workspace_id'
  end

  post = with_retry_after do
    client.posts.create(
      workspace_id: workspace,
      content: content,
      accounts: accounts,
      status: status,
      schedule_at: schedule_at,
      **(options || {}).transform_keys(&:to_sym)
    )
  end

  with_retry_after { client.posts.publish(post.id) } if publish

  post.id
end