simple_email_tracker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/Rakefile +38 -0
  3. data/app/assets/images/t.gif +0 -0
  4. data/app/controllers/email_trackers_controller.rb +14 -0
  5. data/app/controllers/email_trackers_controller.rb~ +21 -0
  6. data/app/models/email_tracker.rb~ +27 -0
  7. data/app/models/visit.rb +29 -0
  8. data/app/models/visit.rb~ +27 -0
  9. data/app/views/simple_email_tracker/email_trackers/index.html.haml~ +40 -0
  10. data/config/routes.rb +4 -0
  11. data/config/routes.rb~ +4 -0
  12. data/lib/generators/simple_email_tracker/install_generator.rb +23 -0
  13. data/lib/generators/simple_email_tracker/install_generator.rb~ +23 -0
  14. data/lib/generators/simple_email_tracker/templates/migration.rb +17 -0
  15. data/lib/generators/simple_email_tracker/templates/migration.rb~ +17 -0
  16. data/lib/generators/simple_email_tracker/utils.rb +30 -0
  17. data/lib/generators/simple_email_tracker/utils.rb~ +30 -0
  18. data/lib/simple_email_tracker/engine.rb +19 -0
  19. data/lib/simple_email_tracker/engine.rb~ +14 -0
  20. data/lib/simple_email_tracker/version.rb +3 -0
  21. data/lib/simple_email_tracker/view_helper.rb +8 -0
  22. data/lib/simple_email_tracker/view_helper.rb~ +8 -0
  23. data/lib/simple_email_tracker.rb +3 -0
  24. data/lib/simple_email_tracker.rb~ +9 -0
  25. data/lib/tasks/simple_email_tracker_tasks.rake +6 -0
  26. data/lib/tasks/simple_email_tracker_tasks.rake~ +4 -0
  27. data/test/dummy/README.rdoc +261 -0
  28. data/test/dummy/Rakefile +7 -0
  29. data/test/dummy/app/assets/javascripts/application.js +15 -0
  30. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  31. data/test/dummy/app/controllers/application_controller.rb +3 -0
  32. data/test/dummy/app/helpers/application_helper.rb +2 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/test/dummy/config/application.rb +59 -0
  35. data/test/dummy/config/boot.rb +10 -0
  36. data/test/dummy/config/database.yml +25 -0
  37. data/test/dummy/config/environment.rb +5 -0
  38. data/test/dummy/config/environments/development.rb +37 -0
  39. data/test/dummy/config/environments/production.rb +67 -0
  40. data/test/dummy/config/environments/test.rb +37 -0
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/dummy/config/initializers/inflections.rb +15 -0
  43. data/test/dummy/config/initializers/mime_types.rb +5 -0
  44. data/test/dummy/config/initializers/secret_token.rb +7 -0
  45. data/test/dummy/config/initializers/session_store.rb +8 -0
  46. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/test/dummy/config/locales/en.yml +5 -0
  48. data/test/dummy/config/routes.rb +58 -0
  49. data/test/dummy/config.ru +4 -0
  50. data/test/dummy/public/404.html +26 -0
  51. data/test/dummy/public/422.html +26 -0
  52. data/test/dummy/public/500.html +25 -0
  53. data/test/dummy/public/favicon.ico +0 -0
  54. data/test/dummy/script/rails +6 -0
  55. data/test/simple_email_tracker_test.rb +7 -0
  56. data/test/test_helper.rb +15 -0
  57. metadata +195 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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/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 = 'SimpleEmailTracker'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
