Class: Mail2FrontMatter::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mail2frontmatter/watcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) {|config| ... } ⇒ Watcher

Returns a new instance of Watcher.

Yields:

  • (config)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mail2frontmatter/watcher.rb', line 11

def initialize(config = nil, &block)
  # load config from file
  if config.is_a?(String)
    config = YAML.load_file(config).deep_symbolize_keys!

  # load config from file at default data/mail2frontmatter.yml relative from run directory
  elsif config.is_a?(NilClass)

    default_config = File.join(Dir.pwd, 'data', 'mail2frontmatter.yml')

    if File.exist?(default_config)
      config = YAML.load_file(default_config).deep_symbolize_keys!
    else
      raise LoadError, 'no configuration given or found at ./data/mail2frontmatter.yml'
    end

  elsif !config.is_a?(Hash)
    raise ArgumentError, 'not a valid configuration type'
  end

  yield(config) if block_given?

  preprocessors = config.delete(:preprocessors) || []
  preprocessors.each do |processor|

    begin
      require "mail2frontmatter/#{processor[:key]}"
    rescue LoadError => e
      puts "could not require specified preprocessor '#{processor[:key]}.rb', no such file in load path. Check your configuration and try again \n\n"
      raise e
    end

    klass = "Mail2FrontMatter::#{processor[:key].underscore.classify}".constantize.register(processor[:options] || {})
  end

  mail_protocol = config.delete(:protocol) || :imap
  poll_interval = config.delete(:interval) || 60

  @receiver = config.delete(:receiver)
  @senders  = config.delete(:senders)
  @logger   = Logger.new(config.delete(:log_file))

  Mailman.config.poll_interval = poll_interval
  Mailman.config.ignore_stdin = true

  Mailman.config.send("#{mail_protocol}=", config[:mailman])
  Mailman.config.logger = @logger
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/mail2frontmatter/watcher.rb', line 9

def logger
  @logger
end

Instance Method Details

#runObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mail2frontmatter/watcher.rb', line 60

def run
  Mailman::Application.run do
    from(@senders).to(@receiver) do
      logger = Mailman.config.logger

      logger.info('parsing message...')
      parser = Mail2FrontMatter::Parser.new(message)

      logger.info('processing body and attachments...')
      , body = Mail2FrontMatter::PreProcessor.process(parser., parser.body)

      logger.info('saving processed post...')
      Mail2FrontMatter::Writer.write(, body)
    end
  end
end