Class: Contentful::Moderator::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/moderator/config.rb

Constant Summary collapse

DEFAULT_PORT =
33123
DEFAULT_ENDPOINT =
'/moderator'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Config

Returns a new instance of Config.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/contentful/moderator/config.rb', line 16

def initialize(config = {})
  @config = Hashie::Mash.new(config)

  @config.port = (ENV.key?('PORT') ? ENV['PORT'].to_i : DEFAULT_PORT) unless @config.port?
  @config.endpoint = DEFAULT_ENDPOINT unless @config.endpoint?

  fail ':content_types not set' unless @config.content_types? && !@config.content_types.empty?
  fail ':authors not set' unless @config.authors? && !@config.authors.empty?
  fail ':editors not set' unless @config.editors? && !@config.editors.empty?
  fail ':mail_origin not set' unless @config.mail_origin?
  fail ':mailer_settings not properly configured' unless mailer_configured?

  configure_mailer
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/contentful/moderator/config.rb', line 10

def config
  @config
end

Class Method Details

.load(path) ⇒ Object



12
13
14
# File 'lib/contentful/moderator/config.rb', line 12

def self.load(path)
  new(Hashie::Mash.load(path))
end

Instance Method Details

#authorsObject



39
40
41
# File 'lib/contentful/moderator/config.rb', line 39

def authors
  @config.authors
end

#configure_mailerObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/contentful/moderator/config.rb', line 89

def configure_mailer
  this = self
  Mail.defaults do
    delivery_method this.config.mailer_settings.connection_type.to_sym, {
      address: this.config.mailer_settings.address,
      port: this.config.mailer_settings.port,
      domain: this.config.mailer_settings.domain,
      user_name: this.mailer_username,
      password: this.mailer_password,
      authentication: this.config.mailer_settings.authentication,
      enable_starttls_auto: this.config.mailer_settings.enable_starttls_auto,
      openssl_verify_mode: this.config.mailer_settings.openssl_verify_mode,
      ssl: this.config.mailer_settings.ssl,
      tls: this.config.mailer_settings.tls
    }
  end
end

#content_typesObject



47
48
49
# File 'lib/contentful/moderator/config.rb', line 47

def content_types
  @config.content_types
end

#editorsObject



43
44
45
# File 'lib/contentful/moderator/config.rb', line 43

def editors
  @config.editors
end

#endpointObject



35
36
37
# File 'lib/contentful/moderator/config.rb', line 35

def endpoint
  @config.endpoint
end

#mail_originObject



51
52
53
# File 'lib/contentful/moderator/config.rb', line 51

def mail_origin
  @config.mail_origin
end

#mailer_configured?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/contentful/moderator/config.rb', line 69

def mailer_configured?
  return false unless @config.mailer_settings?
  return false unless @config.mailer_settings.connection_type
  return false unless @config.mailer_settings.address
  return false unless @config.mailer_settings.port
  return false unless @config.mailer_settings.domain

  # Optionals - Explicitly left here as reminder
  #
  # return false unless @config.mailer_settings.user_name
  # return false unless @config.mailer_settings.password
  # return false unless @config.mailer_settings.authentication
  # return false unless @config.mailer_settings.enable_starttls_auto
  # return false unless @config.mailer_settings.openssl_verify_mode
  # return false unless @config.mailer_settings.ssl
  # return false unless @config.mailer_settings.tls

  true
end

#mailer_passwordObject



64
65
66
67
# File 'lib/contentful/moderator/config.rb', line 64

def mailer_password
  return ENV['ENV_MAILER_PASSWORD'] if config.mailer_settings.password == "'env_mailer_password'"
  config.mailer_settings.password
end

#mailer_settingsObject



55
56
57
# File 'lib/contentful/moderator/config.rb', line 55

def mailer_settings
  @config.mailer_settings
end

#mailer_usernameObject



59
60
61
62
# File 'lib/contentful/moderator/config.rb', line 59

def mailer_username
  return ENV['ENV_MAILER_USERNAME'] if config.mailer_settings.user_name == "'env_mailer_username'"
  config.mailer_settings.user_name
end

#portObject



31
32
33
# File 'lib/contentful/moderator/config.rb', line 31

def port
  @config.port
end