Class: Lita::Handlers::OnewheelBeerWework
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::OnewheelBeerWework
- Defined in:
- lib/lita/handlers/onewheel_beer_wework.rb
Constant Summary collapse
- REDIS_KEY =
'onewheel-beer-wework'- LOCATIONS =
%w(1fs 1fn 2fs 2fn 3fn)
Instance Method Summary collapse
- #command_add_beer(response) ⇒ Object
- #command_fetch_beer(response) ⇒ Object
- #command_list_beers(response) ⇒ Object
- #get_floor_and_keg_name(keg) ⇒ Object
- #get_json_beers(request, response) ⇒ Object
- #get_values_that_start_with_key(key) ⇒ Object
Instance Method Details
#command_add_beer(response) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lita/handlers/onewheel_beer_wework.rb', line 24 def command_add_beer(response) reply = '' key = response.matches[0][0].downcase unless LOCATIONS.include? key Lita.logger.info "#{key} was not found in the floors array." return end beer_name = response.matches[0][1] key = key[0..1] + key[-1].downcase if beer_name.downcase == 'blow' stored = JSON.parse(redis.hget(REDIS_KEY, key)) stored['name'] += ' BLOWN' reply = "#{key} #{stored['name']}" value = stored.to_json else value = { name: beer_name, user: response.user.name, time: Time.now }.to_json reply = "Logged #{beer_name} at #{key}!" end redis.hset(REDIS_KEY, key, value) response.reply reply end |
#command_fetch_beer(response) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/lita/handlers/onewheel_beer_wework.rb', line 52 def command_fetch_beer(response) key = response.matches[0][0] values = get_values_that_start_with_key(key) values.each do |keg| floor_id, keg_name = get_floor_and_keg_name(keg) response.reply "#{floor_id}: #{keg_name}" end end |
#command_list_beers(response) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/lita/handlers/onewheel_beer_wework.rb', line 68 def command_list_beers(response) LOCATIONS.each do |floor| if (data = redis.hget(REDIS_KEY, floor)) floor, keg_name = get_floor_and_keg_name(floor => data) response.reply "#{floor}: #{keg_name}" end end end |
#get_floor_and_keg_name(keg) ⇒ Object
62 63 64 65 66 |
# File 'lib/lita/handlers/onewheel_beer_wework.rb', line 62 def get_floor_and_keg_name(keg) floor_id = keg.keys[0] = JSON.parse(keg[floor_id]) return floor_id, ['name'] end |
#get_json_beers(request, response) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/lita/handlers/onewheel_beer_wework.rb', line 77 def get_json_beers(request, response) all_beers = [] LOCATIONS.each do |floor| if (data = redis.hget(REDIS_KEY, floor)) d = JSON.parse(data) d['id'] = floor all_beers.push d end end Lita.logger.info 'Sending beers via HTTP.' response.headers['Content-Type'] = 'application/json' response.write all_beers.to_json end |
#get_values_that_start_with_key(key) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lita/handlers/onewheel_beer_wework.rb', line 91 def get_values_that_start_with_key(key) values = [] all = redis.hgetall(REDIS_KEY) all.each do |all_key, all_val| if all_key =~ /^#{key}/i values.push all_key => all_val end end values end |