Class: Todoist::Due
- Inherits:
-
Struct
- Object
- Struct
- Todoist::Due
- Defined in:
- lib/todoist/due.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the value of attribute date.
-
#date_obj ⇒ Object
Returns the value of attribute date_obj.
-
#is_recurring ⇒ Object
Returns the value of attribute is_recurring.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#orig_date_str ⇒ Object
Returns the value of attribute orig_date_str.
-
#string ⇒ Object
Returns the value of attribute string.
-
#timezone ⇒ Object
Returns the value of attribute timezone.
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #bump ⇒ Object
- #days_until ⇒ Object
-
#initialize(*arg_hashes) ⇒ Due
constructor
A new instance of Due.
- #parse_due ⇒ Object
- #present? ⇒ Boolean
- #recurring? ⇒ Boolean
- #time_at_next_qarter_hour(start_time = nil) ⇒ Object
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
#date ⇒ Object
Returns the value of attribute date
5 6 7 |
# File 'lib/todoist/due.rb', line 5 def date @date end |
#date_obj ⇒ Object
Returns the value of attribute date_obj
5 6 7 |
# File 'lib/todoist/due.rb', line 5 def date_obj @date_obj end |
#is_recurring ⇒ Object
Returns the value of attribute is_recurring
5 6 7 |
# File 'lib/todoist/due.rb', line 5 def is_recurring @is_recurring end |
#lang ⇒ Object
Returns the value of attribute lang
5 6 7 |
# File 'lib/todoist/due.rb', line 5 def lang @lang end |
#orig_date_str ⇒ Object
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 |
#string ⇒ Object
Returns the value of attribute string
5 6 7 |
# File 'lib/todoist/due.rb', line 5 def string @string end |
#timezone ⇒ Object
Returns the value of attribute timezone
5 6 7 |
# File 'lib/todoist/due.rb', line 5 def timezone @timezone end |
Instance Method Details
#blank? ⇒ Boolean
27 28 29 |
# File 'lib/todoist/due.rb', line 27 def blank? date.blank? && string.blank? end |
#bump ⇒ Object
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_until ⇒ Object
58 59 60 |
# File 'lib/todoist/due.rb', line 58 def days_until (Date.parse(date) - Todoist.today).to_i end |
#parse_due ⇒ Object
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
62 63 64 |
# File 'lib/todoist/due.rb', line 62 def present? !blank? end |
#recurring? ⇒ 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 |