mail_stats 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/.rspec +2 -0
  2. data/.rvmrc +4 -0
  3. data/Gemfile +14 -0
  4. data/Gemfile.lock +124 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +22 -0
  7. data/Rakefile +38 -0
  8. data/app/controllers/mail_stats/application_controller.rb +32 -0
  9. data/app/controllers/mail_stats/link_controller.rb +15 -0
  10. data/app/controllers/mail_stats/pixel_controller.rb +15 -0
  11. data/app/models/mail_stats/link.rb +10 -0
  12. data/app/models/mail_stats/pixel.rb +9 -0
  13. data/app/models/mail_stats/sent_mail.rb +7 -0
  14. data/app/models/mail_stats/visit.rb +6 -0
  15. data/config/routes.rb +4 -0
  16. data/db/migrate/20111006191933_create_mail_stats_links.rb +10 -0
  17. data/db/migrate/20111007095146_create_mail_stats_pixels.rb +9 -0
  18. data/db/migrate/20111011075844_create_mail_stats_sent_mails.rb +11 -0
  19. data/db/migrate/20111011080641_create_mail_stats_visits.rb +9 -0
  20. data/lib/generators/mail_stats/install/USAGE +8 -0
  21. data/lib/generators/mail_stats/install/install_generator.rb +11 -0
  22. data/lib/generators/mail_stats/install/templates/initializer.rb +5 -0
  23. data/lib/mail_stats.rb +9 -0
  24. data/lib/mail_stats/base.rb +7 -0
  25. data/lib/mail_stats/engine.rb +8 -0
  26. data/lib/mail_stats/interceptors/interceptor_prototype.rb +17 -0
  27. data/lib/mail_stats/interceptors/mail_stats_interceptor.rb +64 -0
  28. data/lib/mail_stats/version.rb +3 -0
  29. data/spec/controllers/mail_stats/link_controller_spec.rb +28 -0
  30. data/spec/controllers/mail_stats/pixel_controller_spec.rb +27 -0
  31. data/spec/dummy/Gemfile +30 -0
  32. data/spec/dummy/Gemfile.lock +112 -0
  33. data/spec/dummy/README +261 -0
  34. data/spec/dummy/Rakefile +7 -0
  35. data/spec/dummy/app/assets/images/rails.png +0 -0
  36. data/spec/dummy/app/assets/javascripts/application.js +7 -0
  37. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  38. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  39. data/spec/dummy/app/controllers/site_controller.rb +4 -0
  40. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  41. data/spec/dummy/app/mailers/article_mailer.rb +9 -0
  42. data/spec/dummy/app/views/article_mailer/new_article.html.erb +16 -0
  43. data/spec/dummy/app/views/article_mailer/new_article.text.erb +5 -0
  44. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  45. data/spec/dummy/config.ru +4 -0
  46. data/spec/dummy/config/application.rb +49 -0
  47. data/spec/dummy/config/boot.rb +6 -0
  48. data/spec/dummy/config/database.yml +25 -0
  49. data/spec/dummy/config/environment.rb +5 -0
  50. data/spec/dummy/config/environments/development.rb +30 -0
  51. data/spec/dummy/config/environments/production.rb +60 -0
  52. data/spec/dummy/config/environments/test.rb +42 -0
  53. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/spec/dummy/config/initializers/inflections.rb +10 -0
  55. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  56. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  57. data/spec/dummy/config/initializers/session_store.rb +8 -0
  58. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  59. data/spec/dummy/config/locales/en.yml +5 -0
  60. data/spec/dummy/config/routes.rb +58 -0
  61. data/spec/dummy/db/seeds.rb +0 -0
  62. data/spec/dummy/db/test.sqlite3 +0 -0
  63. data/spec/dummy/doc/README_FOR_APP +2 -0
  64. data/spec/dummy/log/development.log +0 -0
  65. data/spec/dummy/log/test.log +2909 -0
  66. data/spec/dummy/public/404.html +26 -0
  67. data/spec/dummy/public/422.html +26 -0
  68. data/spec/dummy/public/500.html +26 -0
  69. data/spec/dummy/public/favicon.ico +0 -0
  70. data/spec/dummy/public/index.html +241 -0
  71. data/spec/dummy/public/robots.txt +5 -0
  72. data/spec/dummy/script/rails +6 -0
  73. data/spec/mail_stats/interceptors/mail_stats_interceptor_spec.rb +28 -0
  74. data/spec/mail_stats/mail_stats_spec.rb +7 -0
  75. data/spec/models/mail_stats/link_spec.rb +10 -0
  76. data/spec/models/mail_stats/pixel_spec.rb +6 -0
  77. data/spec/models/mail_stats/sent_mail_spec.rb +11 -0
  78. data/spec/models/mail_stats/visit_spec.rb +7 -0
  79. data/spec/spec_helper.rb +33 -0
  80. data/spec/support/basic_model_behavior.rb +6 -0
  81. data/spec/support/code_not_found_behavior.rb +18 -0
  82. data/spec/support/model_with_visits_behavior.rb +8 -0
  83. data/spec/support/regex_constants.rb +2 -0
  84. metadata +235 -0
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format d
data/.rvmrc ADDED
@@ -0,0 +1,4 @@
1
+ rvm_install_on_use_flag=1
2
+ rvm_make_flags="-j 3"
3
+ rvm_gemset_create_on_use_flag=1
4
+ rvm ruby-1.9.3@mail_stats_gem
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in mail_stats.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use debugger
14
+ # gem 'ruby-debug'
data/Gemfile.lock ADDED
@@ -0,0 +1,124 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mail_stats (0.0.3)
5
+ base62 (~> 0.1.4)
6
+ nokogiri (~> 1.5.0)
7
+ rails (~> 3.1.1)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ actionmailer (3.1.1)
13
+ actionpack (= 3.1.1)
14
+ mail (~> 2.3.0)
15
+ actionpack (3.1.1)
16
+ activemodel (= 3.1.1)
17
+ activesupport (= 3.1.1)
18
+ builder (~> 3.0.0)
19
+ erubis (~> 2.7.0)
20
+ i18n (~> 0.6)
21
+ rack (~> 1.3.2)
22
+ rack-cache (~> 1.1)
23
+ rack-mount (~> 0.8.2)
24
+ rack-test (~> 0.6.1)
25
+ sprockets (~> 2.0.2)
26
+ activemodel (3.1.1)
27
+ activesupport (= 3.1.1)
28
+ builder (~> 3.0.0)
29
+ i18n (~> 0.6)
30
+ activerecord (3.1.1)
31
+ activemodel (= 3.1.1)
32
+ activesupport (= 3.1.1)
33
+ arel (~> 2.2.1)
34
+ tzinfo (~> 0.3.29)
35
+ activeresource (3.1.1)
36
+ activemodel (= 3.1.1)
37
+ activesupport (= 3.1.1)
38
+ activesupport (3.1.1)
39
+ multi_json (~> 1.0)
40
+ arel (2.2.1)
41
+ base62 (0.1.4)
42
+ builder (3.0.0)
43
+ database_cleaner (0.6.7)
44
+ diff-lcs (1.1.3)
45
+ email_spec (1.2.1)
46
+ mail (~> 2.2)
47
+ rspec (~> 2.0)
48
+ erubis (2.7.0)
49
+ hike (1.2.1)
50
+ i18n (0.6.0)
51
+ json (1.6.1)
52
+ mail (2.3.0)
53
+ i18n (>= 0.4.0)
54
+ mime-types (~> 1.16)
55
+ treetop (~> 1.4.8)
56
+ mime-types (1.16)
57
+ multi_json (1.0.3)
58
+ nokogiri (1.5.0)
59
+ polyglot (0.3.2)
60
+ rack (1.3.4)
61
+ rack-cache (1.1)
62
+ rack (>= 0.4)
63
+ rack-mount (0.8.3)
64
+ rack (>= 1.0.0)
65
+ rack-ssl (1.3.2)
66
+ rack
67
+ rack-test (0.6.1)
68
+ rack (>= 1.0)
69
+ rails (3.1.1)
70
+ actionmailer (= 3.1.1)
71
+ actionpack (= 3.1.1)
72
+ activerecord (= 3.1.1)
73
+ activeresource (= 3.1.1)
74
+ activesupport (= 3.1.1)
75
+ bundler (~> 1.0)
76
+ railties (= 3.1.1)
77
+ railties (3.1.1)
78
+ actionpack (= 3.1.1)
79
+ activesupport (= 3.1.1)
80
+ rack-ssl (~> 1.3.2)
81
+ rake (>= 0.8.7)
82
+ rdoc (~> 3.4)
83
+ thor (~> 0.14.6)
84
+ rake (0.9.2)
85
+ rcov (0.9.11)
86
+ rdoc (3.10)
87
+ json (~> 1.4)
88
+ rspec (2.6.0)
89
+ rspec-core (~> 2.6.0)
90
+ rspec-expectations (~> 2.6.0)
91
+ rspec-mocks (~> 2.6.0)
92
+ rspec-core (2.6.4)
93
+ rspec-expectations (2.6.0)
94
+ diff-lcs (~> 1.1.2)
95
+ rspec-mocks (2.6.0)
96
+ rspec-rails (2.6.1)
97
+ actionpack (~> 3.0)
98
+ activesupport (~> 3.0)
99
+ railties (~> 3.0)
100
+ rspec (~> 2.6.0)
101
+ shoulda-matchers (1.0.0.beta3)
102
+ sprockets (2.0.2)
103
+ hike (~> 1.2)
104
+ rack (~> 1.0)
105
+ tilt (~> 1.1, != 1.3.0)
106
+ sqlite3 (1.3.4)
107
+ thor (0.14.6)
108
+ tilt (1.3.3)
109
+ treetop (1.4.10)
110
+ polyglot
111
+ polyglot (>= 0.3.1)
112
+ tzinfo (0.3.30)
113
+
114
+ PLATFORMS
115
+ ruby
116
+
117
+ DEPENDENCIES
118
+ database_cleaner (~> 0.6.7)
119
+ email_spec (~> 1.2.1)
120
+ mail_stats!
121
+ rcov (~> 0.9.1)
122
+ rspec-rails (~> 2.6.1)
123
+ shoulda-matchers (~> 1.0.0.beta3)
124
+ sqlite3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Max Riveiro <kavu13@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ = What is MailStats?
2
+
3
+ MailStats is simple Proof of Concept gem providing simple mail statistics with a help of invisible pixel in e-mail body and replacing all links with special refering links.
4
+
5
+ No viewing interfaces, just the inner logic.
6
+
7
+ == Installation
8
+
9
+ == Contributing to MailStats
10
+
11
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
12
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
13
+ * Fork the project
14
+ * Start a feature/bugfix branch
15
+ * Commit and push until you are happy with your contribution
16
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
17
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
18
+
19
+ == Copyright
20
+
21
+ Copyright (c) 2012 Max Riveiro. See MIT-LICENSE for further details.
22
+
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'MailStats'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ require 'rspec/core'
24
+ require 'rspec/core/rake_task'
25
+
26
+ RSpec::Core::RakeTask.new(:spec) do |spec|
27
+ spec.pattern = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ spec.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/,features\/}
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,32 @@
1
+ module MailStats
2
+ class ApplicationController < ActionController::Base
3
+ def process_pixel
4
+ params[:code] ? process_visit(params[:code]) : redirect_to(rescued_root_url)
5
+ end
6
+ alias :process_link :process_pixel
7
+
8
+ private
9
+
10
+ def visit(type, code)
11
+ user_id = begin
12
+ current_user.id
13
+ rescue NameError
14
+ nil
15
+ end
16
+ object = "MailStats::#{type.camelize}".constantize.find code.base62_decode
17
+ object.visits << MailStats::Visit.new(:user_id => user_id)
18
+ object.save!
19
+
20
+ object
21
+ end
22
+
23
+ #TODO: Write spec for root
24
+ def rescued_root_url
25
+ url = begin
26
+ root_url
27
+ rescue Exception
28
+ "/"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ module MailStats
2
+ class LinkController < ApplicationController
3
+
4
+ private
5
+
6
+ def process_visit(code)
7
+ begin
8
+ link = visit("link", code)
9
+ redirect_to link.url
10
+ rescue ActiveRecord::RecordNotFound, NoMethodError
11
+ render :nothing => true, :status => :not_found
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module MailStats
2
+ class PixelController < ApplicationController
3
+
4
+ private
5
+
6
+ def process_visit(code)
7
+ begin
8
+ visit("pixel", code)
9
+ render :nothing => true, :status => :no_content
10
+ rescue ActiveRecord::RecordNotFound, NoMethodError
11
+ render :nothing => true, :status => :not_found
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ module MailStats
2
+ class Link < ActiveRecord::Base
3
+ attr_readonly :visits_count
4
+
5
+ belongs_to :sent_mail, :counter_cache => true
6
+ has_many :visits, :as => :visitable
7
+ validates :url, :visits_count, :presence => true
8
+ validates :url, :uniqueness => true
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module MailStats
2
+ class Pixel < ActiveRecord::Base
3
+ attr_readonly :visits_count
4
+
5
+ belongs_to :sent_mail
6
+ has_many :visits, :as => :visitable
7
+ validates :visits_count, :presence => true
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module MailStats
2
+ class SentMail < ActiveRecord::Base
3
+ has_one :pixel
4
+ has_many :links
5
+ validates :subject, :recipients, :recipients_count, :links_count, :presence => true
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module MailStats
2
+ class Visit < ActiveRecord::Base
3
+ belongs_to :user
4
+ belongs_to :visitable, :polymorphic => true, :counter_cache => true
5
+ end
6
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ MailStats::Engine.routes.draw do
2
+ match "/p(/:code)" => "pixel#process_pixel", :as => 'process_pixel'
3
+ match "/r/(:code)" => "link#process_link", :as => 'process_link'
4
+ end
@@ -0,0 +1,10 @@
1
+ class CreateMailStatsLinks < ActiveRecord::Migration
2
+ def change
3
+ create_table :mail_stats_links do |t|
4
+ t.references :sent_mail
5
+ t.string :url, :null => false
6
+ t.integer :visits_count, :default => 0, :null => false
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class CreateMailStatsPixels < ActiveRecord::Migration
2
+ def change
3
+ create_table :mail_stats_pixels do |t|
4
+ t.references :sent_mail
5
+ t.integer :visits_count, :default => 0, :null => false
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class CreateMailStatsSentMails < ActiveRecord::Migration
2
+ def change
3
+ create_table :mail_stats_sent_mails do |t|
4
+ t.string :subject, :null => false
5
+ t.text :recipients, :null => false
6
+ t.integer :recipients_count, :default => 0, :null => false
7
+ t.integer :links_count, :default => 0, :null => false
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class CreateMailStatsVisits < ActiveRecord::Migration
2
+ def change
3
+ create_table :mail_stats_visits do |t|
4
+ t.references :user
5
+ t.references :visitable, :polymorphic => true
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates simple MailStats gem config initializer and copies MailsStats gem migrations
3
+
4
+ Example:
5
+ rails generate mail_stats:install
6
+
7
+ This will create:
8
+ config/initializers/mail_stats.rb
@@ -0,0 +1,11 @@
1
+ class MailStats::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path("../templates", __FILE__)
3
+
4
+ def create_initializer_file
5
+ copy_file "initializer.rb", "config/initializers/mail_stats.rb"
6
+ end
7
+
8
+ def copy_migrations
9
+ rake "mail_stats:install:migrations"
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ #### MailStats Gem Configuration
2
+
3
+ ### Default Values
4
+ ## Basic URL for generation Redirect Links and Tracking Pixel
5
+ # MailStats::Base.base_url = "http://localhost:3000/"
data/lib/mail_stats.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "nokogiri"
2
+ require "base62"
3
+
4
+ require "mail_stats/engine"
5
+ require "mail_stats/base"
6
+ require "mail_stats/interceptors/interceptor_prototype"
7
+ require "mail_stats/interceptors/mail_stats_interceptor"
8
+
9
+ ActionMailer::Base.register_interceptor(MailStats::MailStatsInterceptor)
@@ -0,0 +1,7 @@
1
+ module MailStats
2
+ class Base
3
+ class<<self; attr_accessor :base_url end
4
+
5
+ self.base_url = "http://localhost:3000/"
6
+ end
7
+ end