heatmap-rails 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.
@@ -0,0 +1,17 @@
1
+ class PointsController < ApplicationController
2
+ protect_from_forgery with: :null_session
3
+ skip_before_action :verify_authenticity_token
4
+
5
+ def create
6
+ if params[:data].present?
7
+ for i in 0..Heatmap::Rails.options[:click]-1
8
+ HeatMap.create(path: params[:data]["#{i}"][:path], click_type: 'click',xpath: params[:data]["#{i}"][:xpath])
9
+ end
10
+ end
11
+ if params[:move_data].present?
12
+ for i in 0..Heatmap::Rails.options[:move]-1
13
+ HeatMap.create(path: params[:move_data]["#{i}"][:path], click_type: 'click',xpath: params[:move_data]["#{i}"][:xpath])
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,2 @@
1
+ class HeatMap < ActiveRecord::Base
2
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "heatmap-rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ resources :points
3
+ end
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "heatmap/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "heatmap-rails"
8
+ spec.version = Heatmap::VERSION
9
+ spec.authors = ["Hassan"]
10
+ spec.email = ["hassan@qbatch.com"]
11
+
12
+ spec.summary = %q{HeatMap Gem}
13
+ spec.description = %q{Get And Display HeatMap Coordinates}
14
+ spec.homepage = "https://github.com/Qbatch/heatmap-rails"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "'TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0")
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.16.a"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/base'
4
+
5
+ module HeatmapRails
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def self.next_migration_number(path)
11
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ def create_model_file
15
+ migration_template "initializer.rb", "db/migrate/create_heat_maps.rb"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ class CreateHeatMaps < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :heat_maps do |t|
4
+ t.string :path
5
+ t.string :click_type
6
+ t.text :xpath
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require "heatmap/version"
2
+ require "heatmap/engine"
3
+ require "heatmap/helper"
4
+
5
+ module Heatmap
6
+ module Rails
7
+ class << self
8
+ attr_accessor :options
9
+ end
10
+ self.options = {click: 3, move: 10}
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Heatmap
2
+ class Engine < Rails::Engine
3
+ initializer "helper" do
4
+ ActiveSupport.on_load(:action_view) do
5
+ include Helper
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,143 @@
1
+ require "erb"
2
+
3
+ module Heatmap
4
+ module Helper
5
+
6
+ def save_heatmap(options = {})
7
+
8
+ click = options[:click] || Heatmap::Rails.options[:click]
9
+ move = options[:move] || Heatmap::Rails.options[:move]
10
+
11
+ html = ""
12
+
13
+ js = <<JS
14
+ <script type="text/javascript">
15
+ $( document ).ready(function() {
16
+ var move_array = [];
17
+ document.querySelector('body').onmousemove = function(ev) {
18
+ var xpath_element = xpathstring(ev);
19
+ var pageCoords = { path: window.location.pathname, type: 'move', xpath: xpath_element };
20
+ console.log(xpath_element);
21
+ var obj = move_array.find(function (obj) { return obj.xpath === xpath_element; });
22
+ if (obj == null){
23
+ move_array.push(pageCoords);
24
+ }
25
+ if (move_array.length >= parseInt(#{move}))
26
+ {
27
+ var coordinates = move_array;
28
+ sendRequest({'move_data': coordinates});
29
+ move_array = [];
30
+ }
31
+ };
32
+ var click_array = [];
33
+ document.querySelector('body').onclick = function(ev) {
34
+
35
+ var xpath_element= xpathstring(ev);
36
+ var pageCoords = { path: window.location.pathname, type: 'click', xpath: xpath_element };
37
+ click_array.push(pageCoords);
38
+ if (click_array.length >= parseInt(#{click}))
39
+ {
40
+ var coordinates = click_array;
41
+ sendRequest({'data': coordinates});
42
+ click_array = [];
43
+ }
44
+ };
45
+ function sendRequest(coordinates_data){
46
+ $.ajax({
47
+ method: "POST",
48
+ url: '/points',
49
+ data: coordinates_data,
50
+ dataType: 'application/json'
51
+ });
52
+ }
53
+ });
54
+
55
+ function xpathstring(event) {
56
+ var
57
+ e = event.srcElement || event.originalTarget,
58
+ path = xpath(e, '');;
59
+ return path
60
+ }
61
+ function xpath(element, suffix) {
62
+ var parent, child_index, node_name;
63
+ parent = element.parentElement;
64
+ if (parent) {
65
+ node_name = element.nodeName.toLowerCase();
66
+ child_index = nodeindex(element, parent.children) + 1;
67
+ return xpath(parent, '/' + node_name + '[' + child_index + ']' + suffix);
68
+ } else {
69
+ return '//html[1]' + suffix;
70
+ }
71
+ }
72
+ function nodeindex(element, array) {
73
+ var i,
74
+ found = -1,
75
+ element_name = element.nodeName.toLowerCase(),
76
+ matched
77
+ ;
78
+
79
+ for (i = 0; i != array.length; ++i) {
80
+ matched = array[i];
81
+ if (matched.nodeName.toLowerCase() === element_name) {
82
+ ++found;
83
+
84
+
85
+ if (matched === element) {
86
+ return found;
87
+ }
88
+ }
89
+ }
90
+
91
+ return -1;
92
+ }
93
+ function getOffset( path ) {
94
+ el = document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
95
+ var _x = 0;
96
+ var _y = 0;
97
+ while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
98
+ _x += el.offsetLeft - el.scrollLeft;
99
+ _y += el.offsetTop - el.scrollTop;
100
+ el = el.offsetParent;
101
+ }
102
+ return { y: _y, x: _x };
103
+ }
104
+ </script>
105
+ JS
106
+
107
+ html += js
108
+ html.respond_to?(:html_safe) ? html.html_safe : html
109
+ end
110
+
111
+ def show_heatmap(path)
112
+ heatmap = HeatMap.where(path: path.to_s)
113
+ @data_points = []
114
+ @data_xpaths = []
115
+ heatmap.each do |coordinate|
116
+ @data_xpaths.push({xpath: coordinate.xpath, x: 0, y:0, value: 100})
117
+ end
118
+ html = ""
119
+ js = <<JS
120
+ <script type="text/javascript">
121
+ var heatmapInstance = h337.create({
122
+ container: document.querySelector('body'),
123
+ radius: 40
124
+ });
125
+ var xpath = JSON.parse('#{raw(@data_xpaths.to_json.html_safe)}');
126
+ var data_xpath = xpath.map(function(path){
127
+ var x_coord = getOffset(path.xpath).x+25;
128
+ var y_coord = getOffset(path.xpath).y+25;
129
+ delete path["xpath"];
130
+ path.x = parseInt(x_coord);
131
+ path.y = parseInt(y_coord);
132
+ return path;
133
+ });
134
+ heatmapInstance.addData(data_xpath);
135
+ </script>
136
+ JS
137
+
138
+ html += js
139
+ html.respond_to?(:html_safe) ? html.html_safe : html
140
+ end
141
+
142
+ end
143
+ end
@@ -0,0 +1,3 @@
1
+ module Heatmap
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ RSpec.describe HeatmapRb do
2
+ it "has a version number" do
3
+ expect(HeatmapRb::VERSION).not_to be nil
4
+ end
5
+
6
+ it "does something useful" do
7
+ expect(false).to eq(true)
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ require "bundler/setup"
2
+ require "heatmap-rails"
3
+
4
+ RSpec.configure do |config|
5
+ # Enable flags like --only-failures and --next-failure
6
+ config.example_status_persistence_file_path = ".rspec_status"
7
+
8
+ # Disable RSpec exposing methods globally on `Module` and `main`
9
+ config.disable_monkey_patching!
10
+
11
+ config.expect_with :rspec do |c|
12
+ c.syntax = :expect
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heatmap-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hassan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.16.a
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.16.a
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Get And Display HeatMap Coordinates
56
+ email:
57
+ - hassan@qbatch.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - app/assets/javascripts/heatmap.js
71
+ - app/controllers/points_controller.rb
72
+ - app/models/heat_map.rb
73
+ - bin/console
74
+ - bin/setup
75
+ - config/routes.rb
76
+ - heatmap-rails.gemspec
77
+ - lib/generators/heatmap_rails/install_generator.rb
78
+ - lib/generators/heatmap_rails/templates/initializer.rb
79
+ - lib/heatmap-rails.rb
80
+ - lib/heatmap/engine.rb
81
+ - lib/heatmap/helper.rb
82
+ - lib/heatmap/version.rb
83
+ - spec/heatmap_rb_spec.rb
84
+ - spec/spec_helper.rb
85
+ homepage: https://github.com/Qbatch/heatmap-rails
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.6.12
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: HeatMap Gem
109
+ test_files: []