class Lita::Handlers::OnewheelBeerCraftpourhouse

Public Instance Methods

get_source() click to toggle source
# File lib/lita/handlers/onewheel_beer_craftpourhouse.rb, line 62
def get_source
  Lita.logger.debug 'get_source started'
  unless (response = redis.get('page_response'))
    Lita.logger.info 'No cached result found, fetching.'
    response = RestClient.get('https://bu7gqj6j5j.execute-api.us-east-1.amazonaws.com/prod/doTheScrapeyThing')
    redis.setex('page_response', 1800, response)
  end
  parse_response response
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_craftpourhouse.rb, line 75
def parse_response(response)
  Lita.logger.debug 'parse_response started.'
  gimme_what_you_got = {}

  beers = JSON.parse(RestClient.get('https://bu7gqj6j5j.execute-api.us-east-1.amazonaws.com/prod/doTheScrapeyThing'))
  beers['result'].each_with_index do |beer, index|
    # abv_node = /\d+\.\d+\%/.match(beer_node.css('span.abvABV').text)
    # if abv_node
    #   abv = abv_node[0]
    #   abv.sub! /\%/, ''
    # end
    #
    # ibu_node = /IBU: \d+/.match(beer_node.css('span.abvABV').text)
    # if ibu_node
    #   ibu = ibu_node[0]
    #   ibu.sub! /IBU /, ''
    # end

    full_text_search = "#{beer['brewery']} #{beer['name'].to_s.gsub /(\d+|')/, ''} #{beer['type']} #{beer['desc'].to_s.gsub(/\d+\.*\d*%*/, '')}"

    # price_node = beer_node.css('td')[1].children.to_s
    # price = (price_node.sub /\$/, '').to_f

    gimme_what_you_got[beer['tap']] = {
    #     type: tap_type,
    #     remaining: remaining,
        brewery: beer['brewery'].to_s,
        name: beer['name'].to_s,
        abv: beer['abv'].to_f,
        ibu: beer['ibu'].to_i,
        desc: beer['desc'].to_s,
        # price: price,
        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_craftpourhouse.rb, line 44
def send_response(tap, datum, response)
  reply = "Craftpourhouse tap #{tap}) #{get_tap_type_text(datum[:type])}"
  # reply += "#{datum[:brewery]} "
  reply += "#{datum[:name]} "
  reply += "- #{datum[:desc]}, "  if datum[:desc]
  # reply += "Served in a #{datum[1]['glass']} glass.  "
  # reply += "#{datum[:remaining]}"
  reply += "#{datum[:abv]}%"
  if datum[:ibu] > 0
    reply += ", #{datum[:ibu]}"
  end
  # reply += "$#{datum[:price].to_s.sub '.0', ''}"

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

  response.reply reply
end