loo_parser 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
+ SHA256:
3
+ metadata.gz: 0ca9ec11cfc3a15201c750a61ff90c5b2714a111ea80140808db40893c944590
4
+ data.tar.gz: 21cf0438fea415a1c22dd87530ebb9e9c3cdd0e0b96e9e7a377e98c08cd3e1b4
5
+ SHA512:
6
+ metadata.gz: e98585a18fdb60421e3bb5df9116362490c7324aa8394d73663e13b62b86ca88dbcc7ac1034cae6a4913a07f0c9b8ce19e5a17ce007e7ee9f69f593c9bb26a76
7
+ data.tar.gz: 4d0499226d6c1f860a6ff10ed78dab5202a374f3482521af4c33aa08a13f97f23d334d83d3e31338268ac7eb680b39080030926515f2c1e01221153be92f3d10
data/.overcommit.yml ADDED
@@ -0,0 +1,14 @@
1
+ CommandContext:
2
+ bundle_exec: true
3
+
4
+ PreCommit:
5
+ RuboCop:
6
+ enabled: true
7
+ command: [ 'bundle', 'exec', 'rubocop' ]
8
+
9
+ BundleCheck:
10
+ enabled: true # Check if Gemfile.lock is up-to-date
11
+
12
+ Brakeman:
13
+ enabled: true
14
+ command: [ 'bundle', 'exec', 'brakeman', '-A', '--force' ] # -A scans current dir, --force ignores missing Rails structure
data/.rspec_status ADDED
@@ -0,0 +1,36 @@
1
+ example_id | status | run_time |
2
+ ----------------------------------------- | ------ | --------------- |
3
+ ./spec/loo_parser/parser_spec.rb[1:1:1] | passed | 0.00085 seconds |
4
+ ./spec/loo_parser/parser_spec.rb[1:1:2] | passed | 0.00004 seconds |
5
+ ./spec/loo_parser/parser_spec.rb[1:1:3] | passed | 0.00003 seconds |
6
+ ./spec/loo_parser/parser_spec.rb[1:1:4] | passed | 0.00004 seconds |
7
+ ./spec/loo_parser/parser_spec.rb[1:1:5] | passed | 0.00004 seconds |
8
+ ./spec/loo_parser/parser_spec.rb[1:1:6] | passed | 0.00025 seconds |
9
+ ./spec/loo_parser/parser_spec.rb[1:1:7] | passed | 0.00002 seconds |
10
+ ./spec/loo_parser/parser_spec.rb[1:2:1] | passed | 0.00011 seconds |
11
+ ./spec/loo_parser/parser_spec.rb[1:2:2] | passed | 0.00004 seconds |
12
+ ./spec/loo_parser/parser_spec.rb[1:2:3] | passed | 0.00002 seconds |
13
+ ./spec/loo_parser/parser_spec.rb[1:2:4] | passed | 0.00003 seconds |
14
+ ./spec/loo_parser/parser_spec.rb[1:3:1] | passed | 0.00012 seconds |
15
+ ./spec/loo_parser/parser_spec.rb[1:3:2:1] | passed | 0.00003 seconds |
16
+ ./spec/loo_parser/parser_spec.rb[1:3:2:2] | passed | 0.00002 seconds |
17
+ ./spec/loo_parser/parser_spec.rb[1:3:2:3] | passed | 0.00002 seconds |
18
+ ./spec/loo_parser/parser_spec.rb[1:3:2:4] | passed | 0.00002 seconds |
19
+ ./spec/loo_parser/parser_spec.rb[1:3:2:5] | passed | 0.00003 seconds |
20
+ ./spec/loo_parser/parser_spec.rb[1:3:2:6] | passed | 0.00002 seconds |
21
+ ./spec/loo_parser/parser_spec.rb[1:3:3] | passed | 0.00005 seconds |
22
+ ./spec/loo_parser/parser_spec.rb[1:3:4] | passed | 0.00004 seconds |
23
+ ./spec/loo_parser/parser_spec.rb[1:3:5] | passed | 0.00004 seconds |
24
+ ./spec/loo_parser/parser_spec.rb[1:3:6] | passed | 0.00003 seconds |
25
+ ./spec/loo_parser/parser_spec.rb[1:3:7] | passed | 0.00003 seconds |
26
+ ./spec/loo_parser/parser_spec.rb[1:3:8] | passed | 0.00003 seconds |
27
+ ./spec/loo_parser/parser_spec.rb[1:3:9] | passed | 0.00005 seconds |
28
+ ./spec/loo_parser/parser_spec.rb[1:3:10] | passed | 0.00004 seconds |
29
+ ./spec/loo_parser/parser_spec.rb[1:3:11] | passed | 0.00005 seconds |
30
+ ./spec/loo_parser/parser_spec.rb[1:3:12] | passed | 0.00005 seconds |
31
+ ./spec/loo_parser/parser_spec.rb[1:3:13] | passed | 0.00004 seconds |
32
+ ./spec/loo_parser/parser_spec.rb[1:3:14] | passed | 0.00004 seconds |
33
+ ./spec/loo_parser/parser_spec.rb[1:3:15] | passed | 0.00003 seconds |
34
+ ./spec/loo_parser/parser_spec.rb[1:3:16] | passed | 0.00029 seconds |
35
+ ./spec/loo_parser/parser_spec.rb[1:3:17] | passed | 0.00005 seconds |
36
+ ./spec/loo_parser/parser_spec.rb[1:3:18] | passed | 0.00004 seconds |
data/.rubocop.yml ADDED
@@ -0,0 +1,92 @@
1
+ plugins:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ # Use the Ruby version from your .ruby-version file or specify explicitly
8
+ TargetRubyVersion: 3.1 # Or determine dynamically if possible
9
+ NewCops: enable
10
+ Exclude:
11
+ - 'bin/console'
12
+ - 'db/schema.rb'
13
+ - 'tmp/**/*'
14
+ - 'vendor/**/*'
15
+ - 'node_modules/**/*'
16
+ - 'spec/fixtures/**/*'
17
+ - 'config/initializers/*' # Common for Rails
18
+ - 'config/environments/*' # Common for Rails
19
+
20
+ # Common Style Preferences
21
+ Style/Documentation:
22
+ Enabled: false
23
+
24
+ Style/StringLiterals:
25
+ EnforcedStyle: double_quotes
26
+
27
+ Style/StringLiteralsInInterpolation:
28
+ EnforcedStyle: double_quotes
29
+
30
+ Style/FrozenStringLiteralComment:
31
+ Enabled: true
32
+
33
+ Layout/LineLength:
34
+ Enabled: false # Disabled as requested
35
+ # Max: 100
36
+ # AllowHeredoc: true
37
+ # AllowURI: true
38
+ IgnoreCopDirectives: true
39
+ Exclude:
40
+ - '*.gemspec' # Gemspec files often have long lines
41
+
42
+ # Often noisy in tests
43
+ Metrics/BlockLength:
44
+ Max: 55 # Increased further to accommodate parser block
45
+ Exclude:
46
+ - 'spec/**/*.rb'
47
+ - 'Rakefile'
48
+ - '*.gemspec'
49
+ # - 'lib/loo_parser/parser.rb' # Remove exclusion, rely on higher Max
50
+
51
+ Metrics/ClassLength:
52
+ Max: 150 # Increased from 100
53
+
54
+ Metrics/MethodLength:
55
+ Max: 25 # Increased from 15
56
+ Exclude:
57
+ - 'spec/**/*.rb'
58
+ - 'lib/loo_parser/parser.rb' # Exclude the main parser methods
59
+
60
+ Metrics/AbcSize:
61
+ Max: 25 # Increased from 20
62
+ Exclude:
63
+ - 'spec/**/*.rb'
64
+ - 'lib/loo_parser/parser.rb' # Exclude the main parser methods
65
+
66
+ Metrics/CyclomaticComplexity:
67
+ Max: 10 # Increased from 7
68
+ Exclude:
69
+ - 'lib/loo_parser/parser.rb'
70
+
71
+ Metrics/PerceivedComplexity:
72
+ Max: 11 # Increased from 8
73
+ Exclude:
74
+ - 'lib/loo_parser/parser.rb'
75
+
76
+ Metrics/ParameterLists:
77
+ Max: 6 # Allow one more param
78
+
79
+ # RSpec Specific configuration
80
+ # Note: The RSpec namespace here is just for grouping settings visually;
81
+ # individual cops are configured directly (e.g., RSpec/MultipleExpectations)
82
+ # RSpec:
83
+ # Enabled: true # Example if you needed to disable all RSpec cops
84
+
85
+ RSpec/ExampleLength:
86
+ Max: 15 # Increased from 5
87
+
88
+ RSpec/MultipleExpectations:
89
+ Max: 5 # Allow a few expectations per test
90
+
91
+ RSpec/NestedGroups:
92
+ Max: 4 # Allow slightly deeper nesting
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2024-06-23
4
+
5
+ - Initial release of Loo Parser gem
6
+ - Added core functionality for parsing WhatsApp chat logs
7
+ - Support for tracking 💩 and 🚽 emoji messages
8
+ - Command-line interface for processing export files
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 BrownOps
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # Loo Parser (Ruby Gem)
2
+
3
+ [![Test & Lint](https://github.com/BrownOps/loo_parser.rb/actions/workflows/test.yml/badge.svg)](https://github.com/BrownOps/loo_parser.rb/actions/workflows/test.yml)
4
+
5
+ A Ruby tool for parsing WhatsApp chat exports to track and analyze bathroom usage patterns through specific emoji-based messages.
6
+
7
+ ## Overview
8
+
9
+ Loo Parser processes WhatsApp chat logs to extract, track, and analyze messages containing 💩 and 🚽 emojis according to a specific syntax. It helps keep track of bathroom usage over time by different chat participants, including timing and frequency analysis.
10
+
11
+ ## Features
12
+
13
+ - Parse WhatsApp chat exports (.txt format)
14
+ - Recognize multiple message formats for tracking events:
15
+ - Live events (happening now): `💩`
16
+ - Past events (specific time): `💩 YYYY-MM-DD HH:MM`
17
+ - Events with time shift: `💩 +N` or `💩 -N`
18
+ - Setting default time shift: `🚽 +N` or `🚽 -N`
19
+ - Track per-user bathroom usage patterns (via default shift)
20
+ - Detect and alert on unusual timing patterns (>30 days between event and message)
21
+ - Export structured data as JSON for further analysis
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'loo_parser'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ ```bash
34
+ bundle install
35
+ ```
36
+
37
+ Or install it yourself as:
38
+
39
+ ```bash
40
+ gem install loo_parser
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ After installation, you can run the parser from your command line:
46
+
47
+ ```bash
48
+ loo-parser parse [INPUT_FILE] [OUTPUT_FILE]
49
+ ```
50
+
51
+ For example, using a sample file:
52
+
53
+ ```bash
54
+ loo-parser parse whatsapp_chat_example.txt example_results.json
55
+ ```
56
+
57
+ This will parse the `whatsapp_chat_example.txt` file and save the extracted events to `example_results.json`.
58
+
59
+ If no files are specified, it will default to `whatsapp_chat.txt` for input and `poop_results.json` for output.
60
+
61
+ ### Message Format Examples
62
+
63
+ The parser recognizes the following message formats within the WhatsApp export:
64
+
65
+ ```
66
+ [DD/MM/YY, HH:MM:SS] Alice: 💩 # Current poop event
67
+ [DD/MM/YY, HH:MM:SS] Bob: 💩 2023-04-15 08:30 # Past poop event
68
+ [DD/MM/YY, HH:MM:SS] Charlie: 💩 +2 # Current poop with +2 shift
69
+ [DD/MM/YY, HH:MM:SS] Dave: 💩 -1 2023-04-10 19:45 # Past poop with -1 shift
70
+ [DD/MM/YY, HH:MM:SS] Eve: 🚽 +3 # Set default shift to +3 for Eve
71
+ ```
72
+
73
+ ### Output Format
74
+
75
+ The parser outputs JSON data in the following format (datetimes are ISO8601 strings, statuses/types are strings):
76
+
77
+ ```json
78
+ [
79
+ {
80
+ "sender": "Alice",
81
+ "message_timestamp": "2023-05-01T10:05:00+00:00",
82
+ "original_message": "💩",
83
+ "type": "poop_live",
84
+ "shift": 0,
85
+ "poop_time": "2023-05-01T10:05:00+00:00",
86
+ "status": "ok"
87
+ },
88
+ {
89
+ "sender": "Bob",
90
+ "message_timestamp": "2023-05-01T10:07:00+00:00",
91
+ "original_message": "💩 2023-04-30 09:00",
92
+ "type": "poop_past",
93
+ "shift": 0,
94
+ "poop_time": "2023-04-30T09:00:00+00:00",
95
+ "status": "ok"
96
+ },
97
+ {
98
+ "sender": "Dave",
99
+ "message_timestamp": "2023-06-15T10:00:00+00:00",
100
+ "original_message": "💩 2023-04-10 09:00",
101
+ "type": "poop_past",
102
+ "shift": 0,
103
+ "poop_time": "2023-04-10T09:00:00+00:00",
104
+ "status": "alert"
105
+ }
106
+ ]
107
+ ```
108
+
109
+ Status values can be:
110
+
111
+ - `ok`: Normal event (reported within 30 days of occurrence)
112
+ - `alert`: Event reported more than 30 days after occurrence
113
+ - `error`: Invalid format or parsing error (includes specific error types)
114
+
115
+ ## Development
116
+
117
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
118
+
119
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
120
+
121
+ ### Running Tests
122
+
123
+ ```bash
124
+ bundle exec rake spec
125
+ # or
126
+ bundle exec rspec
127
+ ```
128
+
129
+ ### Linting
130
+
131
+ ```bash
132
+ bundle exec rake rubocop
133
+ # or
134
+ bundle exec rubocop
135
+ ```
136
+
137
+ ## Contributing
138
+
139
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/BrownOps/loo_parser.rb](https://github.com/BrownOps/loo_parser.rb).
140
+
141
+ ## License
142
+
143
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |t|
8
+ t.rspec_opts = "--format documentation" # Or your preferred format
9
+ end
10
+
11
+ RuboCop::RakeTask.new(:rubocop)
12
+
13
+ # Default task
14
+ task default: %i[spec rubocop]
data/exe/loo-parser ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/loo_parser"
5
+
6
+ # Run the CLI command defined in LooParser::CLI
7
+ LooParser::CLI.start(ARGV)
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require_relative "parser"
5
+
6
+ module LooParser
7
+ # Defines the command-line interface commands
8
+ class CLI < Thor
9
+ package_name "LooParser"
10
+
11
+ desc "parse [INPUT_FILE] [OUTPUT_FILE]", "Parse WhatsApp chat export and save results."
12
+ long_desc <<-LONGDESC
13
+ Parses a WhatsApp chat export file (.txt) to find 💩 and 🚽 messages,
14
+ analyzing bathroom usage patterns.
15
+
16
+ Defaults:
17
+ INPUT_FILE: whatsapp_chat.txt
18
+ OUTPUT_FILE: poop_results.json
19
+ LONGDESC
20
+ def parse(input_file = "whatsapp_chat.txt", output_file = "poop_results.json")
21
+ puts "Starting analysis of #{input_file}..."
22
+ exit_code = LooParser::Parser.run(input_file, output_file)
23
+ exit(exit_code)
24
+ rescue StandardError => e
25
+ warn "Error: #{e.message}"
26
+ exit(1)
27
+ end
28
+
29
+ desc "version", "Show LooParser version"
30
+ def version
31
+ puts LooParser::VERSION
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,223 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "json"
5
+
6
+ module LooParser
7
+ # Core logic for parsing WhatsApp chat logs
8
+ module Parser
9
+ # Represents a parsed WhatsApp message
10
+ WhatsAppMessage = Struct.new(:timestamp, :sender, :content, keyword_init: true)
11
+
12
+ # Regex patterns for message types (order matters)
13
+ PATTERNS = {
14
+ toilet_set_shift: /^🚽\s*([+-]?\d+)(?:\s+.*)?$/,
15
+ poop_live_shift: /^💩\s*([+-]\d+)$/,
16
+ poop_past_shift: /^💩\s*([+-]\d+)\s+(\d{4}-\d{1,2}-\d{1,2})\s+(\d{1,2}:\d{2})(?:\s+.*)?$/,
17
+ poop_past: /^💩\s*(\d{4}-\d{1,2}-\d{1,2})\s+(\d{1,2}:\d{2})(?:\s+.*)?$/,
18
+ poop_live: /^💩/
19
+ }.freeze
20
+
21
+ # Parse date and time strings into a DateTime object
22
+ def self.parse_datetime(date_str, time_str)
23
+ # Basic sanity check: ensure input strings contain digits before parsing
24
+ return nil unless date_str&.match?(/\d/) && time_str&.match?(/\d/)
25
+
26
+ DateTime.parse("#{date_str} #{time_str}")
27
+ rescue Date::Error
28
+ nil
29
+ end
30
+
31
+ # Parse a single WhatsApp export line
32
+ # Example input: "[02/05/25, 14:30:15] Alice: Hello"
33
+ def self.parse_whatsapp_line(line)
34
+ pattern = %r{\[(\d{2})/(\d{2})/(\d{2}), (\d{2}):(\d{2}):(\d{2})\] (.*?): (.+)}
35
+ match = pattern.match(line)
36
+ return nil unless match
37
+
38
+ day, month, year_short, hh, mm, ss, sender, content = match.captures
39
+ year = "20#{year_short}"
40
+ timestamp = DateTime.parse("#{year}-#{month}-#{day}T#{hh}:#{mm}:#{ss}")
41
+ WhatsAppMessage.new(timestamp: timestamp, sender: sender.strip, content: content.strip)
42
+ rescue ArgumentError
43
+ nil
44
+ end
45
+
46
+ # Analyzer class to process messages
47
+ class Analyzer
48
+ attr_reader :sender_shift
49
+
50
+ def initialize
51
+ @sender_shift = Hash.new(0)
52
+ end
53
+
54
+ def analyze_messages(messages)
55
+ messages.filter_map { |msg| process_message(msg) }
56
+ end
57
+
58
+ private
59
+
60
+ def process_message(msg)
61
+ text = msg.content
62
+ PATTERNS.each do |type, regex|
63
+ match = regex.match(text)
64
+ next unless match
65
+
66
+ groups = match.captures
67
+
68
+ # Handle toilet shift setting
69
+ if type == :toilet_set_shift
70
+ begin
71
+ shift = Integer(groups[0])
72
+ @sender_shift[msg.sender] = shift
73
+ return {
74
+ sender: msg.sender,
75
+ message_timestamp: msg.timestamp,
76
+ type: type,
77
+ shift: shift,
78
+ status: :ok
79
+ }
80
+ rescue ArgumentError
81
+ return {
82
+ sender: msg.sender,
83
+ message_timestamp: msg.timestamp,
84
+ original_message: text,
85
+ type: :toilet_set_shift_invalid_number,
86
+ status: :error
87
+ }
88
+ end
89
+ end
90
+
91
+ # Handle poop events
92
+ begin
93
+ shift, poop_time = extract_poop_info(type, groups, msg)
94
+
95
+ if poop_time.nil?
96
+ status = :error
97
+ details = :invalid_datetime_format
98
+ else
99
+ status = compute_status(poop_time, msg.timestamp)
100
+ details = nil
101
+ end
102
+
103
+ result = {
104
+ sender: msg.sender,
105
+ message_timestamp: msg.timestamp,
106
+ original_message: text,
107
+ type: type,
108
+ shift: shift,
109
+ poop_time: poop_time,
110
+ status: status
111
+ }
112
+ result[:error_details] = details if details
113
+ return result
114
+ rescue ArgumentError, TypeError
115
+ return {
116
+ sender: msg.sender,
117
+ message_timestamp: msg.timestamp,
118
+ original_message: text,
119
+ type: :"#{type}_processing_error",
120
+ status: :error
121
+ }
122
+ end
123
+ end
124
+
125
+ # Handle messages with emojis but not matching patterns
126
+ if text.include?("💩")
127
+ return {
128
+ sender: msg.sender,
129
+ message_timestamp: msg.timestamp,
130
+ original_message: text,
131
+ type: :unrecognized_poop_emoji,
132
+ status: :error
133
+ }
134
+ end
135
+
136
+ if text.include?("🚽")
137
+ return {
138
+ sender: msg.sender,
139
+ message_timestamp: msg.timestamp,
140
+ original_message: text,
141
+ type: :unrecognized_toilet_emoji,
142
+ status: :error
143
+ }
144
+ end
145
+
146
+ nil
147
+ end
148
+
149
+ def extract_poop_info(type, groups, msg)
150
+ current_sender_shift = @sender_shift[msg.sender]
151
+
152
+ case type
153
+ when :poop_live
154
+ [current_sender_shift, msg.timestamp]
155
+ when :poop_live_shift
156
+ shift = Integer(groups[0])
157
+ [shift, msg.timestamp]
158
+ when :poop_past
159
+ poop_dt = Parser.parse_datetime(groups[0], groups[1])
160
+ [current_sender_shift, poop_dt]
161
+ when :poop_past_shift
162
+ shift = Integer(groups[0])
163
+ poop_dt = Parser.parse_datetime(groups[1], groups[2])
164
+ [shift, poop_dt]
165
+ else
166
+ [0, nil]
167
+ end
168
+ end
169
+
170
+ def compute_status(poop_time, message_timestamp)
171
+ return :error unless poop_time.is_a?(DateTime)
172
+
173
+ # Calculate difference in days
174
+ time_difference_days = message_timestamp - poop_time
175
+
176
+ time_difference_days > 30 ? :alert : :ok
177
+ end
178
+ end
179
+
180
+ # Main execution logic called by CLI
181
+ def self.run(input_file, output_file)
182
+ # Read input file
183
+ lines = File.readlines(input_file, chomp: true, encoding: "UTF-8")
184
+
185
+ # Parse messages
186
+ messages = lines.filter_map { |line| parse_whatsapp_line(line) }
187
+
188
+ # Analyze messages
189
+ analyzer = Analyzer.new
190
+ results = analyzer.analyze_messages(messages)
191
+
192
+ # Print summary
193
+ puts "Processed #{messages.count} valid messages, found #{results.count} relevant records."
194
+
195
+ # Custom JSON conversion for DateTime and Symbols
196
+ json_results = results.map do |record|
197
+ record.transform_values do |value|
198
+ case value
199
+ when DateTime
200
+ value.strftime("%Y-%m-%dT%H:%M:%S")
201
+ when Symbol
202
+ value.to_s
203
+ else
204
+ value
205
+ end
206
+ end
207
+ end
208
+
209
+ # Store results as JSON
210
+ File.write(output_file, JSON.pretty_generate(json_results))
211
+
212
+ puts "Results saved to #{output_file}"
213
+ 0
214
+ rescue Errno::ENOENT
215
+ warn "Error: Input file not found: #{input_file}"
216
+ 1
217
+ rescue StandardError => e
218
+ warn "An unexpected error occurred: #{e.message}"
219
+ warn e.backtrace.join("\n")
220
+ 1
221
+ end
222
+ end
223
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LooParser
4
+ VERSION = "0.1.0"
5
+ end
data/lib/loo_parser.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+
5
+ loader = Zeitwerk::Loader.for_gem
6
+ loader.setup
7
+
8
+ require_relative "loo_parser/version"
9
+ require_relative "loo_parser/parser"
10
+ require_relative "loo_parser/cli"
11
+
12
+ # Main module for the LooParser gem
13
+ module LooParser
14
+ class Error < StandardError; end
15
+ # Your code goes here...
16
+ end
17
+
18
+ # Optional: if using Zeitwerk
19
+ # loader.eager_load # Optional: If you want to load everything upfront
@@ -0,0 +1,4 @@
1
+ module LooParser
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: loo_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - BrownOps
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: thor
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.2'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.2'
26
+ - !ruby/object:Gem::Dependency
27
+ name: zeitwerk
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.6'
40
+ description: "Loo Parser processes WhatsApp chat logs to extract, track, and analyze
41
+ messages containing \U0001F4A9 and \U0001F6BD emojis according to a specific syntax.\nIt
42
+ helps keep track of bathroom usage over time by different chat participants, including
43
+ timing and frequency analysis.\n"
44
+ email:
45
+ - nethersoul.dev@gmail.com
46
+ executables:
47
+ - loo-parser
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".overcommit.yml"
52
+ - ".rspec_status"
53
+ - ".rubocop.yml"
54
+ - CHANGELOG.md
55
+ - CODE_OF_CONDUCT.md
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - exe/loo-parser
60
+ - lib/loo_parser.rb
61
+ - lib/loo_parser/cli.rb
62
+ - lib/loo_parser/parser.rb
63
+ - lib/loo_parser/version.rb
64
+ - sig/loo_parser.rbs
65
+ homepage: https://github.com/BrownOps/loo_parser.rb
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ homepage_uri: https://github.com/BrownOps/loo_parser.rb
70
+ source_code_uri: https://github.com/BrownOps/loo_parser.rb
71
+ changelog_uri: https://github.com/BrownOps/loo_parser.rb/blob/main/CHANGELOG.md
72
+ rubygems_mfa_required: 'true'
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 3.1.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.6.7
88
+ specification_version: 4
89
+ summary: A Ruby tool for parsing WhatsApp chat exports to track bathroom usage patterns.
90
+ test_files: []