Class: Medjool::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/medjool/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}) ⇒ Parser

Returns a new instance of Parser.



8
9
10
# File 'lib/medjool/parser.rb', line 8

def initialize(context = {})
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/medjool/parser.rb', line 6

def context
  @context
end

Instance Method Details

#is_date_range?(text) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/medjool/parser.rb', line 12

def is_date_range?(text)
  Medjool::DATE_RANGE_MATCHER.match(text.strip).present?
end

#parse(text, update_now = true) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/medjool/parser.rb', line 53

def parse(text, update_now = true)
  if Medjool::DATE_MATCHER.match(text)
    # Let's do an aggressive strip of any whitespace at the start and end, including crazy unicodeness
    # that the standard 'strip' would miss
    text = text.gsub(/^[^a-zA-Z0-9]*/, "").gsub(/[^a-zA-Z0-9]*$/, "")

    if /^[0-9]$/.match(text)
      # Handle lone integers
      text = "#{text}st"
    end

    begin
      base_date = Date.parse(text)
    rescue ArgumentError => e
      # If Date.parse is used on the string "31st" during
      # a month with only 30 days, it will explode
      if Medjool::END_OF_MONTH_MATCHER.match(text)
        # This is a little bit hacky, but switch to a date that will definitely work
        base_date = Date.parse("#{text} January")
      else
        return nil
      end
    end

    should_determine_ambiguity = false
    if @context[:now]
      should_determine_ambiguity = true
    else
      # In the absence of context, always fall back on the default guess
      guess_date = base_date

      # Only exception for that is the special case where it's currently December,
      # and "January" *obviouly* should mean January NEXT year not THIS year
      if Date.today.month == 12 && guess_date < 10.months.ago          
        should_determine_ambiguity = true
        @context[:now] = Date.today
      end
    end

    if should_determine_ambiguity
      # Determine the ambiguity level for this data
      ambiguity = determine_ambiguity(text)

      now = @context[:now].to_date

      case ambiguity
        when :none
          return base_date

        when :weekly
          # Pick the nearest week that meets the context
          guess_date = now + (base_date.wday - now.wday).days - 1.week

          while guess_date < now
            guess_date += 1.week
          end
        when :monthly
          # Pick the nearest month where the provided day of the month meets the context
          y = now.year
          m = now.month - 1
          guess_date = nil

          while guess_date.nil? || guess_date < now
            m += 1
            if m > 12
              m = 1
              y += 1
            end
            begin
              guess_date = Date.new(y, m, base_date.day)
            rescue ArgumentError
              # This might happen e.g. 31st Feb,
              # so just carry on to the next month
              next
            end
          end

        when :yearly
          # Skip ahead a year at a time unless we meet the context
          guess_date = Date.new(now.year, base_date.month, base_date.day)
          while guess_date < now
            guess_date += 1.year
          end

      end
    end

    @context[:now] = guess_date if update_now
    return guess_date
  else
    return nil
  end
end

#parse_date_range(text) ⇒ Object



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
# File 'lib/medjool/parser.rb', line 16

def parse_date_range(text)
  if bits = Medjool::DATE_RANGE_MATCHER.match(text.strip)
    if bits[1].present?
      prefix = bits[1].strip.sub(/:$/, '')
    else
      prefix = nil
    end

    if bits[52]
      # October
      if month_start = self.parse(text.strip, update_now = false)
        month_end = month_start.end_of_month
        return Medjool::DateRange.new(month_start, month_end, prefix)
      end
    elsif bits[3]
      # 12-15 Oct
      # Start is 12 Oct
      end_month = bits[17]
      start_month = bits[6] || end_month
      range_start = self.parse("#{bits[4]} #{start_month}", update_now = false)
      # End is 15 Oct
      range_end = self.parse("#{bits[16]} #{end_month}", update_now = false)
      return Medjool::DateRange.new(range_start, range_end, prefix)
    elsif bits[29]
      # Oct 12-15
      # Start is 12 Oct
      end_month = bits[41]
      start_month = bits[29] || end_month

      range_start = self.parse("#{bits[39]} #{start_month}", update_now = false)
      # End is 15 Oct
      range_end = self.parse("#{bits[51]} #{end_month}", update_now = false)
      return Medjool::DateRange.new(range_start, range_end, prefix)
    end
  end
end