Class: Rivendell::Import::Notifier::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rivendell/import/notifier/base.rb

Direct Known Subclasses

Mail

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.notify(target, options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rivendell/import/notifier/base.rb', line 78

def self.notify(target, options = {})
  new_notifier = case options.delete(:by)
                 when :email
                   Mail.new options.merge(:to => target)
                 end

  key = new_notifier.parameters_hash
  if existing_notifier = where(:type => new_notifier.type, :key => key).first
    existing_notifier
  else
    new_notifier.save!
    new_notifier
  end
end

Instance Method Details

#delayObject



25
26
27
# File 'lib/rivendell/import/notifier/base.rb', line 25

def delay
  tasks.pending.empty? ? 0 : 60
end

#loggerObject



43
44
45
# File 'lib/rivendell/import/notifier/base.rb', line 43

def logger
  Rivendell::Import.logger
end

#notifyObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rivendell/import/notifier/base.rb', line 29

def notify
  to_sent_notifications = notifications.to_sent.includes(:task)

  if delay > 0
    to_sent_notifications = to_sent_notifications.waiting_for_more_than(delay)
  end

  if to_sent_notifications.present?
    logger.debug "Notify #{to_sent_notifications.size} tasks with #{self.inspect}"
    notify! to_sent_notifications.map(&:task)
    to_sent_notifications.update_all(:sent_at => Time.now)
  end
end

#parametersObject



10
11
12
# File 'lib/rivendell/import/notifier/base.rb', line 10

def parameters
  {}
end

#parameters=(parameters) ⇒ Object



47
48
49
# File 'lib/rivendell/import/notifier/base.rb', line 47

def parameters=(parameters)
  parameters.each { |k,v| send "#{k}=", v }
end

#parameters_hashObject



61
62
63
64
# File 'lib/rivendell/import/notifier/base.rb', line 61

def parameters_hash
  parameters_as_string = parameters.sort_by { |p| p.first.to_s }.flatten.join('-')
  Digest::SHA256.hexdigest parameters_as_string
end

#raw_parametersObject



51
52
53
# File 'lib/rivendell/import/notifier/base.rb', line 51

def raw_parameters
  read_attribute :parameters
end

#read_parametersObject



55
56
57
# File 'lib/rivendell/import/notifier/base.rb', line 55

def read_parameters
  self.parameters = ActiveSupport::JSON.decode(raw_parameters) if raw_parameters.present?
end

#write_parametersObject



66
67
68
69
70
71
72
73
74
# File 'lib/rivendell/import/notifier/base.rb', line 66

def write_parameters
  if parameters.present?
    write_attribute :parameters, parameters.to_json
    write_attribute :key, parameters_hash
  else
    write_attribute :parameters, nil
    write_attribute :key, nil
  end
end