browser_sense 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e198b13035e5ca4775150f1413cd09e67b84e0be7d6817e8855a4358d5f80b22
4
- data.tar.gz: a479aff279f85768b99442bc4de1546c2c74c9830e65dea7d780305639c7c0c5
3
+ metadata.gz: e8fa590833475085fe473a90988cb128353267bc7bbcef259b0159a812fcf98c
4
+ data.tar.gz: 4b34a65d1875803994557840fb211e769fe406f63ccbd8b6b81b83f32216e382
5
5
  SHA512:
6
- metadata.gz: e642b21b603aa52382ce1b94f838c6c66f4872e28f5f8aa470d523ae9e9a846a12081972b1a81e92b12b0445fbf84a609d5b049d12e95e36c89316810dbbf325
7
- data.tar.gz: 9ff5e4816096babc640372678d7a11186ab39318435d433a9b8142e06b8291a83a0c4597771f9b97297ee876d16f3a2720a8f864ccae840ae91940d9ba8d7637
6
+ metadata.gz: 8cf8c94b32d56f262c414adbff5560f23da9a1e5263ff380583d8f81582a4f4fa35c0c198adfddd9633fe4902938fc2920b445c2183dc83df5ad60d61bc702af
7
+ data.tar.gz: 99d3a4e0c8938291aa0155459579dbdf83569dcecbf024d35de61d49ea771ab3105da1255c5698d7e4b6beca4711f28d42c87c699000229bf28a40f3a3fa972c
data/CHANGELOG.org CHANGED
@@ -1,10 +1,31 @@
1
- #+TITLE: Browser Info ChangeLog
1
+ #+TITLE: BrowserSense ChangeLog
2
2
 
3
- * [1.0.0] - 2025-08-31
3
+ * [1.1.0] - <2025-12-21 Sun>
4
+
5
+ - Store more information. The following fields are now appended to the lines
6
+ generated by BrowserSense:
7
+
8
+ - ip
9
+ - browser.version
10
+ - platform.version,
11
+ - browser.bot?
12
+ - browser.bot&.name
13
+ - browser.bot&.search_engine?
14
+
15
+ - Use CSV to ensure the log line is properly parsable as a CSV string
16
+ - Add the following two methods: =BrowserSenseParser.match?= and
17
+ =BrowserSenseParser.parse=
18
+
19
+
20
+ * [1.0.1] - <2025-09-30 Tue>
21
+
22
+ - Fixes documentation
23
+
24
+ * [1.0.0] - <2025-08-31 Sun>
4
25
 
5
26
  - First public release
6
27
  - Changes and improvements to the documenation for public release
7
28
 
8
- * [0.1.0] - 2022-11-23
29
+ * [0.1.0] - <2022-11-23 Wed>
9
30
 
10
31
  - Initial release (private)
data/README.org CHANGED
@@ -18,13 +18,14 @@ Add the gem to your application's Gemfile and then bundle:
18
18
  bundle
19
19
  #+end_example
20
20
 
21
- To enable logging, add =browser_infro= to the =application_controller.rb= of
22
- your app, e.g.:
21
+ To enable logging, add =browser_sense= to the =application_controller.rb= of
22
+ your app, i.e.:
23
23
 
24
24
  #+begin_src ruby
25
25
  class ApplicationController < ActionController::Base
26
26
  [...]
27
27
 
28
+ include BrowserSenseFilter
28
29
  browser_sense
29
30
 
30
31
  [...]
@@ -46,23 +47,41 @@ following details:
46
47
  7. Hashed IP, which allows to track requests from the same IP, while preserving
47
48
  privacy
48
49
  8. Timestamp
50
+ 9. IP
51
+ 10. browser_version
52
+ 11. platform_version
53
+ 12. bot?
54
+ 13. search_engine?
55
+ 14. bot name
49
56
 
50
57
  Information is stored in the application log, each line prefixed by
51
58
  =BrowserSense= and data presented in CSV.
52
59
 
53
60
  *Example*
54
61
 
62
+ Before 1.10:
63
+
55
64
  #+begin_example bash
56
65
  $ cat production.log
57
66
  [...]
58
- BrowserSense: "Firefox","linux","Unknown","Devise::SessionsController","new","12ca17b49af2289436f303e0166030a21e525d266e209267433801a8fd4071a0","2022-06-09T10:08:09+02:00"
67
+ [<production log header>] BrowserSense: "Firefox","linux","Unknown","Devise::SessionsController","new","12ca17b49af2289436f303e0166030a21e525d266e209267433801a8fd4071a0","2022-06-09T10:08:09+02:00"
59
68
  [...]
60
69
  #+end_example
61
70
 
