Class: Lita::Handlers::OnewheelBeerPints

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

Instance Method Summary collapse

Instance Method Details

#get_sourceObject



71
72
73
74
75
76
77
78
79
# File 'lib/lita/handlers/onewheel_beer_pints.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.pintsbrewing.com/brew-menu/')
    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.



82
83
84
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lita/handlers/onewheel_beer_pints.rb', line 82

def parse_response(response)
  Lita.logger.debug 'parse_response started.'
  gimme_what_you_got = {}
  got_beer = false
  tap = 1
  beer_name = nil
  beer_abv = nil
  beer_ibu = nil
  beer_desc = nil

  noko = Nokogiri.HTML response
  noko.css('.entry-content p').each do |beer_node|
    # gimme_what_you_got
    if got_beer
      beer_desc = beer_node.children.to_s

      got_beer = false
      full_text_search = "#{beer_name} #{beer_desc.to_s.gsub /\d+\.*\d*%*/, ''}"

      gimme_what_you_got[tap] = {
          # type: tap_type,
          # brewery: brewery.to_s,
          name: beer_name.to_s,
          desc: beer_desc.to_s,
          abv: beer_abv.to_f,
          ibu: beer_ibu.to_i,
          # prices: prices,
          # price: prices[1][:cost],
          search: full_text_search
      }
      tap += 1
    end

    if !got_beer and beer_node.to_s.match(/\d+ IBU/)
      got_beer = true
      # beer_name = nil
      # beer_abv = nil
      # beer_ibu = nil

      data = beer_node.css('strong')
      beer_name = data.children.first.to_s
      beer_name.strip!
      beer_name.sub! /\s*……….*ABV.*IBU/, ''
      beer_name.sub! /………/, ''
      beer_abv = data.children.last.to_s[/\d+\.\d+% ABV/]
      beer_abv.sub! /% ABV/, ''
      beer_ibu = data.children.last.to_s[/\d+ IBU/]
      beer_ibu.sub! /\sIBU/, ''
    end

  end
  gimme_what_you_got
end

#send_response(tap, datum, response) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lita/handlers/onewheel_beer_pints.rb', line 59

def send_response(tap, datum, response)
  reply = "Pints's tap #{tap}) "
  reply += "#{datum[:name]} - "
  reply += datum[:abv].to_s + '% ABV '
  reply += datum[:ibu].to_s + ' IBU '
  reply += "- #{datum[:desc]}"

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

  response.reply reply
end

#taps_list(response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lita/handlers/onewheel_beer_pints.rb', line 43

def taps_list(response)
  beers = self.get_source
  reply = 'Pints taps: '
  beers.each do |tap, datum|
    reply += "#{tap}) "
    reply += datum[:name] + ' '
    reply += '- ' + datum[:abv].to_s + '% '
    reply += ' '
    # reply += datum[:ibu].to_s + ' IBU '
  end
  reply = reply.strip.sub /,\s*$/, ''

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