mongoid_traffic 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2c00bbcb80356f2546b251e0f48a2863dd8bfcc
4
- data.tar.gz: b8ee6c7cece3700d385118cbb0f3fbd925065047
3
+ metadata.gz: 48c3eb9a025f842258ed056e4ef1b435bc99c260
4
+ data.tar.gz: 78ccd5f6083cec8c081d217a0265e34c8abf3819
5
5
  SHA512:
6
- metadata.gz: a2026efe6cee6b0f1e491d914daf4d6c8f32b73edb5da304398148643865b5b193b8c4c97a0a8f33462c74c467c94108be8add4cf6b6aab724f665b105d2c387
7
- data.tar.gz: be0c1960d5d52002398e556ff397480dfac95dceb8c64c0a7aae4624208d1c2e67c06c439367fa27538858b6a2ecc48e0c6b77e6b7ee3ee2c64bd7059a831c96
6
+ metadata.gz: da181545b24d5cfd258d21bc71c44f28cd9ffcf0a693c9abee7bb43811c83315492a11f77ddf5ce8ccbaf53b1084d4316141b137d3e144bed06f9ad60b2451a3
7
+ data.tar.gz: 815f3e65d468f5a271f2bb1190e0bd373513356770606441f4e303f5c48a6aa3851571bf5395971f9a5372c10089b150962d1c1a44b24366f5ac921dab5e058f
data/.travis.yml CHANGED
@@ -1,8 +1,9 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
  script: 'bundle exec rake'
4
+ sudo: false
4
5
  rvm:
5
- - 2.1.2
6
+ - 2.2.2
6
7
  services:
7
8
  - mongodb
8
9
 
@@ -11,4 +12,4 @@ notifications:
11
12
  recipients:
12
13
  - tomas.celizna@gmail.com
13
14
  on_failure: change
14
- on_success: never
15
+ on_success: never
data/Gemfile CHANGED
@@ -1,9 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in mongoid_permalink_extension.gemspec
4
3
  gemspec
5
4
 
6
- # TEMP
7
- group :test do
8
- gem 'database_cleaner', git: 'https://github.com/DatabaseCleaner/database_cleaner.git'
9
- end
5
+ gem 'database_cleaner', git: 'https://github.com/DatabaseCleaner/database_cleaner.git'
data/Gemfile.lock CHANGED
@@ -1,103 +1,104 @@
1
1
  GIT
2
2
  remote: https://github.com/DatabaseCleaner/database_cleaner.git
3
- revision: b87f00320f8aa0f7e499d183128f05ce29cedc33
3
+ revision: f318781a77228f5999f5f2371c12f59089c141f3
4
4
  specs:
5
5
  database_cleaner (1.5.1)
6
6
 
7
7
  PATH
8
8
  remote: .
9
9
  specs:
10
- mongoid_traffic (0.2.0)
10
+ mongoid_traffic (0.2.1)
11
11
  geoip
12
- mongoid (~> 5.0)
12
+ mongoid (~> 5.1)
13
13
  nokogiri
14
- useragent (~> 0.10.0)
14
+ useragent
15
15
 
16
16
  GEM
17
17
  remote: https://rubygems.org/
18
18
  specs:
19
- activemodel (4.2.4)
20
- activesupport (= 4.2.4)
19
+ activemodel (4.2.6)
20
+ activesupport (= 4.2.6)
21
21
  builder (~> 3.1)
22
- activesupport (4.2.4)
22
+ activesupport (4.2.6)
23
23
  i18n (~> 0.7)
24
24
  json (~> 1.7, >= 1.7.7)
25
25
  minitest (~> 5.1)
26
26
  thread_safe (~> 0.3, >= 0.3.4)
27
27
  tzinfo (~> 1.1)
28
- bson (3.2.4)
28
+ bson (4.0.4)
29
29
  builder (3.2.2)
