Skip to content

Commit

Permalink
Subclassing from beer-base.
Browse files Browse the repository at this point in the history
  • Loading branch information
onewheelskyward committed May 14, 2016
1 parent 9a399cd commit 8904891
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 142 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source 'https://rubygems.org'

gemspec

# For local development and base testing
# gem 'lita-onewheel-beer-base', :github => 'onewheelskyward/lita-onewheel-beer-base', branch: :master
144 changes: 3 additions & 141 deletions lib/lita/handlers/onewheel_baileys.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require 'rest-client'
require 'nokogiri'
require 'sanitize'
require 'lita-onewheel-beer-base'

module Lita
module Handlers
class OnewheelBaileys < Handler
class OnewheelBaileys < OnewheelBeerBase
route /^taps$/i,
:taps_list,
command: true,
help: {'taps' => 'Display the current taps at baileys.'}
help: {'taps' => 'Display the current taps.'}

route /^taps ([\w ]+)$/i,
:taps_deets,
Expand Down Expand Up @@ -45,141 +46,6 @@ class OnewheelBaileys < Handler
command: true,
help: {'tapslow' => 'Show me the highest abv keg.'}

def taps_list(response)
beers = get_source
reply = "Bailey's taps: "
beers.each do |tap, datum|
reply += "#{tap}) "
reply += get_tap_type_text(datum[:type])
reply += datum[:brewery] + ' '
reply += datum[:name] + ' '
end
reply = reply.strip.sub /,\s*$/, ''

Lita.logger.info "Replying with #{reply}"
response.reply reply
end

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.
if (query.match(/^\d+$/) and tap == query) or (datum[:search].match(/#{query}/i)) or (datum[:type].downcase.match(/#{query}/i)) # Cask and Nitro
send_response(tap, datum, response)
end
end
end

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

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[:prices][1][:cost].to_f > price_requested.to_f
send_response(tap, datum, response)
end
if direction == '<' and datum[:prices][1][:cost].to_f < price_requested.to_f
send_response(tap, datum, response)
end
if direction == '>=' and datum[:prices][1][:cost].to_f >= price_requested.to_f
send_response(tap, datum, response)
end
if direction == '<=' and datum[:prices][1][:cost].to_f <= price_requested.to_f
send_response(tap, datum, response)
end
end
end
end

def taps_by_random(response)
beers = get_source
beer = beers.to_a.sample
send_response(beer[0], beer[1], response)
end

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

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

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

def send_response(tap, datum, response)
reply = "Bailey's tap #{tap}) #{get_tap_type_text(datum[:type])}"
reply += "#{datum[:brewery]} "
Expand All @@ -194,10 +60,6 @@ def send_response(tap, datum, response)
response.reply reply
end

def get_tap_type_text(type)
(type.empty?) ? '' : "(#{type}) "
end

def get_display_prices(prices)
price_array = []
prices.each do |p|
Expand Down
4 changes: 3 additions & 1 deletion lita-onewheel-baileys.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'lita-onewheel-baileys'
spec.version = '3.8.0'
spec.version = '3.8.1'
spec.authors = ['Andrew Kreps']
spec.email = ['andrew.kreps@gmail.com']
spec.description = %q{Lita interface to Bailey's Taproom listings.}
Expand All @@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'sanitize', '~> 4.0'

spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'lita-onewheel-beer-base', '~> 1'

# spec.add_development_dependency 'pry-byebug', '~> 3.1'
spec.add_development_dependency 'rake', '~> 10.4'
spec.add_development_dependency 'rack-test', '~> 0.6'
Expand Down

0 comments on commit 8904891

Please sign in to comment.