Class: WCC::Contentful::WebhookEnableJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/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



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

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



9
10
11
12
13
14
15
16
# File 'lib/wcc/contentful/webhook_enable_job.rb', line 9

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