30
- celluloid (0.15.2)
31
- timers (~> 1.1.0)
32
- coderay (1.1.0)
33
- coveralls (0.7.0)
34
- multi_json (~> 1.3)
35
- rest-client
36
- simplecov (>= 0.7)
37
- term-ansicolor
38
- thor
30
+ coderay (1.1.1)
31
+ coveralls (0.8.13)
32
+ json (~> 1.8)
33
+ simplecov (~> 0.11.0)
34
+ term-ansicolor (~> 1.3)
35
+ thor (~> 0.19.1)
36
+ tins (~> 1.6.0)
39
37
  docile (1.1.5)
40
- ffi (1.9.3)
38
+ ffi (1.9.10)
41
39
  formatador (0.2.5)
42
40
  geoip (1.6.1)
43
- guard (2.6.1)
41
+ guard (2.13.0)
44
42
  formatador (>= 0.2.4)
45
- listen (~> 2.7)
43
+ listen (>= 2.7, <= 4.0)
46
44
  lumberjack (~> 1.0)
45
+ nenv (~> 0.1)
46
+ notiffany (~> 0.0)
47
47
  pry (>= 0.9.12)
48
+ shellany (~> 0.0)
48
49
  thor (>= 0.18.1)
49
- guard-minitest (2.3.1)
50
- guard (~> 2.0)
50
+ guard-compat (1.2.1)
51
+ guard-minitest (2.4.4)
52
+ guard-compat (~> 1.2)
51
53
  minitest (>= 3.0)
52
54
  i18n (0.7.0)
53
55
  json (1.8.3)
54
- listen (2.7.9)
55
- celluloid (>= 0.15.2)
56
+ listen (3.0.6)
56
57
  rb-fsevent (>= 0.9.3)
57
- rb-inotify (>= 0.9)
58
- lumberjack (1.0.9)
58
+ rb-inotify (>= 0.9.7)
59
+ lumberjack (1.0.10)
59
60
  method_source (0.8.2)
60
- mime-types (1.25.1)
61
- mini_portile (0.6.2)
62
- minitest (5.4.0)
63
- mongo (2.1.1)
64
- bson (~> 3.0)
65
- mongoid (5.0.0)
61
+ mini_portile2 (2.0.0)
62
+ minitest (5.8.4)
63
+ minitest-around (0.3.2)
64
+ minitest (~> 5.0)
65
+ mongo (2.2.4)
66
+ bson (~> 4.0)
67
+ mongoid (5.1.1)
66
68
  activemodel (~> 4.0)
67
69
  mongo (~> 2.1)
68
- origin (~> 2.1)
70
+ origin (~> 2.2)
69
71
  tzinfo (>= 0.3.37)
70
- multi_json (1.10.1)
71
- netrc (0.7.7)
72
- nokogiri (1.6.6.2)
73
- mini_portile (~> 0.6.0)
74
- origin (2.1.1)
75
- pry (0.10.0)
72
+ nenv (0.3.0)
73
+ nokogiri (1.6.7.2)
74
+ mini_portile2 (~> 2.0.0.rc2)
75
+ notiffany (0.0.8)
76
+ nenv (~> 0.1)
77
+ shellany (~> 0.0)
78
+ origin (2.2.0)
79
+ pry (0.10.3)
76
80
  coderay (~> 1.1.0)
77
81
  method_source (~> 0.8.1)
78
82
  slop (~> 3.4)
79
- rake (10.3.2)
80
- rb-fsevent (0.9.4)
81
- rb-inotify (0.9.5)
83
+ rake (11.1.1)
84
+ rb-fsevent (0.9.7)
85
+ rb-inotify (0.9.7)
82
86
  ffi (>= 0.5.0)
83
- rest-client (1.7.2)
84
- mime-types (>= 1.16, < 3.0)
85
- netrc (~> 0.7)
86
- simplecov (0.9.0)
87
+ shellany (0.0.1)
88
+ simplecov (0.11.2)
87
89
  docile (~> 1.1.0)
