class Lita::Handlers::OnewheelBeerBase

Public Instance Methods

get_source() click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 155
def get_source
  raise Exception
end
get_tap_type_text(type) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 23
def get_tap_type_text(type)
  (type.nil? or type.empty?) ? '' : "(#{type}) "
end
taps_by_abv(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 48
def taps_by_abv(response)
  beers = get_source
  beers.each do |tap, datum|
    if datum[:abv].to_f == 0.0
      next
    end
    query = response.matches[0][0].strip
    # Search directly by abv matcher.
    if (abv_matches = query.match(/([><=]+)\s*(\d+\.*\d*)/))
      direction = abv_matches.to_s.match(/[<>=]+/).to_s
      abv_requested = abv_matches.to_s.match(/\d+.*\d*/).to_s
      if direction == '>' and datum[:abv].to_f > abv_requested.to_f
        send_response(tap, datum, response)
      end
      if direction == '<' and datum[:abv].to_f < abv_requested.to_f
        send_response(tap, datum, response)
      end
      if direction == '>=' and datum[:abv].to_f >= abv_requested.to_f
        send_response(tap, datum, response)
      end
      if direction == '<=' and datum[:abv].to_f <= abv_requested.to_f
        send_response(tap, datum, response)
      end
    end
  end
end
taps_by_price(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 75
def taps_by_price(response)
  beers = get_source
  beers.each do |tap, datum|
    # if datum[:prices][1][:cost].to_f == 0.0
    #   next
    # end

    query = response.matches[0][0].strip
    # Search directly by tap number OR full text match.
    if (price_matches = query.match(/([><=]+)\s*\$(\d+\.*\d*)/))
      direction = price_matches.to_s.match(/[<>=]+/).to_s
      price_requested = price_matches.to_s.match(/\d+.*\d*/).to_s
      if direction == '>' and datum[:price].to_f > price_requested.to_f
        send_response(tap, datum, response)
      end
      if direction == '<' and datum[:price].to_f < price_requested.to_f
        send_response(tap, datum, response)
      end
      if direction == '>=' and datum[:price].to_f >= price_requested.to_f
        send_response(tap, datum, response)
      end
      if direction == '<=' and datum[:price].to_f <= price_requested.to_f
        send_response(tap, datum, response)
      end
    end
  end
end
taps_by_random(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 103
def taps_by_random(response)
  beers = get_source
  beer = beers.to_a.sample
  send_response(beer[0], beer[1], response)
end
taps_by_remaining(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 109
def taps_by_remaining(response)
  beers = get_source
  response_sent = false
  low_tap = nil
  beers.each do |tap, datum|
    unless low_tap
      low_tap = tap
    end
    if low_tap and beers[low_tap][:remaining] > datum[:remaining]
      low_tap = tap
    end
    if datum[:remaining].to_i <= 10
      send_response(tap, datum, response)
      response_sent = true
    end
  end
end
taps_deets(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 27
def taps_deets(response)
  Lita.logger.debug 'taps_deets started'
  beers = get_source
  beers.each do |tap, datum|
    query = response.matches[0][0].strip
    # Search directly by tap number OR full text match.
    # Let's make cask and nitro taps specific.
    Lita.logger.debug "Searching for #{query} within #{tap}, #{datum[:search]}"
    if query.match(/^\d+$/)
      # Short circuit if we're searching only by number.
      if tap.to_s == query.to_s
        Lita.logger.debug "#{query} matched #{tap} by number"
        send_response(tap, datum, response)
      end
    elsif (datum[:search].to_s.match(/#{query}/i)) or (datum[:type].to_s.downcase.match(/#{query}/i))  # Cask and Nitro
      Lita.logger.debug "#{query} matched #{datum[:search]} by text"
      send_response(tap, datum, response)
    end
  end
end
taps_high_abv(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 141
def taps_high_abv(response)
  beers = get_source
  high_tap = nil
  beers.each do |tap, datum|
    unless high_tap
      high_tap = tap
    end
    if datum[:abv] != 0 and beers[high_tap][:abv] < datum[:abv]
      high_tap = tap
    end
  end
  send_response(high_tap, beers[high_tap], response)
end
taps_list(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 8
def taps_list(response)
  beers = self.get_source
  reply = 'taps: '
  beers.each do |tap, datum|
    reply += "#{tap}) "
    reply += get_tap_type_text(datum[:type])
    reply += (datum[:brewery].to_s.empty?)? '' : datum[:brewery].to_s + ' '
    reply += (datum[:name].to_s.empty?)? '' : datum[:name].to_s + '  '
  end
  reply = reply.strip.sub /,\s*$/, ''

  Lita.logger.info "Replying with #{reply}"
  response.reply reply
end
taps_low_abv(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_base.rb, line 127
def taps_low_abv(response)
  beers = get_source
  low_tap = nil
  beers.each do |tap, datum|
    unless low_tap
      low_tap = tap
    end
    if datum[:abv] != 0 and beers[low_tap][:abv] > datum[:abv]
      low_tap = tap
    end
  end
  send_response(low_tap, beers[low_tap], response)
end