Class: Lita::Handlers::OnewheelBeerBaileys

Inherits:
OnewheelBeerBase
  • Object
show all
Defined in:
lib/lita/handlers/onewheel_beer_baileys.rb

Instance Method Summary collapse

Instance Method Details

#get_abv(beer_desc) ⇒ Object



137
138
139
140
141
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 137

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

#get_beer_desc(noko) ⇒ Object

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



144
145
146
147
148
149
150
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 144

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.



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

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



79
80
81
82
83
84
85
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 79

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.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 165

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*/).to_s.sub('$', '')
    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_sourceObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 87

def get_source
  # https://visualizeapi.com/api/baileys
  # Lita.logger.debug "get_source started"
  # unless (response = redis.get('page_response'))
  #   Lita.logger.info 'No cached result found, fetching.'
  Lita.logger.info 'Getting http://www.baileystaproom.com/draft-list/'
  response = RestClient.get('http://www.baileystaproom.com/draft-list/')
    # redis.setex('page_response', 1800, response)
  # end
  response.gsub! '<div id="responsecontainer"">', ''
  parse_response response
end

#get_tap_name(noko) ⇒ Object

Returns 1, 2, Cask 3, Nitro 4…



188
189
190
191
192
193
194
195
196
197
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 188

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

#parse_response(response) ⇒ Object

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.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 103

def parse_response(response)
  Lita.logger.debug "parse_response started."
  gimme_what_you_got = {}
  noko = Nokogiri.HTML response
  noko.css('div#boxfielddata').each do |beer_node|
    # gimme_what_you_got
    tap_name = get_tap_name(beer_node)
    tap = tap_name.match(/\d+/).to_s
    tap_type = tap_name.match(/(cask|nitro)/i).to_s

    remaining = beer_node.attributes['title'].to_s

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

#send_response(tap, datum, response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 65

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

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

  response.reply reply
end

#taps_list(response) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lita/handlers/onewheel_beer_baileys.rb', line 49

def taps_list(response)
  # wakka wakka
  beers = self.get_source
  reply = "Bailey's taps: "
  beers.each do |tap, datum|
    reply += "#{tap}) "
    reply += get_tap_type_text(datum[:type])
    reply += 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