Class: Contentful::Social::Controller

Inherits:
Webhook::Listener::Controllers::WebhookAware
  • Object
show all
Defined in:
lib/contentful/social/controller.rb

Instance Method Summary collapse

Instance Method Details

#can_publish?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/contentful/social/controller.rb', line 55

def can_publish?(webhook)
  config.contentful[webhook.space_id]
end

#configObject



36
37
38
# File 'lib/contentful/social/controller.rb', line 36

def config
  ::Contentful::Social.config
end

#contentful_clientObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/contentful/social/controller.rb', line 40

def contentful_client
  if config.contentful[webhook.space_id]
    ::Contentful::Client.new(
      access_token: config.contentful[webhook.space_id],
      space: webhook.space_id,
      dynamic_entries: :auto,
      raise_errors: true,
      application_name: 'contentful-social',
      application_version: Contentful::Social::VERSION
    )
  else
    fail "Space '#{webhook.space_id}' not configured"
  end
end

#publishObject



8
9
10
11
12
13
14
# File 'lib/contentful/social/controller.rb', line 8

def publish
  return unless webhook.entry?
  return unless can_publish?(webhook)

  publish_to_twitter(webhook)
  publish_to_facebook(webhook)
end

#publish_to_facebook(webhook) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/contentful/social/controller.rb', line 26

def publish_to_facebook(webhook)
  return unless config.facebook_configured?

  ::Contentful::Social::FacebookHandler.new(config.facebook, contentful_client, webhook).post

  logger.debug 'Successfully published on Facebook'
rescue StandardError => e
  logger.error "Error while trying to publish to Facebook: #{e}"
end

#publish_to_twitter(webhook) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/contentful/social/controller.rb', line 16

def publish_to_twitter(webhook)
  return unless config.twitter_configured?

  ::Contentful::Social::TwitterHandler.new(config.twitter, contentful_client, webhook).tweet

  logger.debug 'Successfully published on Twitter'
rescue StandardError => e
  logger.error "Error while trying to publish to Twitter: #{e}"
end