class Contentful::Social::Controller

Public Instance Methods

can_publish?(webhook) click to toggle source
# File lib/contentful/social/controller.rb, line 55
def can_publish?(webhook)
  config.contentful[webhook.space_id]
end
config() click to toggle source
# File lib/contentful/social/controller.rb, line 36
def config
  ::Contentful::Social.config
end
contentful_client() click to toggle source
# 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
publish() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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