88
- multi_json
89
- simplecov-html (~> 0.8.0)
90
- simplecov-html (0.8.0)
90
+ json (~> 1.8)
91
+ simplecov-html (~> 0.10.0)
92
+ simplecov-html (0.10.0)
91
93
  slop (3.6.0)
92
- term-ansicolor (1.3.0)
94
+ term-ansicolor (1.3.2)
93
95
  tins (~> 1.0)
94
96
  thor (0.19.1)
95
97
  thread_safe (0.3.5)
96
- timers (1.1.0)
97
- tins (1.3.1)
98
+ tins (1.6.0)
98
99
  tzinfo (1.2.2)
99
100
  thread_safe (~> 0.1)
100
- useragent (0.10.0)
101
+ useragent (0.16.5)
101
102
 
102
103
  PLATFORMS
103
104
  ruby
@@ -109,5 +110,9 @@ DEPENDENCIES
109
110
  guard
110
111
  guard-minitest
111
112
  minitest
113
+ minitest-around
112
114
  mongoid_traffic!
113
115
  rake
116
+
117
+ BUNDLED WITH
118
+ 1.11.2
@@ -1,7 +1,6 @@
1
1
  module MongoidTraffic
2
2
  module ControllerAdditions
3
-
4
- def log_traffic log_cls, scope: nil
3
+ def log_traffic(log_cls, scope: nil)
5
4
  MongoidTraffic::Logger.log(
6
5
  log_cls,
7
6
  ip_address: request.remote_ip,
@@ -10,19 +9,18 @@ module MongoidTraffic
10
9
  user_agent: request.headers['User-Agent']
11
10
  )
12
11
  end
13
-
14
- def log_scoped_traffic log_cls, scope: nil
12
+
13
+ def log_scoped_traffic(_log_cls, scope: nil)
15
14
  log_traffic(scope: (scope || request.fullpath.split('?').first))
16
15
  end
17
16
 
18
17
  # ---------------------------------------------------------------------
19
-
20
- def self.included base
18
+
19
+ def self.included(base)
21
20
  base.extend ClassMethods
22
21
  base.helper_method :log_traffic
23
22
  base.helper_method :log_scoped_traffic
24
23
  end
25
-
26
24
  end
27
25
  end
28
26
 
@@ -30,4 +28,4 @@ if defined? ActionController::Base
30
28
  ActionController::Base.class_eval do
31
29
  include MongoidTraffic::ControllerAdditions
32
30
  end
33
- end
31
+ end
@@ -2,8 +2,7 @@ require 'logger'
2
2
 
3
3
  module MongoidTraffic
4
4
  module Log
5
-
6
- def self.included base
5
+ def self.included(base)
7
6
  base.extend ClassMethods
8
7
  base.class_eval do
9
8
  field :s, as: :scope, type: String
@@ -31,36 +30,36 @@ module MongoidTraffic
31
30
 
32
31
  default_scope -> { where(scope: nil) }
33
32
 
34
- scope :for_dates, -> date_from, date_to { where(date_from: date_from, date_to: date_to) }
33
+ scope :for_dates, -> (date_from, date_to) { where(date_from: date_from, date_to: date_to) }
35
34
 
36
- scope :yearly, -> year { self.for_dates(Date.parse("01/01/#{year}"), Date.parse("01/01/#{year}").at_end_of_year) }
37
- scope :monthly, -> month, year { self.for_dates(Date.parse("01/#{month}/#{year}"), Date.parse("01/#{month}/#{year}").at_end_of_month) }
38
- scope :weekly, -> week, year { self.for_dates(Date.commercial(year, week), Date.commercial(year, week).at_end_of_week) }
39
- scope :daily, -> date { self.for_dates(date, date) }
35
+ scope :yearly, -> (year) { for_dates(Date.parse("01/01/#{year}"), Date.parse("01/01/#{year}").at_end_of_year) }
36
+ scope :monthly, -> (month, year) { for_dates(Date.parse("01/#{month}/#{year}"), Date.parse("01/#{month}/#{year}").at_end_of_month) }
37
+ scope :weekly, -> (week, year) { for_dates(Date.commercial(year, week), Date.commercial(year, week).at_end_of_week) }
38
+ scope :daily, -> (date) { for_dates(date, date) }
40
39
 
