class StanfordMenus::Menus::GSB

Constants

URL

Public Instance Methods

all() { |grouped_items| ... } click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 9
def all(&_block)
  (block_given? ? yield(grouped_items) : grouped_items).map do |station, items|
    Struct.new(:station, :items).new(station, items)
  end
end
filtered_by_category(station_query) click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 15
def filtered_by_category(station_query)
  normalized_query = normalize_station(station_query)
  all do |grouped_items|
    grouped_items.select do |station, _|
      (normalize_station(station) & normalized_query).any? ||
        normalize_station(station).join('').include?(normalized_query.join(''))
    end
  end
end
filtered_by_price(price_query) click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 25
def filtered_by_price(price_query)
  all do |grouped_items|
    grouped_items.each_with_object({}) do |(station, items), hash|
      next unless (filted_items = items.select { |item| item.costs_less_than?(price_query) }).any?
      hash[station] = filted_items
    end
  end
end

Private Instance Methods

grouped_items() click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 36
def grouped_items
  items.select(&:special).group_by(&:station)
end
items() { |item| ... } click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 49
def items
  return to_enum(:items) unless block_given?
  json.values.each do |item_json|
    yield Item.new(item_json)
  end
end
json() click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 56
def json
  JSON.parse(json_string_from_response)
end
json_string_from_response() click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 64
def json_string_from_response
  @json_string_from_response ||= begin
    response[/Bamco\.menu_items = (.*)\n/]
    Regexp.last_match.to_s.gsub(/;$/, '').gsub('Bamco.menu_items = ', '')
  end
end
normalize_station(string) click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 40
def normalize_station(string)
  string
    .downcase
    .gsub(/[[:punct:]]/, ' ')
    .split(/\s/)
    .compact
    .reject(&:empty?)
end
response() click to toggle source
# File lib/stanford_menus/menus/gsb.rb, line 60
def response
  @response ||= Faraday.get(URL).body
end