summaryrefslogtreecommitdiff
path: root/lib/gitlab/quick_actions/spend_time_and_date_separator.rb
blob: 7328c517a3037d580dc318217d8766de3a4a345d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module Gitlab
  module QuickActions
    # This class takes spend command argument
    # and separates date and time from spend command arguments if it present
    # example:
    # spend_command_time_and_date = "15m 2017-01-02"
    # SpendTimeAndDateSeparator.new(spend_command_time_and_date).execute
    # => [900, Mon, 02 Jan 2017]
    # if date doesn't present return time with current date
    # in other cases return nil
    class SpendTimeAndDateSeparator
      DATE_REGEX = %r{(\d{2,4}[/\-.]\d{1,2}[/\-.]\d{1,2})}

      def initialize(spend_command_arg)
        @spend_arg = spend_command_arg
      end

      def execute
        return if @spend_arg.blank?
        return [get_time, DateTime.now.to_date] unless date_present?
        return unless valid_date?

        [get_time, get_date]
      end

      private

      def get_time
        raw_time = @spend_arg.gsub(DATE_REGEX, '')
        Gitlab::TimeTrackingFormatter.parse(raw_time)
      end

      def get_date
        string_date = @spend_arg.match(DATE_REGEX)[0]
        Date.parse(string_date)
      end

      def date_present?
        DATE_REGEX =~ @spend_arg
      end

      def valid_date?
        string_date = @spend_arg.match(DATE_REGEX)[0]
        date = Date.parse(string_date) rescue nil

        date_past_or_today?(date)
      end

      def date_past_or_today?(date)
        date&.past? || date&.today?
      end
    end
  end
end