class Lita::Handlers::OnewheelBeerBaileys

Public Instance Methods

get_abv(beer_desc) click to toggle source
# File lib/lita/handlers/onewheel_beer_baileys.rb, line 137
def get_abv(beer_desc)
  if (abv_matches = beer_desc.match(/\d+\.\d+%/))
    abv_matches.to_s.sub '%', ''
  end
end
get_beer_desc(noko) click to toggle source

Return the desc of the beer, “Amber ale 6.9%”

# File lib/lita/handlers/onewheel_beer_baileys.rb, line 144
def get_beer_desc(noko)
  beer_desc = ''
  if (beer_desc_matchdata = noko.to_s.gsub(/\n/, '').match(/(<br\s*\/*>)(.+%) /))
    beer_desc = beer_desc_matchdata[2].gsub(/\s+/, ' ').strip
  end
  beer_desc
end
get_brewery(noko) click to toggle source

Get the brewery from the node, return it or blank.

# File lib/lita/handlers/onewheel_beer_baileys.rb, line 153
def get_brewery(noko)
  brewery = ''
  if (node = noko.css('span a').first)
    brewery = node.children.to_s.gsub(/\n/, '')
    brewery.gsub! /RBBA/, ''
    brewery.strip!
  end
  brewery
end
get_display_prices(prices) click to toggle source
# File lib/lita/handlers/onewheel_beer_baileys.rb, line 79
def get_display_prices(prices)
  price_array = []
  prices.each do |p|
    price_array.push "#{p[:size]} - $#{p[:cost]}"
  end
  price_array.join ' | '
end
get_prices(noko) click to toggle source

Returns … There are a bunch of hidden html fields that get stripped after sanitize.

# File lib/lita/handlers/onewheel_beer_baileys.rb, line 165
def get_prices(noko)
  prices_str = noko.css('div#prices').children.to_s.strip
  prices = Sanitize.clean(prices_str)
      .gsub(/We're Sorry/, '')
      .gsub(/Inventory Restriction/, '')
      .gsub(/Inventory Failure/, '')
      .gsub('Success!', '')
      .gsub(/\s+/, ' ')
      .strip
  price_points = prices.split(/\s\|\s/)
  prices_array = []
  price_points.each do |price|
    size = price.match /\d+(oz|cl)/
    dollars = price.match(/\$\d+\.*\d*/).to_s.sub('$', '')
    crowler = price.match ' Crowler'
    size = size.to_s + crowler.to_s
    p = {size: size, cost: dollars}
    prices_array.push p
  end
  prices_array
end
get_source() click to toggle source
# File lib/lita/handlers/onewheel_beer_baileys.rb, line 87
def get_source
  # https://visualizeapi.com/api/baileys
  # Lita.logger.debug "get_source started"
  # unless (response = redis.get('page_response'))
  #   Lita.logger.info 'No cached result found, fetching.'
  Lita.logger.info 'Getting http://www.baileystaproom.com/draft-list/'
  response = RestClient.get('http://www.baileystaproom.com/draft-list/')
    # redis.setex('page_response', 1800, response)
  # end
  response.gsub! '<div id="responsecontainer"">', ''
  parse_response response
end
get_tap_name(noko) click to toggle source

Returns 1, 2, Cask 3, Nitro 4…

# File lib/lita/handlers/onewheel_beer_baileys.rb, line 188
def get_tap_name(noko)
  noko.css('span')
      .first
      .children
      .first
      .to_s
      .match(/[\w ]+\:/)
      .to_s
      .sub(/\:$/, '')
end
parse_response(response) click to toggle source

This is the worker bee- decoding the html into our “standard” document. Future implementations could simply override this implementation-specific code to help this grow more widely.

# File lib/lita/handlers/onewheel_beer_baileys.rb, line 103
def parse_response(response)
  Lita.logger.debug "parse_response started."
  gimme_what_you_got = {}
  noko = Nokogiri.HTML response
  noko.css('div#boxfielddata').each do |beer_node|
    # gimme_what_you_got
    tap_name = get_tap_name(beer_node)
    tap = tap_name.match(/\d+/).to_s
    tap_type = tap_name.match(/(cask|nitro)/i).to_s

    remaining = beer_node.attributes['title'].to_s

    brewery = get_brewery(beer_node)
    beer_name = beer_node.css('span i').first.children.to_s
    beer_desc = get_beer_desc(beer_node)
    abv = get_abv(beer_desc)
    full_text_search = "#{tap.sub /\d+/, ''} #{brewery} #{beer_name} #{beer_desc.to_s.gsub /\d+\.*\d*%*/, ''}"
    prices = get_prices(beer_node)

    gimme_what_you_got[tap] = {
        type: tap_type,
        remaining: remaining,
        brewery: brewery.to_s,
        name: beer_name.to_s,
        desc: beer_desc.to_s,
        abv: abv.to_f,
        prices: prices,
        price: prices[1][:cost],
        search: full_text_search
    }
  end
  gimme_what_you_got
end
send_response(tap, datum, response) click to toggle source
# File lib/lita/handlers/onewheel_beer_baileys.rb, line 65
def send_response(tap, datum, response)
  reply = "Bailey's tap #{tap}) #{get_tap_type_text(datum[:type])}"
  reply += "#{datum[:brewery]} "
  reply += "#{datum[:name]} "
  reply += "- #{datum[:desc]}, "
  # reply += "Served in a #{datum[1]['glass']} glass.  "
  reply += "#{get_display_prices datum[:prices]}, "
  reply += "#{datum[:remaining]}"

  Lita.logger.info "send_response: Replying with #{reply}"

  response.reply reply
end
taps_list(response) click to toggle source
# File lib/lita/handlers/onewheel_beer_baileys.rb, line 49
def taps_list(response)
  # wakka wakka
  beers = self.get_source
  reply = "Bailey's taps: "
  beers.each do |tap, datum|
    reply += "#{tap}) "
    reply += get_tap_type_text(datum[:type])
    reply += 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