41
- scope :scoped_to, -> scope { where(scope: scope) }
40
+ scope :scoped_to, -> (scope) { where(scope: scope) }
42
41
 
43
42
  # ---------------------------------------------------------------------
44
43
 
45
- index({ scope: 1, date_from: 1, date_to: 1 })
44
+ index(scope: 1, date_from: 1, date_to: 1)
46
45
  end
47
46
  end
48
47
 
49
48
  # =====================================================================
50
49
 
51
50
  module ClassMethods
52
- def log *args
51
+ def log(*args)
53
52
  MongoidTraffic::Logger.log(self, *args)
54
53
  end
55
54
 
56
- def aggregate_on att
55
+ def aggregate_on(att)
57
56
  case find_field_by_name(att).type.to_s
58
57
  when 'Integer' then sum(att)
59
58
  when 'Hash' then sum_hash(att)
60
59
  end
61
60
  end
62
61
 
63
- def sum att
62
+ def sum(att)
64
63
  if att.to_sym == :unique_ids
65
64
  aggregate_on(:unique_ids).keys.count
66
65
  else
@@ -68,26 +67,23 @@ module MongoidTraffic
68
67
  end
69
68
  end
70
69
 
71
- def find_field_by_name field_name
72
- return unless f = fields.detect{ |k,v| k == field_name.to_s or v.options[:as].to_s == field_name.to_s }
70
+ def find_field_by_name(field_name)
71
+ return unless f = fields.detect { |k, v| k == field_name.to_s || v.options[:as].to_s == field_name.to_s }
73
72
  f.last
74
73
  end
75
74
 
76
- def sum_hash field_name
77
- self.pluck(field_name).inject({}) do |res, h|
78
- merger = proc { |key, v1, v2|
79
- if Hash === v1 && Hash === v2
80
- v1.merge(v2, &merger)
81
- elsif Hash === v2
82
- v2
83
- else
84
- v1.to_i + v2.to_i
75
+ def sum_hash(field_name)
76
+ pluck(field_name).inject({}) do |res, h|
77
+ merger = proc do |_key, v1, v2|
78
+ case
79
+ when Hash === v1 && Hash === v2 then v1.merge(v2, &merger)
80
+ when Hash === v2 then v2
81
+ else v1.to_i + v2.to_i
85
82
  end
86
- }
83
+ end
87
84
  res = res.merge(h, &merger)
88
85
  end
89
86
  end
90
87
  end
91
-
92
88
  end
93
89
  end
@@ -5,30 +5,27 @@ require 'nokogiri'
5
5
  module MongoidTraffic
6
6
  class Logger
7
7
  class Bots
8
+ DATA_URL = 'http://www.user-agents.org/allagents.xml'.freeze
9
+ FILE_PATH = 'vendor/mongoid_traffic/allagents.xml'.freeze
8
10
 
9
- DATA_URL = "http://www.user-agents.org/allagents.xml"
10
- FILE_PATH = "vendor/mongoid_traffic/allagents.xml"
11
-
12
11
  class << self
13
12
  def list
14
13
  @list ||= begin
15
14
  response = File.open(FILE_PATH).read
16
15
  doc = Nokogiri::XML(response)
17
- list = []
18
- doc.xpath('//user-agent').each do |agent|
19
- type = agent.xpath("Type").text
20
- list << agent.xpath('String').text.gsub("&lt;","<") if %w(R S).include?(type)
16
+ doc.xpath('//user-agent').inject([]) do |res, agent|
17
+ type = agent.xpath('Type').text
18
+ res << agent.xpath('String').text.gsub('&lt;', '<') if %w(R S).include?(type)
19
+ res
21
20
  end
22
- list
23
21
  end
24
22
  end
25
23
 
26
- def is_a_bot? referer
24
+ def is_a_bot?(referer)
27
25
  return false unless referer.present?