Binary file
@@ -0,0 +1,14 @@
1
+ module SimpleEmailTracker
2
+ class EmailTrackersController < ::ApplicationController
3
+
4
+ def show
5
+ v = Visit.find_by_uuid params[:uuid]
6
+ v.visit_by request
7
+ send_file(
8
+ File.expand_path("../../assets/images/t.gif", __FILE__),
9
+ filename: "t.gif",
10
+ type: "image/pdf")
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module SimpleEmailTracker
2
+ class EmailTrackersController < ::ApplicationController
3
+ skip_filter :authenticate_user!, only: [:show]
4
+
5
+ def show
6
+ et = EmailTracker.find_by_uuid params[:uuid]
7
+ et.visit_by request
8
+ send_file(
9
+ "#{Rails.root}/app/assets/images/standalone/t.gif",
10
+ filename: "t.gif",
11
+ type: "image/pdf")
12
+ end
13
+
14
+ def index
15
+ @trackers = EmailTracker.order('id desc')
16
+ @trackers = @trackers.where('email_trackers.key like ?', "%#{params[:search]}%") if params[:search]
17
+ @trackers = @trackers.paginate(page: params[:page], per_page: 20)
18
+ render layout: nil
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ require 'uuid'
2
+
3
+ module EmailTracker
4
+ class EmailTracker < ActiveRecord::Base
5
+ before_create :set_uuid
6
+
7
+ def self.get_by_key key
8
+ key = key.join(".") if key.kind_of? Array
9
+ et = EmailTracker.find_or_create_by_key key
10
+ end
11
+
12
+ def visit_by request
13
+ now = Time.now
14
+ self.count += 1
15
+ self.first_visited_at = now unless self.first_visited_at
16
+ self.last_visited_at = now
17
+ self.ip = request.ip
18
+ self.user_agent = request.env["HTTP_USER_AGENT"]
19
+ self.save
20
+ end
21
+
22
+ private
23
+ def set_uuid
24
+ self.uuid = UUID.new.generate
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ require 'uuid'
2
+
3
+ module SimpleEmailTracker
4
+ class Visit < ActiveRecord::Base
5
+ before_create :set_uuid
6
+
7
+ scope :search, lambda{ |key| where('simple_email_tracker_visits.key like ?', "%#{key}%") }
8
+
9
+ def self.get_by_key key
10
+ key = key.join(".") if key.kind_of? Array
11
+ et = self.find_or_create_by_key key
12
+ end
13
+
14
+ def visit_by request
15
+ now = Time.now
16
+ self.count += 1
17
+ self.first_visited_at = now unless self.first_visited_at
18
+ self.last_visited_at = now
19
+ self.ip = request.ip
20
+ self.user_agent = request.env["HTTP_USER_AGENT"]
21
+ self.save
22
+ end
23
+
24
+ private
25
+ def set_uuid
26
+ self.uuid = UUID.new.generate
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ require 'uuid'
2
+
3
+ module SimpleEmailTracker
4
+ class EmailTracker < ActiveRecord::Base
5
+ before_create :set_uuid
6
+
7
+ def self.get_by_key key
8
+ key = key.join(".") if key.kind_of? Array
9
+ et = EmailTracker.find_or_create_by_key key
10
+ end
11
+
12
+ def visit_by request
13
+ now = Time.now
14
+ self.count += 1
15
+ self.first_visited_at = now unless self.first_visited_at
16
+ self.last_visited_at = now
17
+ self.ip = request.ip
18
+ self.user_agent = request.env["HTTP_USER_AGENT"]
19
+ self.save
20
+ end
21
+
22
+ private
23
+ def set_uuid
24
+ self.uuid = UUID.new.generate
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ :css
2
+ table{border-collapse: collapse; margin-bottom: 1em}
3
+ td, th{ border: 1px solid #333; padding: 4px}
4
+
5
+ %p
6
+ = form_tag do
7
+ = text_field_tag :search, params[:search]
8
+ = submit_tag "search"
9
+
10
+ %table
11
+ %tr
12
+ %th count
13
+ %td= @trackers.count
14
+ %tr
15
+ %th visited
16
+ %td= @trackers.where('ip is not null').count
17
+
18
+ %p= will_paginate @trackers
19
+
20
+ %table
21
+ %tr
22
+ %th key
23
+ %th ip
24
+ %th created at
25
+ %th first visited at
26
+ %th last visited at
27
+ %th count
28
+ %th uuid
29
+ %th user agent
30
+
31
+ - @trackers.each do |et|
32
+ %tr
33
+ %td= et.key
34
+ %td= et.ip
35
+ %td= et.created_at.strftime "%Y-%m-%d %H:%M:%S"
36
+ %td= et.first_visited_at.strftime "%Y-%m-%d %H:%M:%S" if et.first_visited_at
37
+ %td= et.last_visited_at.strftime "%Y-%m-%d %H:%M:%S" if et.last_visited_at
38
+ %td= et.count
39
+ %td= et.uuid
40
+ %td= et.user_agent
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ SimpleEmailTracker::Engine.routes.draw do
2
+ match "/:uuid/t.gif", controller: :email_trackers, action: :show
3
+ match "/", controller: :email_trackers, action: :index
4
+ end
data/config/routes.rb~ ADDED
@@ -0,0 +1,4 @@
1
+ SimpleEmailTracker::Engine.routes.draw do
2
+ match "/email_trackers/:uuid/t.gif", controller: :email_tracker, action: :show
3
+ match "/email_trackers/", controller: :email_trackers, action: :index
4
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators'
2
+ require File.expand_path('../utils', __FILE__)
3
+
4
+ module SimpleEmailTracker
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+ include Rails::Generators::Migration
8
+ include Generators::Utils::InstanceMethods
9
+ extend Generators::Utils::ClassMethods
10
+
11
+ desc "simple email tracker installation generator"
12
+
13
+ def install
14
+ routes = File.open(Rails.root.join("config/routes.rb")).try :read
15
+
16
+ route("mount SimpleEmailTracker::Engine => '/email_trackers'")
17
+
18
+ display "Adding a migration..."
19
+ migration_template 'migration.rb', 'db/migrate/create_simple_email_tracker_tables.rb' rescue display $!.message
20
+ display "Job's done: migrate!", :blue
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators'
2
+ require File.expand_path('../utils', __FILE__)
3
+
4
+ module SimpleEmailTracker
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+ include Rails::Generators::Migration
8
+ include Generators::Utils::InstanceMethods
9
+ extend Generators::Utils::ClassMethods
10
+
11
+ desc "simple email tracker installation generator"
12
+
13
+ def install
14
+ routes = File.open(Rails.root.join("config/routes.rb")).try :read
15
+
16
+ route("mount SimpleEmailTracker => 'email_trackers'")
17
+
18
+ display "Adding a migration..."
19
+ migration_template 'migration.rb', 'db/migrate/create_simple_email_trackers_table.rb' rescue display $!.message
20
+ display "Job's done: migrate!", :blue
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ class CreateSimpleEmailTrackerTables < ActiveRecord::Migration
2
+ def change
3
+ create_table :simple_email_tracker_visits do |t|
4
+ t.string :uuid
5
+ t.string :key
6
+ t.string :ip
7
+ t.integer :count, default: 0
8
+ t.string :user_agent
9
+ t.datetime :first_visited_at
10
+ t.datetime :last_visited_at
11
+
12
+ t.timestamps
13
+ end
14
+ add_index :simple_email_tracker_visits, :uuid
15
+ add_index :simple_email_tracker_visits, :key
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ class CreateSimpleEmailTrackersTable < ActiveRecord::Migration
2
+ def change
3
+ create_table :email_trackers do |t|
4
+ t.string :uuid
5
+ t.string :key
6
+ t.string :ip
7
+ t.integer :count, default: 0
8
+ t.string :user_agent
9
+ t.datetime :first_visited_at
10
+ t.datetime :last_visited_at
11
+
12
+ t.timestamps
13
+ end
14
+ add_index :email_trackers, :uuid
15
+ add_index :email_trackers, :key
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module SimpleEmailTracker
2
+ module Generators
3
+ module Utils
4
+ module InstanceMethods
5
+ def display(output, color = :green)
6
+ say(" - #{output}", color)
7
+ end
8
+
9
+ def ask_for(wording, default_value = nil, override_if_present_value = nil)
10
+ override_if_present_value.present? ?
11
+ display("Using [#{override_if_present_value}] for question '#{wording}'") && override_if_present_value :
12
+ ask(" ? #{wording} Press <enter> for [#{default_value}] >", :yellow).presence || default_value
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ def next_migration_number(dirname)
18
+ if ActiveRecord::Base.timestamped_migrations
19
+ migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
20
+ migration_number += 1
21
+ migration_number.to_s
22
+ else
23
+ "%.3d" % (current_migration_number(dirname) + 1)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,30 @@
1
+ module RailsAdmin
2
+ module Generators
3
+ module Utils
4
+ module InstanceMethods
5
+ def display(output, color = :green)
6
+ say(" - #{output}", color)
7
+ end
8
+
9
+ def ask_for(wording, default_value = nil, override_if_present_value = nil)
10
+ override_if_present_value.present? ?
11
+ display("Using [#{override_if_present_value}] for question '#{wording}'") && override_if_present_value :
12
+ ask(" ? #{wording} Press <enter> for [#{default_value}] >", :yellow).presence || default_value
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ def next_migration_number(dirname)
18
+ if ActiveRecord::Base.timestamped_migrations
19
+ migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
20
+ migration_number += 1
21
+ migration_number.to_s
22
+ else
23
+ "%.3d" % (current_migration_number(dirname) + 1)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,19 @@
1
+ module SimpleEmailTracker
2
+ class Engine < Rails::Engine
3
+ isolate_namespace SimpleEmailTracker
4
+
5
+ initializer 'helper' do |app|
6
+ # if defined?(ActionView::Base)
7
+ # ActionView::Base.send :include, SimpleEmailTracker::ViewHelper
8
+ # end
9
+
10
+ ActiveSupport.on_load(:action_view) do
11
+ include SimpleEmailTracker::ViewHelper
12
+ end
13
+ end
14
+
15
+ rake_tasks do
16
+ Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module SimpleEmailTracker
2
+ class Engine < Rails::Engine
3
+ initializer 'viewer helper' do
4
+ ActiveSupport.on_load(:action_view) do
5
+ raise "xxx"
6
+ include SimpleEmailTracker::ViewHelper
7
+ end
8
+ end
9
+
10
+ rake_tasks do
11
+ Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleEmailTracker
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ module SimpleEmailTracker
2
+ module ViewHelper
3
+ def set_email_tracker *args
4
+ et = SimpleEmailTracker::Visit.get_by_key args
5
+ image_tag "/email_trackers/#{et.uuid}/t.gif"
6
+ end
7
+ end
8
+ end