Class: Lita::Handlers::OnewheelBaileys

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

Instance Method Summary collapse

Instance Method Details

#get_abv(beer_desc) ⇒ Object



148
149
150
151
152
# File 'lib/lita/handlers/onewheel_baileys.rb', line 148

def get_abv(beer_desc)
  if (abv_matches = beer_desc.match(/\d+\.\d+%/))
    abv_matches.to_s.sub '%', ''
  end
end

#get_baileysObject



114
115
116
117
118
# File 'lib/lita/handlers/onewheel_baileys.rb', line 114

def get_baileys
  response = RestClient.get('http://www.baileystaproom.com/draft-list/')
  response.gsub! '<div id="responsecontainer"">', ''
  parse_response response
end

#get_beer_desc(noko) ⇒ Object

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



155
156
157
158
159
160
161
# File 'lib/lita/handlers/onewheel_baileys.rb', line 155

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) ⇒ Object

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



164
165
166
167
168
169
170
171
172
# File 'lib/lita/handlers/onewheel_baileys.rb', line 164

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) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/lita/handlers/onewheel_baileys.rb', line 106

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) ⇒ Object

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/lita/handlers/onewheel_baileys.rb', line 176

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*/
    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_tap_name(noko) ⇒ Object

Returns 1, 2, Cask 3, Nitro 4…



199
200
201
202
203
204
205
206
207
208
# File 'lib/lita/handlers/onewheel_baileys.rb', line 199

def get_tap_name(noko)
  noko.css('span')
      .first
      .children
      .first
      .to_s
      .match(/[\w ]+\:/)
      .to_s
      .sub(/\:$/, '')
end

#parse_response(response) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/lita/handlers/onewheel_baileys.rb', line 120

def parse_response(response)
  gimme_what_you_got = {}
  noko = Nokogiri.HTML response
  noko.css('div#boxfielddata').each do |beer_node|
    # gimme_what_you_got
    tap = get_tap_name(beer_node)
    remaining = beer_node.attributes['title']

    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] = {
        remaining: remaining,
        brewery: brewery.to_s,
        name: beer_name.to_s,
        desc: beer_desc.to_s,
        abv: abv.to_f,
        prices: prices,
        search: full_text_search
    }
  end
  gimme_what_you_got
end

#send_response(tap, datum, response) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/lita/handlers/onewheel_baileys.rb', line 95

def send_response(tap, datum, response)
  reply = "Bailey's tap #{tap}) "
  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]}"
  response.reply reply
end

#taps_by_abv(response) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lita/handlers/onewheel_baileys.rb', line 53

def taps_by_abv(response)
  beers = get_baileys
  beers.each do |tap, datum|
    if datum[:abv].to_f == 0.0
      next
    end
    query = response.matches[0][0].strip
    # Search directly by tap number OR full text match.
    if (abv_matches = query.match(/([><])(\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
    end
  end
end

#taps_by_price(response) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lita/handlers/onewheel_baileys.rb', line 74

def taps_by_price(response)
  beers = get_baileys
  beers.each do |tap, datum|
    if datum[:abv].to_f == 0.0
      next
    end
    query = response.matches[0][0].strip
    # Search directly by tap number OR full text match.
    if (abv_matches = query.match(/([><])(\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
    end
  end
end

#taps_deets(response) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/lita/handlers/onewheel_baileys.rb', line 42

def taps_deets(response)
  beers = get_baileys
  beers.each do |tap, datum|
    query = response.matches[0][0].strip
    # Search directly by tap number OR full text match.
    if (query.match(/^\d+$/) and tap == query) or (datum[:search].match(/#{query}/i))
      send_response(tap, datum, response)
    end
  end
end

#taps_list(response) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lita/handlers/onewheel_baileys.rb', line 28

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

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