Class: Lita::Handlers::OnewheelHalfstaff

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/onewheel_halfstaff.rb

Instance Method Summary collapse

Instance Method Details

#does_today_match_date_range(month_start, day_start, month_end, day_end, current_year) ⇒ Object



78
79
80
81
82
# File 'lib/lita/handlers/onewheel_halfstaff.rb', line 78

def does_today_match_date_range(month_start, day_start, month_end, day_end, current_year)
  start_time = DateTime::parse("#{month_start} #{day_start} #{current_year} 00:00")
  end_time = DateTime::parse("#{month_end} #{day_end} #{current_year} 23:59")
  return (start_time..end_time).include? Time.now
end

#get_flag_dataObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lita/handlers/onewheel_halfstaff.rb', line 30

def get_flag_data
  flag_html = RestClient.get 'http://www.flagsexpress.com/HalfStaff_s/1852.htm'
  results = []
  noko_flag = Nokogiri::HTML flag_html
  noko_flag.css('a').each do |a_tag|
    if a_tag['href'].match /http\:\/\/www\.flagsexpress\.com\/Articles\.asp\?ID\=/i
      if is_at_half_staff(a_tag.text)
        pieces = a_tag.text.split(/ - /)
        Lita.logger.info 'Returning flag data'
        results.push "#{pieces[1]} - #{pieces[2]} - #{a_tag['href']}"
      end
    end
  end
  results
end

#get_flag_status(response) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lita/handlers/onewheel_halfstaff.rb', line 17

def get_flag_status(response)
  flag_data = get_flag_data
  if flag_data.empty?
    Lita.logger.info 'No flag match for today.'
    response.reply ["Everything's cool, yo.", 'No half staff known.'].sample
  else
    Lita.logger.info 'Got a match on the flag.'
    flag_data.each do |reply|
      response.reply reply
    end
  end
end

#get_history(response) ⇒ Object



84
85
86
# File 'lib/lita/handlers/onewheel_halfstaff.rb', line 84

def get_history(response)
  response.reply 'https://en.wikipedia.org/wiki/Half-mast'
end

#is_at_half_staff(text) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lita/handlers/onewheel_halfstaff.rb', line 46

def is_at_half_staff(text)
  half_staff = false
  pieces = text.split(/ - /)
  current_year = Date::today.year
  if pieces[0].match(/#{current_year}/)
    Lita.logger.info "Checking for flag date match on #{text}"
    if date_matches = pieces[0].match(/(\w+\s+\d+,\s+\d+)/)   # February 26, 2016
      # Lita.logger.info 'Standard'
      # Lita.logger.info date_matches[1]
      date = Date::parse(date_matches[1])
      half_staff = date == Date::today
    elsif date_matches = pieces[0].match(/(\w+)\s+(\d+)-(\d+)/)   # March 5-11, 2016
      # Lita.logger.info 'Date range'
      month = date_matches[1]
      day_start = date_matches[2]
      day_end = date_matches[3]
      half_staff = does_today_match_date_range(month, day_start, month, day_end, current_year)
    elsif date_matches = pieces[0].match(/(\w+)\s+(\d+) until sunset \w+, (\w+)\s+(\d+)/i)   # May 3 until sunset Sunday, December 12
      # Lita.logger.info 'until sunset'
      start_month = date_matches[1]
      start_day = date_matches[2]
      end_month = date_matches[3]
      end_day = date_matches[4]
      half_staff = does_today_match_date_range(start_month, start_day, end_month, end_day, current_year)
    else
      Lita.logger.info "Couldn't match #{pieces[0]}"
    end
  end

  half_staff
end