28
26
  list.include?(referer)
29
27
  end
30
28
  end
31
-
32
29
  end
33
30
  end
34
- end
31
+ end
@@ -3,13 +3,10 @@ require 'user_agent'
3
3
  module MongoidTraffic
4
4
  class Logger
5
5
  class Browser
6
-
7
- def initialize user_agent_string
6
+ def initialize(user_agent_string)
8
7
  @user_agent_string = user_agent_string
9
8
  end
10
9
 
11
- # ---------------------------------------------------------------------
12
-
13
10
  def platform
14
11
  user_agent.platform
15
12
  end
@@ -27,7 +24,6 @@ module MongoidTraffic
27
24
  def user_agent
28
25
  @user_agent ||= ::UserAgent.parse(@user_agent_string)
29
26
  end
30
-
31
27
  end
32
28
  end
33
29
  end
@@ -3,20 +3,18 @@ require 'geoip'
3
3
  module MongoidTraffic
4
4
  class Logger
5
5
  class GeoIp
6
-
7
- DATA_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz"
8
- FILE_URL = "vendor/mongoid_traffic/GeoIP.dat"
6
+ DATA_URL = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz'.freeze
7
+ FILE_URL = 'vendor/mongoid_traffic/GeoIP.dat'.freeze
9
8
 
10
9
  class << self
11
10
  def geoip
12
11
  @geoip ||= ::GeoIP.new(FILE_URL)
13
12
  end
14
13
 
15
- def country_code2 str
14
+ def country_code2(str)
16
15
  geoip.country(str).country_code2
17
16
  end
18
17
  end
19
-
20
18
  end
21
19
  end
22
- end
20
+ end
@@ -3,8 +3,7 @@ require 'uri'
3
3
  module MongoidTraffic
4
4
  class Logger
5
5
  class Referer
6
-
7
- def initialize referer_string
6
+ def initialize(referer_string)
8
7
  @referer_string = referer_string
9
8
  end
10
9
 
@@ -17,11 +16,10 @@ module MongoidTraffic
17
16
  end
18
17
 
19
18
  private # =============================================================
20
-
19
+
21
20
  def uri
22
21
  URI.parse(@referer_string)
23
22
  end
24
-
25
23
  end
26
24
  end
27
25
  end
@@ -9,18 +9,17 @@ require_relative './logger/referer'
9
9
 
10
10
  module MongoidTraffic
11
11
  class Logger
12
-
13
- TIME_SCOPE_OPTIONS = %i(year month week day)
12
+ TIME_SCOPE_OPTIONS = %i(year month week day).freeze
14
13
 
15
14
  # ---------------------------------------------------------------------
16
15
 
17
- def self.log log_cls, *args
16
+ def self.log(log_cls, *args)
18
17
  new(log_cls, *args).log
19
18
  end
20
19
 
21
20
  # ---------------------------------------------------------------------
22
21
 
23
- def initialize log_cls, ip_address: nil, referer: nil, scope: nil, time_scope: %i(month day), unique_id: nil, user_agent: nil
22
+ def initialize(log_cls, ip_address: nil, referer: nil, scope: nil, time_scope: %i(month day), unique_id: nil, user_agent: nil)
24
23
  @log_cls = log_cls
25
24
  @ip_address = ip_address
26
25
  @referer_string = referer
@@ -32,10 +31,10 @@ module MongoidTraffic
32
31
 
33
32
  def log
34
33
  return if Bots.is_a_bot?(@referer_string)
35
- raise "Invalid time scope definition: #{@time_scope}" unless @time_scope.all?{ |ts| TIME_SCOPE_OPTIONS.include?(ts) }
34
+ raise "Invalid time scope definition: #{@time_scope}" unless @time_scope.all? { |ts| TIME_SCOPE_OPTIONS.include?(ts) }
36
35
 
37
36
  @time_scope.each do |ts|
