Class: Lita::Handlers::OnewheelBeerBase

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/onewheel_beer_base.rb

Instance Method Summary collapse

Instance Method Details

#get_sourceObject

Raises:

  • (Exception)


155
156
157
# File 'lib/lita/handlers/onewheel_beer_base.rb', line 155

def get_source
  raise Exception
end

#get_tap_type_text(type) ⇒ Object



23
24
25
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object



103
104
105
106
107
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# 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) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# 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