new_data_notifier 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ New Data Notifier
2
+ ===============
3
+
4
+ If there is some new user signed up on your site or some new article been added by some other user,
5
+ You hope been notified.
6
+
7
+ Example
8
+ =======
9
+
10
+ 1.add a new_data_notifier.rb to initialiers dir, and copy below content:
11
+ NewDataNotifier.default_recipients = ["hlxwell@gmail.com"]
12
+ NewDataNotifier.be_monitored_models = ["Job", "Partner"]
13
+ NewDataNotifier.sender_address = ["new_data@gmail.com"]
14
+
15
+ 2.Add cron task.
16
+ */20 * * * * sh -c "cd /home/michael.he/app/current && rake send_latest_data RAILS_ENV=production"
17
+
18
+
19
+ Copyright (c) 2010 Michael He, released under the MIT license
@@ -0,0 +1,63 @@
1
+ # NewDataNotifier
2
+ require 'action_mailer'
3
+
4
+ class NewDataNotifier < ActionMailer::Base
5
+ self.mailer_name = 'new_dat_notifier'
6
+ self.append_view_path "#{File.dirname(__FILE__)}/views"
7
+
8
+ class << self
9
+ def default_recipients=(recipients)
10
+ @@recipients = recipients
11
+ end
12
+
13
+ def default_recipients
14
+ @@recipients ||= []
15
+ end
16
+
17
+ def be_monitored_models=(models)
18
+ @@models = models
19
+ end
20
+
21
+ def be_monitored_models
22
+ @@models ||= []
23
+ end
24
+
25
+ def sender_address=(address)
26
+ @@sender_address = address
27
+ end
28
+
29
+ def sender_address
30
+ @@sender_address ||= %("New Data Notifier" <newdata.notifier@example.com>)
31
+ end
32
+
33
+ def default_options
34
+ {
35
+ :sender_address => sender_address,
36
+ :recipients => default_recipients,
37
+ :subject => "[New Data Notification]"
38
+ }
39
+ end
40
+ end
41
+
42
+ def notification(data_hash)
43
+ @data_hash = data_hash
44
+ options = self.class.default_options
45
+ mail(:to => options[:recipients], :from => options[:sender_address], :subject => options[:subject]) do |format|
46
+ format.text { render "new_data_notifier/notification" }
47
+ end
48
+ end
49
+
50
+ protected
51
+
52
+ helper_method :object_name
53
+ def object_name(obj)
54
+ if obj.respond_to?(:title) and obj.try(:title).present?
55
+ obj.title
56
+ elsif obj.respond_to?(:name) and obj.try(:name).present?
57
+ obj.name
58
+ else
59
+ "#{obj.class}##{obj.id}"
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,30 @@
1
+ desc "Send mail to tell admin the latest added data."
2
+ task :send_latest_data => :environment do
3
+ time_mark_file_path = File.join(Rails.root, 'tmp', 'last_notification_send_at')
4
+
5
+ # read last sent time, if blank, set current time.
6
+ begin
7
+ last_sent_at = File.read(time_mark_file_path).to_time
8
+ raise if last_sent_at.blank?
9
+ rescue
10
+ last_sent_at = Time.now
11
+ ensure
12
+ File.open(time_mark_file_path, "w") do |f|
13
+ f.write(Time.now)
14
+ end
15
+ end
16
+
17
+ # find data
18
+ data_hash = {}
19
+
20
+ NewDataNotifier.be_monitored_models.each do |model|
21
+ data_hash[model.downcase.to_sym] = model.constantize.all(:conditions => ["created_at > ?", last_sent_at], :order => "created_at DESC")
22
+ end
23
+
24
+ data_hash.delete_if { |key, value| value.blank? }
25
+
26
+ # send mail
27
+ unless data_hash.select { |key, value| value.size > 0 }.blank?
28
+ NewDataNotifier.notification(data_hash).deliver
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+ <% @data_hash.each do |key, value| %>
2
+
3
+ ============ New <%= key %>(s) (<%= value.size %>) ================
4
+ <% value.each do |obj| %>
5
+ <%= object_name(obj) %> --- created at: <%= obj.created_at.to_s(:db) %>
6
+ <% end %>
7
+
8
+ <% end %>
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: new_data_notifier
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Michael He
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-03 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: when has new data been added send notification mail.
23
+ email: hlxwell@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.rdoc
32
+ - lib/new_data_notifier.rb
33
+ - lib/tasks/notifier.rake
34
+ - lib/views/new_data_notifier/notification.text.erb
35
+ has_rdoc: true
36
+ homepage: http://hlxwell.github.com/new_data_notifier
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 57
50
+ segments:
51
+ - 1
52
+ - 8
53
+ - 7
54
+ version: 1.8.7
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: when has new data been added send notification mail.
71
+ test_files: []
72
+