Class: ToiletTracker::Services::AnalysisService

Inherits:
Object
  • Object
show all
Defined in:
lib/toilet_tracker/services/analysis_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(whatsapp_parser: Parsers::WhatsappParser.new, message_parser: Parsers::MessageParser.new, shift_service: ShiftService.new, timezone_service: TimezoneService.new) ⇒ AnalysisService

Returns a new instance of AnalysisService.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/toilet_tracker/services/analysis_service.rb', line 8

def initialize(
  whatsapp_parser: Parsers::WhatsappParser.new,
  message_parser: Parsers::MessageParser.new,
  shift_service: ShiftService.new,
  timezone_service: TimezoneService.new
)
  @whatsapp_parser = whatsapp_parser
  @message_parser = message_parser
  @shift_service = shift_service
  @timezone_service = timezone_service
end

Instance Attribute Details

#message_parserObject (readonly)

Returns the value of attribute message_parser.



6
7
8
# File 'lib/toilet_tracker/services/analysis_service.rb', line 6

def message_parser
  @message_parser
end

#shift_serviceObject (readonly)

Returns the value of attribute shift_service.



6
7
8
# File 'lib/toilet_tracker/services/analysis_service.rb', line 6

def shift_service
  @shift_service
end

#timezone_serviceObject (readonly)

Returns the value of attribute timezone_service.



6
7
8
# File 'lib/toilet_tracker/services/analysis_service.rb', line 6

def timezone_service
  @timezone_service
end

#whatsapp_parserObject (readonly)

Returns the value of attribute whatsapp_parser.



6
7
8
# File 'lib/toilet_tracker/services/analysis_service.rb', line 6

def whatsapp_parser
  @whatsapp_parser
end

Instance Method Details

#analyze_line(line) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/toilet_tracker/services/analysis_service.rb', line 28

def analyze_line(line)
  # Parse WhatsApp line format first
  whatsapp_result = whatsapp_parser.parse(line)
  return create_error_result(whatsapp_result.error, line) if whatsapp_result.failure?

  message = whatsapp_result.value

  # Parse message content for toilet tracking commands
  message_result = message_parser.parse(message)
  return create_error_result(message_result.error, line) if message_result.failure?

  parsed_result = message_result.value
  return nil unless parsed_result # Not a toilet tracking message

  # Process the parsed result
  process_parsed_result(parsed_result)
end

#analyze_lines(lines) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/toilet_tracker/services/analysis_service.rb', line 20

def analyze_lines(lines)
  # Parse all lines first
  results = lines.filter_map { |line| analyze_line(line) }

  # Apply retroactive shifts
  apply_retroactive_shifts_to_results(results)
end

#analyze_messages(messages, apply_retroactive_shifts: true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/toilet_tracker/services/analysis_service.rb', line 46

def analyze_messages(messages, apply_retroactive_shifts: true)
  results = []

  # First pass: process all messages and build shift history
  messages.each do |message|
    message_result = message_parser.parse(message)
    next if message_result.failure? || message_result.value.nil?

    result = process_parsed_result(message_result.value)
    results << result if result
  end

  # Second pass: apply retroactive shifts to poop events if enabled
  results = apply_retroactive_shifts_to_results(results) if apply_retroactive_shifts

  results
end