track_rate 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fe55c59824483926030241646b4ed5e8dfbd6d29
4
+ data.tar.gz: dd91f47cd419d0834437e38970cc806e9a5017f5
5
+ SHA512:
6
+ metadata.gz: f907b85395d00d2c261f024d2d9337583db62e536dcf02b824b0a852477b247768a03021b9fb2162630f32cf13234462c9ff00335b18c5b7cc41427e35049353
7
+ data.tar.gz: 192d49ff0a53a77a3c93a0caa5ef9a195387c954341ffffd766bf6a3010f215ce1b2e6a38494012eae6946d4f7f3fc62d253d10428679fc51c5e6e436f837974
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Craig Sheen
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.md ADDED
@@ -0,0 +1,47 @@
1
+ # TrackRate
2
+ Keep track of open/click rates
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'track_rate'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install track_rate
19
+ ```
20
+
21
+ Add this to your `config/routes.rb`
22
+ ```ruby
23
+ mount TrackRate::Engine, at: "/track_rate"
24
+ ```
25
+
26
+ When you send you will need to add a call to track the send. When you do this pass the `tracking_object` for example an instance of user and a `service_key` for example `email`.
27
+ ```ruby
28
+ TrackRate::ProcessSend.perform(user, service_key)
29
+ ```
30
+
31
+ Finally set up your provider to make post requests when an event occurs.
32
+
33
+ SendGrid: https://sendgrid.com/docs/API_Reference/Webhooks/event.html with webhook URL as `<www.mywebsite.com>/send_grid/open`
34
+
35
+ ## Currently supports
36
+
37
+ ### Email
38
+
39
+ Using the `service_key` as `email`
40
+
41
+ - SendGrid
42
+
43
+ ## Contributing
44
+ PRs to track other providers welcome!
45
+
46
+ ## License
47
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'TrackRate'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/track_rate .js
2
+ //= link_directory ../stylesheets/track_rate .css
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,9 @@
1
+ module TrackRate
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+
5
+ def tracking_object
6
+ TrackRate.tracking_object
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module TrackRate
2
+ class SendGridsController < ApplicationController
3
+ def open
4
+ tracking_object = tracking_class.find_by(sanitized_params[:email])
5
+ ProcessOpen.perform(tracking_object, 'email')
6
+ end
7
+
8
+ private
9
+
10
+ def sanitized_params
11
+ params.permit(:email)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module TrackRate
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module TrackRate
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module TrackRate
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module TrackRate
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module TrackRate
2
+ class TrackRate < ApplicationRecord
3
+ belongs_to :tracking_object, polymorphic: true
4
+
5
+ validates :tracking_service, presence: true
6
+
7
+ def self.for(object, service)
8
+ find_or_intialize_by(
9
+ tracking_object_type: object.class.name, tracking_object_id: object.id,
10
+ tracking_service: service
11
+ )
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,31 @@
1
+ module TrackRate
2
+ class ProcessOpen
3
+ def initialize(object, service)
4
+ self.object = object
5
+ self.service = service
6
+ end
7
+
8
+ def self.perform(object, service)
9
+ new.perform(object, service)
10
+ end
11
+
12
+ def perform
13
+ object.update_attributes(
14
+ open_count: new_open_count,
15
+ open_percent: new_open_percent
16
+ )
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :object, :service
22
+
23
+ def new_open_count
24
+ object.open_count + 1
25
+ end
26
+
27
+ def new_open_percent
28
+ (new_open_count / object.send_count) * 100
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ module TrackRate
2
+ class ProcessSend
3
+ def initialize(object, service)
4
+ self.object = object
5
+ self.service = service
6
+ end
7
+
8
+ def self.perform(object, service)
9
+ new.perform(object, service)
10
+ end
11
+
12
+ def perform
13
+ object.update_attributes(
14
+ send_count: new_send_count,
15
+ open_percent: new_open_percent
16
+ )
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :object, :service
22
+
23
+ def new_send_count
24
+ object.send_count + 1
25
+ end
26
+
27
+ def new_open_percent
28
+ (object.open_count / new_send_count) * 100
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Track rate</title>
5
+ <%= stylesheet_link_tag "track_rate/application", media: "all" %>
6
+ <%= javascript_include_tag "track_rate/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,5 @@
1
+ module TrackRate
2
+ def self.tracking_object
3
+ 'User'
4
+ end
5
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ TrackRate::Engine.routes.draw do
2
+ namespace :send_grid do
3
+ post 'open' => 'send_grid#open'
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ class CreateTrackRates < ActiveRecord::Migration
2
+ def change
3
+ create_table :track_rates do |t|
4
+ t.string :tracking_object_type
5
+ t.integer :tracking_object_id
6
+ t.string :tracking_service
7
+ t.integer :send_count, default: 0
8
+ t.integer :open_count, default: 0
9
+ t.integer :open_percent, default: 0
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :track_rate, [:tracking_object_type, :tracking_object_id, :tracking_service]
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :track_rate do
3
+ # # Task goes here
4
+ # end
data/lib/track_rate.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'track_rate/engine'
2
+
3
+ module TrackRate
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,5 @@
1
+ module TrackRate
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace TrackRate
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module TrackRate
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: track_rate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Craig Sheen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Keep track of open/click rates
42
+ email:
43
+ - craig_sheen@hotmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/assets/config/track_rate_manifest.js
52
+ - app/assets/javascripts/track_rate/application.js
53
+ - app/assets/stylesheets/track_rate/application.css
54
+ - app/controllers/track_rate/application_controller.rb
55
+ - app/controllers/track_rate/send_grids_controller.rb
56
+ - app/helpers/track_rate/application_helper.rb
57
+ - app/jobs/track_rate/application_job.rb
58
+ - app/mailers/track_rate/application_mailer.rb
59
+ - app/models/track_rate/application_record.rb
60
+ - app/models/track_rate/track_rate.rb
61
+ - app/services/process_open.rb
62
+ - app/services/process_send.rb
63
+ - app/views/layouts/track_rate/application.html.erb
64
+ - config/initializers/tracking_object.rb
65
+ - config/routes.rb
66
+ - db/migrate/20171118031539_create_track_rate_track_rates.rb
67
+ - lib/tasks/track_rate_tasks.rake
68
+ - lib/track_rate.rb
69
+ - lib/track_rate/engine.rb
70
+ - lib/track_rate/version.rb
71
+ homepage: http://github.com/craigsheen/track_rate
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.5.1
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Track usage rates
95
+ test_files: []