Class: Lita::Handlers::OnewheelBeerProperPint

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

Instance Method Summary collapse

Instance Method Details

#get_beer_desc(noko) ⇒ Object

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



102
103
104
105
106
107
108
# File 'lib/lita/handlers/onewheel_beer_properpint.rb', line 102

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_sourceObject



48
49
50
51
52
53
54
55
56
# File 'lib/lita/handlers/onewheel_beer_properpint.rb', line 48

def get_source
  # unless (response = redis.get('page_response'))
  #   Lita.logger.info 'No cached result found, fetching.'
  Lita.logger.info 'Getting https://my-site-109373-104980.square.site'
  response = RestClient.get('https://my-site-109373-104980.square.site')
    # redis.setex('page_response', 1800, response)
  # end
  parse_response response
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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
# File 'lib/lita/handlers/onewheel_beer_properpint.rb', line 61

def parse_response(response)
  Lita.logger.debug "parse_response started."
  gimme_what_you_got = {}
  valid_products = {}
  products = {}
  response.split(/\n/).each do |line|
    if matches = line.match(/window.siteData = (.*)/)
      site_hash = JSON.parse matches[1].sub /;$/, ''
      valid_products = site_hash['page']['properties']['contentAreas']['userContent']['content']['cells'][0]['content']['properties']['menuItems']
    end
    if matches = line.match(/window.__BOOTSTRAP_STATE__ = (.*)/)
      whatevenintheshitsquarespace = JSON.parse matches[1].sub /;$/, ''
      products = whatevenintheshitsquarespace['commerceLinks']['products']
    end
  end

  valid_products
  products
  live_products = []

  valid_products.each do |p|
    live_products.push products[p]  unless products[p].nil?
  end

  live_products.each_with_index do |beer, idx|
    tap = idx + 1
    tap_type = ''
    beer_name = beer['name'].sub(/ 32oz Crowler/, '')
    full_text_search = "#{tap} #{beer_name}"

    gimme_what_you_got[tap] = {
        type: tap_type,
        name: beer_name.to_s,
        search: full_text_search
    }
  end

  gimme_what_you_got
end

#send_response(tap, datum, response) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/lita/handlers/onewheel_beer_properpint.rb', line 37

def send_response(tap, datum, response)
  reply = "Proper's tap #{tap}) #{get_tap_type_text(datum[:type])}"
  reply += "#{datum[:name]} "
  # reply += "- #{datum[:desc]}, "
  # reply += "Served in a #{datum[1]['glass']} glass.  "

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

  response.reply reply
end

#taps_list(response) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lita/handlers/onewheel_beer_properpint.rb', line 22

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