71
+ From 1.10 on (we add some field and use CSV for rendering the CSV, which removes unnecessary quotes).
72
+ (Notice that the last field, =bot_name= is empty. Hence the final comma.
73
+
74
+ #+begin_example bash
75
+ $ cat production.log
76
+ [...]
77
+ [<production log header>] BrowserSense: Firefox,linux,Unknown,UsageController,index,html,12ca17b49af2289436f303e0166030a21e525d266e209267433801a8fd4071a0,2025-12-22T09:29:58+01:00,127.0.0.1,145,0,false,false,
78
+ [...]
79
+ #+end_example
80
+
62
81
 
63
82
  * Analyzing Data
64
83
 
65
- When you want to analyze data, extract the information with:
84
+ *(Option 1)* When you want to analyze data, extract the information with:
66
85
 
67
86
  #+begin_example
68
87
  grep BrowserSense production.log | cut -f2- -d: > data.csv
@@ -70,12 +89,18 @@ grep BrowserSense production.log | cut -f2- -d: > data.csv
70
89
 
71
90
  and then, e.g., open the resulting file in a spreadsheet.
72
91
 
73
- There is also a ruby script =browser_sense=. The script takes as input the
74
- pathname of the file with the log and prints to stdout the result of various
92
+ *(Option 2)* If you prefer you can use
93
+
94
+ - =BrowserSenseParser.match?(line)= which returns true if the =line= is a line
95
+ produced by BrowserSense
96
+ - =BrowserSenseParser.parse(line)= which returns a Hash with all the fields
97
+
98
+ *(Option 3)* There is a Ruby script =browser_sense=. The script takes as input
99
+ the pathname of the file with the log and prints to stdout the result of various
75
100
  analyses. The script is a quick hack, wrapping =sed=, =grep= and =miller=
76
101
  ([[https://github.com/johnkerl/miller][Miller)]], which must be installed on your machine for the script to succeed.
77
102
 
78
- If you want more reports and plots, you can use the =log_sense= gem
103
+ *(Option 4)* If you want more reports and plots, you can use the =log_sense= gem
79
104
  (https://rubygems.org/gems/log_sense), which understands the data generated by
80
105
  this gem.
81
106
 
@@ -1,5 +1,6 @@
1
1
  require "active_support/concern"
2
2
  require "browser"
3
+ require "csv"
3
4
 
4
5
  module BrowserSenseFilter
5
6
  extend ActiveSupport::Concern
@@ -18,15 +19,27 @@ module BrowserSenseFilter
18
19
  now = DateTime.now
19
20
 
20
21
  logger = Rails.logger
22
+
21
23
  browser_data = [
22
- b.name, b.platform, b.device.name,
23
- controller.class.name, controller.action_name,
24
+ b.name,
25
+ b.platform,
26
+ b.device.name,
27
+ controller.class.name,
28
+ controller.action_name,
24
29
  request.format.symbol,
25
30
  hashed_ip,
26
- now
31
+ now,
32
+ # added in v 1.1.10
33
+ ip,
34
+ b.version,
35
+ b.platform.version,
36
+ browser.bot?,
37
+ browser.bot&.search_engine?,
38
+ browser.bot&.name
27
39
  ]
28
40
 
29
- browser_data_str = browser_data.map { |x| "\"#{x}\"" }.join(",")
41
+ CSV(browser_data_str = "") { |csv_str| csv_str << browser_data }
42
+
30
43
  logger.info "BrowserSense: #{browser_data_str}"
31
44
  end
32
45
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrowserSenseParser
4
+ HEADERS = %i[
5
+ browser platform device_name controller method
6
+ request_format anon_ip timestamp
7
+ ip browser_version platform_version
8
+ bot search_engine bot_name
9
+ ]
10
+
11
+ def self.match?(line)
12
+ /Browser(Sense|Info):/.match? line
13
+ end
14
+
15
+ def self.parse(line)
16
+ begin
17
+ csv_portion = line.gsub(/^.*Browser(Sense|Info): /, "")
18
+ data = CSV.parse csv_portion, headers: HEADERS
19
+ log_id = log_id(line)
20
+ data.first.to_h.merge(log_id)
21
+ rescue => exception
22
+ Rails.logger.debug exception
23
+ {}
24
+ end
25
+ end
26
+
27
+ private_class_method def self.log_id(line)
28
+ matchdata = /-- : \[(?<log_id>[a-f0-9-]+)\]/.match(line)
29
+
30
+ { log_id: matchdata[:log_id] }
31
+ end
32
+ end
@@ -10,31 +10,10 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = "Monitor which browser is accessing your Ruby on Rails app."
12
12
  spec.description = <<-EOS
13
- BrowserSense logs information about the browser accessing your
14
- RubyOnRails app.
13
+ BrowserSense logs information about the browsers using your RubyOnRails app.
15
14
 
16
- Installation is pretty simple:
17
-
18
- 1. Add =gem "browser_sense"= to your Gemfile
19
-
20
- 2. Add the following line to your =application_controller.rb=:
21
-
22
- browser_sense
23
-
24
- Data is stored in csv format in Rails' production log. Each URL invocation
25
- generates a log line.
26
-
27
- Extract browser info data with:
28
-
29
- grep BrowserSense development.log | cut -f2- -d:
30
-
31
- Analyize with
32
-
33
- browser-info log/production.log > output.txt
34
-
35
- or with log_sense (https://rubygems.org/gems/log_sense)
36
-
37
- BrowserSense relies on the browser gem to get informaton about the browsers.
15
+ Data can be easily extracted from the log, analyzed with the included
16
+ script or with the log_sense gem.
38
17
  EOS
39
18
  spec.homepage = "https://github.com/shair-tech/browser_sense"
40
19
  spec.license = "MIT"
@@ -58,8 +37,10 @@ Gem::Specification.new do |spec|
58
37
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
59
38
  spec.require_paths = ["lib"]
60
39
 
61
- # Uncomment to register a new dependency of your gem
40
+ # We just log. All the recognition work is done by browser
62
41
  spec.add_dependency "browser", "~> 6.2.0"
42
+ # To properly quote log lines
43
+ spec.add_dependency "csv"
63
44
 
64
45
  # For more information and examples about making a new gem, check out our
65
46
  # guide at: https://bundler.io/guides/creating_gem.html
data/exe/browser_sense CHANGED
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  # Look for BrowserSense | cut info by Rails | remove lines with less than 7 fields
24
24
  # No need to remove double quotes with sed -e 's/\"//g'
25
- `grep -E -e 'Browser(Info|Sense)' #{LOG_FILE} | sed -E 's/.*Browser(Info|Sense): (.+,.+,.+,.+,.+,.+,.+,.+)/\\2/g' > #{TMP_FILE}`
25
+ `grep -E -e 'Browser(Info|Sense)' #{LOG_FILE} | sed -E 's/.*Browser(Info|Sense): (.+,.+,.+,.+,.+,.+,.+,.+,(.+,.+,.+,.+,.+,.+)?)/\\2/g' > #{TMP_FILE}`
26
26
 
27
27
  # Look at the README file of BrowserSense for the field specification
28
28
  #
@@ -37,6 +37,15 @@ end
37
37
  # 7. Hashed IP, which allows to track requests from the same IP, while preserving privacy
38
38
  # 8. Timestamp
39
39
  #
40
+ # Added in 1.10 (and made optional in the regexp above
41
+ #
42
+ # 9, ip
43
+ # 10. b.version
44
+ # 11. b.platform.version
45
+ # 12. browser.bot?
46
+ # 13. browser.bot&.search_engine?
47
+ # 13. browser.bot&.name
48
+ #
40
49
 
41
50
  # methods by os
42
51
  # the usage of reshape and unsparsify is still kind of magic to me.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BrowserSense
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browser_sense
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adolfo Villafiorita
@@ -23,15 +23,25 @@ dependencies:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
25
  version: 6.2.0
26
- description: " BrowserSense logs information about the browser accessing your\n
27
- \ RubyOnRails app. \n\n Installation is pretty simple:\n\n 1. Add =gem
28
- \"browser_sense\"= to your Gemfile\n\n 2. Add the following line to your =application_controller.rb=:\n\n
29
- \ browser_sense\n\n Data is stored in csv format in Rails' production
30
- log. Each URL invocation\n generates a log line.\n\n Extract browser info
31
- data with:\n\n grep BrowserSense development.log | cut -f2- -d:\n\n Analyize
32
- with\n\n browser-info log/production.log > output.txt\n\n or with log_sense
33
- (https://rubygems.org/gems/log_sense)\n\n BrowserSense relies on the browser
34
- gem to get informaton about the browsers.\n"
26
+ - !ruby/object:Gem::Dependency
27
+ name: csv
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ description: |2
41
+ BrowserSense logs information about the browsers using your RubyOnRails app.
42
+
43
+ Data can be easily extracted from the log, analyzed with the included
44
+ script or with the log_sense gem.
35
45
  email:
36
46
  - adolfo@shair.tech
37
47
  executables:
@@ -47,6 +57,7 @@ files:
47
57
  - README.org
48
58
  - Rakefile
49
59
  - app/controllers/browser_sense_filter.rb
60
+ - app/services/browser_sense_parser.rb
50
61
  - browser_sense.gemspec
51
62
  - exe/browser_sense
52
63
  - lib/browser_sense.rb