Skip to content

Instantly share code, notes, and snippets.

@mjc-gh
Last active September 6, 2018 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjc-gh/f554ea79d901742dda9d2ee75bc1dd5b to your computer and use it in GitHub Desktop.
Save mjc-gh/f554ea79d901742dda9d2ee75bc1dd5b to your computer and use it in GitHub Desktop.
require 'active_support/multibyte/chars'
require 'active_support/core_ext/string/inflections'
require 'json'
require 'httpi'
require 'nokogiri'
STG_CONTROL_FILE = '.last-date'.freeze
STG_CHANNEL_URL = 'https://hooks.slack.com/services/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'.freeze
STG_SKIPPED_MENUS = %w(Pastries Breakfast).freeze
STG_DESC_EXP = /\s?\(?V?\)? ?\(?(DF)?\)? ?\(?(GF)?\)? ?\(?N?\)?\z/
html = Nokogiri::HTML(HTTPI.get('https://www.smiletogonyc.com/location/howard-street/').body)
date = html.css('article header').text.strip.titleize
last_run_date = File.read(STG_CONTROL_FILE) rescue nil
exit if date == last_run_date
pages = html.css('script[type="application/ld+json"]').map { |script| JSON.parse(script) }
pages.select! { |pg| pg['@type'] == 'Menu' }
msg = "_#{date}_"
pages.each do |pg|
menu = pg['hasMenuSection']
menu = menu.first if Array === menu
next if STG_SKIPPED_MENUS.include? pg['name']
next unless Array === menu['hasMenuItem']
msg << "\n\n"
msg << pg['name']
items = menu['hasMenuItem'].map do |item|
desc = item['description'].gsub(STG_DESC_EXP) do |match|
" (#{match.gsub(/\(|\)/, '').strip.tr(' ', '/')})" unless match.empty?
end
msg << "\n- #{item['name'].titleize} #{desc.strip}"
end
end
File.open(STG_CONTROL_FILE, 'w+') do |file|
file << date
end
HTTPI.post(
HTTPI::Request.new(
url: STG_CHANNEL_URL,
body: JSON.dump(
channel: '#seems-less',
username: 'SMILE TO GO',
text: msg,
icon_emoji: ':smiletogo:'
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment