impressiongram 0.0.4 → 0.0.5
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/Rakefile +1 -1
- data/VERSION +1 -1
- data/app/controllers/impressionist_controller.rb +68 -0
- data/app/models/impressionist/bots.rb +1462 -0
- data/app/models/impressionist/impressionable.rb +68 -0
- data/lib/impressionist.rb +5 -0
- data/lib/impressionist/bots.rb +18 -0
- data/lib/impressionist/engine.rb +17 -0
- data/lib/impressionist/railties/tasks.rake +0 -0
- metadata +8 -1
@@ -0,0 +1,68 @@
|
|
1
|
+
module Impressionist
|
2
|
+
module Impressionable
|
3
|
+
def is_impressionable
|
4
|
+
has_many :impressions, :as=>:impressionable
|
5
|
+
include InstanceMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module InstanceMethods
|
9
|
+
def impressionable?
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
def impressionist_count(options={})
|
14
|
+
options.reverse_merge!(:filter=>:request_hash, :start_date=>nil, :end_date=>Time.now)
|
15
|
+
imps = options[:start_date].blank? ? impressions : impressions.where("created_at>=? and created_at<=?",options[:start_date],options[:end_date])
|
16
|
+
if options[:filter]!=:all
|
17
|
+
imps = imps.select(options[:filter]).group(options[:filter])
|
18
|
+
end
|
19
|
+
imps.all.size
|
20
|
+
end
|
21
|
+
|
22
|
+
def impression_gram(millisecond=false, time_format="seconds", graph_label="", minute_increments, hours_covered, hour_offset)
|
23
|
+
time_formatter = case time_format
|
24
|
+
when "seconds" then "%s"
|
25
|
+
when "hour/minute" then "%h:%M %p"
|
26
|
+
end
|
27
|
+
def key_formatter(millisecond, time_formatter, minute_increments)
|
28
|
+
key = minute_increments.minutes.ago.strftime(time_formatter).to_i
|
29
|
+
if millisecond
|
30
|
+
key = key*1000
|
31
|
+
end
|
32
|
+
return key
|
33
|
+
end
|
34
|
+
hits_label = {:label => graph_label}
|
35
|
+
hits_by_interval = []
|
36
|
+
minute = 0 + offset*60
|
37
|
+
while minute < (offset+60*hours_covered)
|
38
|
+
if minute == offset*60
|
39
|
+
hits_by_interval << [ key_formatter(millisecond, time_formatter, minute_increments), impressions.where("created_at >= ?", minute_increments.minutes.ago).count]
|
40
|
+
else
|
41
|
+
hits_by_interval << [ key_formatter(millisecond, time_formatter, (minute_increments+minute)).minute.ago.strftime("%s").to_i*1000, impressions.where("created_at >= ?", (minute_increments+minute).minutes.ago).where("created_at <= ?", minute.minutes.ago).count ]
|
42
|
+
end
|
43
|
+
minute += minute_increments
|
44
|
+
end
|
45
|
+
hits_hash = {:data => hits_by_hour}
|
46
|
+
return hits_label.merge!(hits_hash)
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
# OLD METHODS - DEPRECATE IN V0.5
|
51
|
+
def impression_count(start_date=nil,end_date=Time.now)
|
52
|
+
impressionist_count({:start_date=>start_date, :end_date=>end_date, :filter=>:all})
|
53
|
+
end
|
54
|
+
|
55
|
+
def unique_impression_count(start_date=nil,end_date=Time.now)
|
56
|
+
impressionist_count({:start_date=>start_date, :end_date=>end_date, :filter=> :request_hash})
|
57
|
+
end
|
58
|
+
|
59
|
+
def unique_impression_count_ip(start_date=nil,end_date=Time.now)
|
60
|
+
impressionist_count({:start_date=>start_date, :end_date=>end_date, :filter=> :ip_address})
|
61
|
+
end
|
62
|
+
|
63
|
+
def unique_impression_count_session(start_date=nil,end_date=Time.now)
|
64
|
+
impressionist_count({:start_date=>start_date, :end_date=>end_date, :filter=> :session_hash})
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
module Impressionist
|
5
|
+
module Bots
|
6
|
+
LIST_URL = "http://www.user-agents.org/allagents.xml"
|
7
|
+
def self.consume
|
8
|
+
response = HTTPClient.new.get_content(LIST_URL)
|
9
|
+
doc = Nokogiri::XML(response)
|
10
|
+
list = []
|
11
|
+
doc.xpath('//user-agent').each do |agent|
|
12
|
+
type = agent.xpath("Type").text
|
13
|
+
list << agent.xpath("String").text.gsub("<","<") if ["R","S"].include?(type) #gsub hack for badly formatted data
|
14
|
+
end
|
15
|
+
list
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "impressionist"
|
2
|
+
require "rails"
|
3
|
+
|
4
|
+
module Impressionist
|
5
|
+
class Engine < Rails::Engine
|
6
|
+
initializer 'impressionist.extend_ar' do |app|
|
7
|
+
ActiveRecord::Base.extend Impressionist::Impressionable
|
8
|
+
end
|
9
|
+
|
10
|
+
initializer 'impressionist.controller' do
|
11
|
+
ActiveSupport.on_load(:action_controller) do
|
12
|
+
include ImpressionistController::InstanceMethods
|
13
|
+
extend ImpressionistController::ClassMethods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
File without changes
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: impressiongram
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- paulkaplan
|
@@ -94,10 +94,17 @@ files:
|
|
94
94
|
- README.md
|
95
95
|
- Rakefile
|
96
96
|
- VERSION
|
97
|
+
- app/controllers/impressionist_controller.rb
|
97
98
|
- app/models/impression.rb
|
99
|
+
- app/models/impressionist/bots.rb
|
100
|
+
- app/models/impressionist/impressionable.rb
|
98
101
|
- config/routes.rb
|
99
102
|
- lib/generators/impressionist/impressionist_generator.rb
|
100
103
|
- lib/generators/impressionist/templates/create_impressions_table.rb
|
104
|
+
- lib/impressionist.rb
|
105
|
+
- lib/impressionist/bots.rb
|
106
|
+
- lib/impressionist/engine.rb
|
107
|
+
- lib/impressionist/railties/tasks.rake
|
101
108
|
- logo.png
|
102
109
|
- upgrade_migrations/version_0_3_0.rb
|
103
110
|
- upgrade_migrations/version_0_4_0.rb
|