module KfcMenu
Constants
- PROMO_CATEGORY_PATTERN
- STYLE_TITLE
Public Instance Methods
Source
# File lib/kfc_menu.rb, line 90 def extract_category_items(category) items = Array(category["products"]).filter_map do |product| next unless product_visible?(product) product_item(product) end Array(category["categories"]).each do |child| items.concat(extract_category_items(child)) end items end
Source
# File lib/kfc_menu.rb, line 136 def format_item_line(item) if item[:price] && !item[:price].empty? "#{item[:name]} โ #{item[:price]}" else item[:name].to_s end end
Source
# File lib/kfc_menu.rb, line 144 def format_item_paragraphs(items, max_bytes: KfcConfig::MENU_PARAGRAPH_MAX_BYTES, separator: ", ") entries = items.map { |item| format_item_line(item) } ParagraphFormat.build_paragraphs(entries, max_bytes: max_bytes, separator: separator) end
Source
# File lib/kfc_menu.rb, line 192 def format_not_found(location) header = IrcFormat.report_header(tag: "KFC", title: location.to_s.strip, style: STYLE_TITLE) [ header, "Could not find a KFC store near #{location}.", IrcFormat.report_footer("store lookup", "kfc.com.au") ].join("\n") end
Source
# File lib/kfc_menu.rb, line 36 def format_price(value) return nil if value.nil? amount = value.to_f amount = amount / 100.0 if amount >= 100 format("$%.2f", amount) end
Source
# File lib/kfc_menu.rb, line 168 def format_report(store:, promo_items:, menu_items:) header = IrcFormat.report_header(tag: "KFC", title: banner_title(store), style: STYLE_TITLE) lines = [header] detail = store_detail_line(store) lines << detail unless detail.empty? lines.concat(format_section("--- Promos & Specials ---", promo_items)) lines.concat(format_section("--- Menu ---", menu_items)) if promo_items.empty? && menu_items.empty? lines << "No menu items found for this store." end promo_count = promo_items.length menu_count = menu_items.length footer_bits = [] footer_bits << "#{promo_count} promos" if promo_count.positive? footer_bits << "#{menu_count} items" if menu_count.positive? footer_bits << store[:id] if store[:id] && !store[:id].empty? lines << IrcFormat.report_footer(*footer_bits, "kfc.com.au") lines.join("\n") end
Source
# File lib/kfc_menu.rb, line 149 def format_section(title, items) return [] if items.empty? [title, *format_item_paragraphs(items)] end
Source
# File lib/kfc_menu.rb, line 65 def localized_value(value) if value.is_a?(Hash) text = value["value"] || value[:value] text = text.to_s.strip return text unless text.empty? end if value.is_a?(Array) english = value.find { |entry| entry["lang"].to_s.downcase.start_with?("en") } candidate = english || value.first return localized_value(candidate) if candidate end text = value.to_s.strip text.empty? ? nil : text end
Source
# File lib/kfc_menu.rb, line 104 def partition_items(categories) promo_items = [] menu_items = [] seen = {} walk_categories(categories) do |category| name = category["name"].to_s next if hidden_category?(name) target = promo_category?(name) ? promo_items : menu_items extract_category_items(category).each do |item| key = item[:name].downcase next if key.empty? || seen[key] seen[key] = true target << item end end { promo_items: promo_items.first(KfcConfig::MAX_PROMO_ITEMS), menu_items: menu_items.first(KfcConfig::MAX_MENU_ITEMS) } end
Source
# File lib/kfc_menu.rb, line 82 def product_item(product) { name: product_name(product), price: product_price(product), kind: :product } end
Source
# File lib/kfc_menu.rb, line 52 def product_name(product) localized_value(product["name"]) || localized_value(product["dname"]) || product["title"].to_s.strip end
Source
# File lib/kfc_menu.rb, line 58 def product_price(product) return format_price(product["price"]) if product.key?("price") item = Array(product["items"]).find { |entry| entry.key?("price") } format_price(item["price"]) if item end
Source
# File lib/kfc_menu.rb, line 44 def product_visible?(product) return false if product["snoozed"] return false if product["visible"] == false name = product_name(product) !name.empty? end
Source
# File lib/kfc_menu.rb, line 24 def promo_category?(name) text = name.to_s.strip return false if text.empty? return false if hidden_category?(text) text.match?(PROMO_CATEGORY_PATTERN) end
Source
# File lib/kfc_menu.rb, line 161 def store_detail_line(store) parts = [] parts << store[:address] if store[:address] && !store[:address].empty? parts << store[:phone] if store[:phone] && !store[:phone].empty? parts.join(" ยท ") end
Source
# File lib/kfc_menu.rb, line 129 def walk_categories(categories, &block) Array(categories).each do |category| yield category walk_categories(category["categories"], &block) end end