Class: Todoist::Due

Inherits:
Struct
  • Object
show all
Defined in:
lib/todoist/due.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arg_hashes) ⇒ Due

Returns a new instance of Due.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/todoist/due.rb', line 8

def initialize(*arg_hashes)
  if arg_hashes.is_a?(Array)
    arg_hash = arg_hashes.first
  end
  if arg_hash.nil?
    super()
    return
  end
  @orig_date_str = arg_hash&.dig("date").freeze
  arg_hash[:date_obj] = if arg_hash&.dig("date").nil?
    nil
  elsif arg_hash&.dig("date")&.to_s&.upcase&.include?("T")
    Time.parse(arg_hash["date"]).utc
  else
    Date.parse(arg_hash["date"])
  end
  super(arg_hash)
end

Instance Attribute Details

#dateObject

Returns the value of attribute date

Returns:

  • (Object)

    the current value of date



5
6
7
# File 'lib/todoist/due.rb', line 5

def date
  @date
end

#date_objObject

Returns the value of attribute date_obj

Returns:

  • (Object)

    the current value of date_obj



5
6
7
# File 'lib/todoist/due.rb', line 5

def date_obj
  @date_obj
end

#is_recurringObject

Returns the value of attribute is_recurring

Returns:

  • (Object)

    the current value of is_recurring



5
6
7
# File 'lib/todoist/due.rb', line 5

def is_recurring
  @is_recurring
end

#langObject

Returns the value of attribute lang

Returns:

  • (Object)

    the current value of lang



5
6
7
# File 'lib/todoist/due.rb', line 5

def lang
  @lang
end

#orig_date_strObject

Returns the value of attribute orig_date_str.



6
7
8
# File 'lib/todoist/due.rb', line 6

def orig_date_str
  @orig_date_str
end

#stringObject

Returns the value of attribute string

Returns:

  • (Object)

    the current value of string



5
6
7
# File 'lib/todoist/due.rb', line 5

def string
  @string
end

#timezoneObject

Returns the value of attribute timezone

Returns:

  • (Object)

    the current value of timezone



5
6
7
# File 'lib/todoist/due.rb', line 5

def timezone
  @timezone
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/todoist/due.rb', line 27

def blank?
  date.blank? && string.blank?
end

#bumpObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/todoist/due.rb', line 31

def bump
  if string.blank?
    TodoistRestClient.logger.error { "You can only bump recurring tasks" }
    return nil
  end
  mm = string.match(RECURRING_REGEX)
  unless mm
    TodoistRestClient.logger.error { "Cannot parse the string of this due obj: #{string}" }
    return nil
  end
  hours = mm[1].to_i * HOURS_PER_UNIT.fetch(mm[2].downcase)
  if date_obj.is_a?(Date) && hours % 24 == 0
    days = (hours / 24).to_i
    date_today = Todoist.today
    if date_today + days > date_obj
      self.date_obj = date_today + days
      date_obj.to_s
    end
  else
    bumped = time_at_next_qarter_hour + hours * 3600
    if bumped > date_obj
      self.date_obj = Time.utc(bumped.year, bumped.month, bumped.day, bumped.hour, bumped.min)
      date_obj.strftime("%Y-%m-%dT%H:%M:%S")
    end
  end
end

#days_untilObject



58
59
60
# File 'lib/todoist/due.rb', line 58

def days_until
  (Date.parse(date) - Todoist.today).to_i
end

#parse_dueObject



70
71
72
73
74
# File 'lib/todoist/due.rb', line 70

def parse_due
  regex = /^(?:ev(?:ery)?(?<bang>!)?)\s+(?<dates>.+?)(?:\s+(?:at\s+)?(?<time>\d{1,2}(?::\d{1,2})?))?(?:\s+starting\s+(?<starting>.+?))?(?:\s+ending\s+(?<ending>.+?))?$/i
  date_parts = string.match(regex)
  date_parts[:dates].split(/,\s/)
end

#present?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/todoist/due.rb', line 62

def present?
  !blank?
end

#recurring?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/todoist/due.rb', line 66

def recurring?
  is_recurring.present?
end

#time_at_next_qarter_hour(start_time = nil) ⇒ Object



76
77
78
79
80
81
# File 'lib/todoist/due.rb', line 76

def time_at_next_qarter_hour(start_time = nil)
  start_time ||= Todoist.now
  minutes = ((start_time.min + 14) / 15) * 15
  top_of_hour = Time.utc(start_time.year, start_time.month, start_time.day, start_time.hour, 0, 0)
  top_of_hour + minutes * 60
end