38
- @log_cls.collection.find( find_query(ts) ).update_many( upsert_query, upsert: true )
37
+ @log_cls.collection.find(find_query(ts)).update_many(upsert_query, upsert: true)
39
38
  end
40
39
  end
41
40
 
@@ -43,11 +42,11 @@ module MongoidTraffic
43
42
 
44
43
  def upsert_query
45
44
  {
46
- '$inc' => access_count_query.
47
- merge(browser_query).
48
- merge(country_query).
49
- merge(referer_query).
50
- merge(unique_id_query),
45
+ '$inc' => access_count_query
46
+ .merge(browser_query)
47
+ .merge(country_query)
48
+ .merge(referer_query)
49
+ .merge(unique_id_query),
51
50
  '$set' => { uat: Time.now }
52
51
  }
53
52
  end
@@ -60,7 +59,7 @@ module MongoidTraffic
60
59
 
61
60
  def browser_query
62
61
  return {} unless browser.present?
63
- browser_path = [browser.platform, browser.name, browser.version].map{ |s| escape_key(s) }.join('.')
62
+ browser_path = [browser.platform, browser.name, browser.version].map { |s| escape_key(s) }.join('.')
64
63
  { "b.#{browser_path}" => 1 }
65
64
  end
66
65
 
@@ -85,13 +84,13 @@ module MongoidTraffic
85
84
 
86
85
  # ---------------------------------------------------------------------
87
86
 
88
- def escape_key key
89
- CGI::escape(key).gsub('.', '%2E')
87
+ def escape_key(key)
88
+ CGI.escape(key).gsub('.', '%2E')
90
89
  end
91
90
 
92
91
  # ---------------------------------------------------------------------
93
92
 
94
- def find_query ts
93
+ def find_query(ts)
95
94
  res = time_query(ts)
96
95
  res = res.merge(scope_query) if @scope.present?
97
96
  res
@@ -101,7 +100,7 @@ module MongoidTraffic
101
100
  { s: @scope }
102
101
  end
103
102
 
104
- def time_query ts
103
+ def time_query(ts)
105
104
  date = Date.today
106
105
  case ts
107
106
  when :day then { df: date, dt: date }
@@ -120,6 +119,5 @@ module MongoidTraffic
120
119
  return unless @referer_string.present?
121
120
  @referer ||= Referer.new(@referer_string)
122
121
  end
123
-
124
122
  end
125
123
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidTraffic
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -19,15 +19,15 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "geoip"
22
- spec.add_dependency "mongoid", "~> 5.0"
22
+ spec.add_dependency "mongoid", "~> 5.1"
23
23
  spec.add_dependency "nokogiri"
24
- spec.add_dependency "useragent", "~> 0.10.0"
24
+ spec.add_dependency "useragent"
25
25
 
26
26
  spec.add_development_dependency "bundler", "~> 1.3"
27
27
  spec.add_development_dependency "coveralls"
28
- spec.add_development_dependency "database_cleaner", ">= 1.5.1"
29
28
  spec.add_development_dependency "guard"
30
29
  spec.add_development_dependency "guard-minitest"
31
30
  spec.add_development_dependency "minitest"
31
+ spec.add_development_dependency "minitest-around"
32
32
  spec.add_development_dependency "rake"
33
33
  end
@@ -4,11 +4,10 @@ require_relative '../../lib/mongoid_traffic/controller_additions'
4
4
 
5
5
  module MongoidTraffic
6
6
  describe 'ControllerAdditions' do
7
-
8
7
  before(:each) do
9
8
  @controller_class = Class.new
10
9
  @controller = @controller_class.new
11
- def @controller_class.helper_method(name); end
10
+ def @controller_class.helper_method(_name); end
12
11
  @controller_class.send(:include, MongoidTraffic::ControllerAdditions)
13
12
  end
14
13
 
@@ -22,6 +21,5 @@ module MongoidTraffic
22
21
  describe '.log_scoped_traffic' do
23
22
  it 'infers scope from request path'
24
23
  end
25
-
26
24
  end
27
25
  end