Paul: Check-in [76472a19bf]

Online event coordination and survey application

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a shared config object
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:76472a19bf024a3ca085f30bb394651d1a48ba84f18eb2293a4da1dd74ea82c7
User & Date: milouse 2018-11-15 22:22:32
Context
2018-11-15
22:40
Forgot the config singleton in the last commit 🤦 check-in: 9add5281a0 user: milouse tags: trunk
22:22
Add a shared config object check-in: 76472a19bf user: milouse tags: trunk
2018-11-01
17:19
Add a new logger mechanism check-in: ea94e475cb user: milouse tags: trunk
Changes

Changes to .fossil-settings/ignore-glob.

1
2
3
4
5
6
7
8
9
.ruby-gemset
.bundle
tmp/logs/application.log
tmp/pids/sinatra.pid
node_modules
public/paul.js*
polls/*.yml
config/config.yml
config/polls.db


|






1
2
3
4
5
6
7
8
9
.ruby-gemset
.bundle
tmp/logs/*.log
tmp/pids/sinatra.pid
node_modules
public/paul.js*
polls/*.yml
config/config.yml
config/polls.db

Changes to config/config.yml.example.

1

2
3



4
5
6
---

mail:
  from: paul@TODO



  smtp:
    address: localhost
    port: 25

>
|
|
>
>
>



1
2
3
4
5
6
7
8
9
10
---
default: &default
  mail:
    from: paul@TODO

production:
  <<: *default
  smtp:
    address: localhost
    port: 25

Changes to lib/notification.rb.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

require 'mail'
require 'yaml'

# Generic notification class.
# For now, will just send emails
class Notification
  attr_reader :method

  def initialize
    @method = :sendmail
    @from = 'paul@todo.fr'
    conf = load_settings
    if conf.has_key? 'mail'
      if conf['mail'].has_key? 'smtp'
        load_smtp_settings(conf['mail']['smtp'])
      end
      @from = conf['mail']['from'] if conf['mail'].has_key? 'from'
    else
      Mail.defaults do
................................................................................
    rescue Errno::ECONNREFUSED
      ::PaulLogger.log.error 'SMTP server refuses to answer'
    end
  end

  private

  def load_settings
    conf = File.join 'config', 'config.yml'
    return YAML.load_file(conf).freeze if File.exist? conf
    {}
  end

  def load_smtp_settings(conf)
    @method = :smtp
    opts = {
      address: conf['address'],
      port: conf['port']
    }
    [:domain, :user_name, :password, :authentication,



|









|







 







<
<
<
<
<
<







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
42
43
44
45
46
47
48






49
50
51
52
53
54
55
# frozen_string_literal: true

require 'mail'
require './lib/paul_config'

# Generic notification class.
# For now, will just send emails
class Notification
  attr_reader :method

  def initialize
    @method = :sendmail
    @from = 'paul@todo.fr'
    conf = PaulConfig.settings
    if conf.has_key? 'mail'
      if conf['mail'].has_key? 'smtp'
        load_smtp_settings(conf['mail']['smtp'])
      end
      @from = conf['mail']['from'] if conf['mail'].has_key? 'from'
    else
      Mail.defaults do
................................................................................
    rescue Errno::ECONNREFUSED
      ::PaulLogger.log.error 'SMTP server refuses to answer'
    end
  end

  private







  def load_smtp_settings(conf)
    @method = :smtp
    opts = {
      address: conf['address'],
      port: conf['port']
    }
    [:domain, :user_name, :password, :authentication,

Changes to lib/paul_logger.rb.

1
2
3
4
5
6
7
8
9
10




11
12
13
14
15
16
17
18
19
20
# frozen_string_literal: true

require 'logger'
require 'english'

# Custom logger to be used everywhere in the app
class PaulLogger
  class << self
    def log
      return @logger unless @logger.nil?




      @logger = Logger.new 'tmp/logs/application.log'
      @logger.datetime_format = '%Y-%m-%d %H:%M:%S'
      @logger.progname = 'Paul'
      @logger.formatter = proc do |severity, datetime, _, msg|
        "[#{datetime} #{$PID}] #{severity} #{msg}\n"
      end
      @logger
    end
  end
end



|






>
>
>
>
|









1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# frozen_string_literal: true

require 'logger'
require './lib/paul_config'

# Custom logger to be used everywhere in the app
class PaulLogger
  class << self
    def log
      return @logger unless @logger.nil?
      log_file = 'development'
      if PaulConfig.settings.has_key? 'environment'
        log_file = PaulConfig.settings['environment']
      end
      @logger = Logger.new "tmp/logs/#{log_file}.log"
      @logger.datetime_format = '%Y-%m-%d %H:%M:%S'
      @logger.progname = 'Paul'
      @logger.formatter = proc do |severity, datetime, _, msg|
        "[#{datetime} #{$PID}] #{severity} #{msg}\n"
      end
      @logger
    end
  end
end