Class: Contentful::Moderator::Controller

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

Instance Method Summary collapse

Instance Method Details

#author_field(webhook) ⇒ Object



88
89
90
# File 'lib/contentful/moderator/controller.rb', line 88

def author_field(webhook)
  content_type(webhook).author_field
end

#auto_saveObject Also known as: save



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/contentful/moderator/controller.rb', line 7

def auto_save
  return unless webhook.entry?
  return unless notificable?(webhook)
  logger.debug("Workflow Webhook Received for '#{webhook.space_id}/#{webhook.id}': Checking for Notifications")

  emails = []
  emails << notify_authors(webhook) if notify_author?(webhook)
  emails << notify_reviewers(webhook) if notify_reviewer?(webhook)

  send_emails(emails)

  logger.debug("\tDone!")
end

#configObject



22
23
24
# File 'lib/contentful/moderator/controller.rb', line 22

def config
  ::Contentful::Moderator.config
end

#content_type(webhook) ⇒ Object



62
63
64
# File 'lib/contentful/moderator/controller.rb', line 62

def content_type(webhook)
  config.content_types[webhook_content_type(webhook)]
end

#email_body(type, webhook) ⇒ Object



54
55
56
# File 'lib/contentful/moderator/controller.rb', line 54

def email_body(type, webhook)
  self.send("#{type}_field", webhook).email_body.gsub("'webhook_url'", webhook_url(webhook))
end

#notificable?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/contentful/moderator/controller.rb', line 66

def notificable?(webhook)
  config.content_types.keys.include?(webhook_content_type(webhook))
end

#notify_author?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
# File 'lib/contentful/moderator/controller.rb', line 70

def notify_author?(webhook)
  field = webhook.fields[reviewer_field(webhook).field_id]
  if field.is_a? Hash
    return field[field.keys.first] == reviewer_field(webhook).notify_author_on
  else
    return field == reviewer_field(webhook).notify_author_on
  end
end

#notify_authors(webhook) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/contentful/moderator/controller.rb', line 32

def notify_authors(webhook)
  logger.debug("\tCreating Author Notification Email")
  this = self
  ::Mail.new do
    from this.config.mail_origin
    to this.config.authors
    subject this.reviewer_field(webhook).email_subject
    body this.email_body(:reviewer, webhook)
  end
end

#notify_reviewer?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
# File 'lib/contentful/moderator/controller.rb', line 79

def notify_reviewer?(webhook)
  field = webhook.fields[author_field(webhook).field_id]
  if field.is_a? Hash
    return field[field.keys.first] == author_field(webhook).notify_reviewer_on
  else
    return field == author_field(webhook).notify_reviewer_on
  end
end

#notify_reviewers(webhook) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/contentful/moderator/controller.rb', line 43

def notify_reviewers(webhook)
  logger.debug("\tCreating Reviewer Notification Email")
  this = self
  ::Mail.new do
    from this.config.mail_origin
    to this.config.editors
    subject this.author_field(webhook).email_subject
    body this.email_body(:author, webhook)
  end
end

#reviewer_field(webhook) ⇒ Object



92
93
94
# File 'lib/contentful/moderator/controller.rb', line 92

def reviewer_field(webhook)
  content_type(webhook).reviewer_field
end

#send_emails(emails) ⇒ Object



26
27
28
29
30
# File 'lib/contentful/moderator/controller.rb', line 26

def send_emails(emails)
  emails.each do |email|
    email.deliver!
  end
end

#webhook_content_type(webhook) ⇒ Object



58
59
60
# File 'lib/contentful/moderator/controller.rb', line 58

def webhook_content_type(webhook)
  webhook.sys['contentType']['sys']['id']
end

#webhook_url(webhook) ⇒ Object



96
97
98
# File 'lib/contentful/moderator/controller.rb', line 96

def webhook_url(webhook)
  "https://app.contentful.com/spaces/#{webhook.space_id}/entries/#{webhook.id}"
end