Class: Lita::Handlers::OnewheelBaileys

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

Instance Method Summary collapse

Instance Method Details

#get_abv(beer_desc) ⇒ Object



120
121
122
123
124
# File 'lib/lita/handlers/onewheel_baileys.rb', line 120

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%”



127
128
129
130
131
132
133
# File 'lib/lita/handlers/onewheel_baileys.rb', line 127

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.



136
137
138
139
140
141
142
143
144
# File 'lib/lita/handlers/onewheel_baileys.rb', line 136

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



63
64
65
66
67
68
69
# File 'lib/lita/handlers/onewheel_baileys.rb', line 63

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.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/lita/handlers/onewheel_baileys.rb', line 148

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



71
72
73
74
75
76
77
78
79
80
# File 'lib/lita/handlers/onewheel_baileys.rb', line 71

def get_source
  Lita.logger.debug "get_source started"
  unless (response = redis.get('page_response'))
    Lita.logger.info 'No cached result found, fetching.'
    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…



171
172
173
174
175
176
177
178
179
180
# File 'lib/lita/handlers/onewheel_baileys.rb', line 171

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/lita/handlers/onewheel_baileys.rb', line 85

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)

    Lita.logger.debug "Adding tap #{tap}"

    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,
        search: full_text_search
    }
  end
  gimme_what_you_got
end

#send_response(tap, datum, response) ⇒ Object



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

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