Class: WCC::Contentful::WebhookEnableJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/wcc/contentful/webhook_enable_job.rb

Instance Method Summary collapse

Instance Method Details

#enable_webhook(client, receive_url:, webhook_username: nil, webhook_password: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/jobs/wcc/contentful/webhook_enable_job.rb', line 19

def enable_webhook(client, receive_url:, webhook_username: nil, webhook_password: nil)
  webhook = client.webhook_definitions.items.find { |w| w['url'] == receive_url }
  logger.debug "existing webhook: #{webhook.inspect}" if webhook
  return if webhook

  body = {
    'name' => 'WCC::Contentful webhook',
    'url' => receive_url,
    'topics' => [
      '*.publish',
      '*.unpublish'
    ],
    'filters' => webhook_filters
  }
  body['httpBasicUsername'] = webhook_username if webhook_username.present?
  body['httpBasicPassword'] = webhook_password if webhook_password.present?

  begin
    resp = client.post_webhook_definition(body)
    logger.info "Created webhook: #{resp.raw.dig('sys', 'id')}"
  rescue WCC::Contentful::SimpleClient::ApiError => e
    logger.error "#{e.response.code}: #{e.response.raw}" if e.response
    raise
  end
end

#perform(args = {}) ⇒ Object



10
11
12
13
14
15
16
17
# File 'app/jobs/wcc/contentful/webhook_enable_job.rb', line 10

def perform(args = {})
  args = default_configuration.merge!(args)

  client = WCC::Contentful::SimpleClient::Management.new(
    **args
  )
  enable_webhook(client, args.slice(:receive_url, :webhook_username, :webhook_password))
end