Class: PickupLine::Parser
- Inherits:
-
Object
- Object
- PickupLine::Parser
- Defined in:
- lib/pickup_line/parser.rb
Instance Method Summary collapse
-
#initialize(date) ⇒ Parser
constructor
A new instance of Parser.
- #locate(str) ⇒ Object
Constructor Details
#initialize(date) ⇒ Parser
Returns a new instance of Parser.
6 7 8 |
# File 'lib/pickup_line/parser.rb', line 6 def initialize(date) @date = date end |
Instance Method Details
#locate(str) ⇒ Object
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 |
# File 'lib/pickup_line/parser.rb', line 10 def locate(str) str = str.downcase words = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'] # Full month names with date if str =~ /(#{Date::MONTHNAMES.compact.map(&:downcase).join('|')})\s+(\d{1,2})(?!\d)/ month_num = Date::MONTHNAMES.compact.map(&:downcase).index($1) + 1 future_date $2.to_i, month_num, @date.year # Abbreviated month names with date elsif str =~ /(#{Date::ABBR_MONTHNAMES.compact.map(&:downcase).join('|')})\.?\s+(\d{1,2})(?!\d)/ month_num = Date::ABBR_MONTHNAMES.compact.map(&:downcase).index($1) + 1 day = $2 ? $2.to_i : -1 future_date day, month_num, @date.year # (next) Day names elsif str =~ /next\s+(#{Date::DAYNAMES.map(&:downcase).join('|')})/ dindex = Date::DAYNAMES.map(&:downcase).index $1 d = @date d += 1 until d.wday == 0 # go to next week d += 1 until d.wday == dindex d # Day names elsif str =~ /(#{Date::DAYNAMES.map(&:downcase).join('|')})/ dindex = Date::DAYNAMES.map(&:downcase).index $1 d = @date d += 1 until d.wday == dindex d # In N days elsif str =~ /in\s+(\d)\s+days?/ @date + $1.to_i # In N days (with words) elsif str =~ /in\s+(#{words.join('|')})\s+days?/ days = words.index($1) + 1 @date + days # Today elsif str.include?('today') @date # Tomorrow elsif str.include?('tomorrow') @date + 1 end end |