Class: Lita::Handlers::OnewheelBeerLoyalLegion

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

Instance Method Summary collapse

Instance Method Details

#get_sourceObject



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

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://loyallegionpdx.com/taplist')
    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.



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
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lita/handlers/onewheel_beer_loyal_legion.rb', line 73

def parse_response(response)
  Lita.logger.debug 'parse_response started.'
  gimme_what_you_got = {}
  noko = Nokogiri.HTML response

  noko.css('article.tap-item').each do |beer_node|
    # gimme_what_you_got
    tap_name = beer_node.css('small.tapnum').children.to_s.sub 'Tap#', ''
    next if tap_name.empty?

    beer_name = beer_node.css('div.tap-content h1').children.to_s.strip

    greyout = beer_node.css('small.grayout').children.to_s
    greyedouts = greyout.split /\|/
    beer_type = greyedouts[0].strip
    abv = greyedouts[1].match(/\d+\.*\d*/).to_s
    if greyedouts[2]
      ibu = greyedouts[2].match(/\d+/).to_s
    end

    price = beer_node.css('div.beer-price').children.first.to_s.strip.sub '$', ''

    full_text_search = "#{beer_name} #{beer_type}"

    gimme_what_you_got[tap_name] = {
    #     type: tap_type,
    #     remaining: remaining,
    #     brewery: brewery.to_s,
        name: beer_name.to_s,
        desc: beer_type.to_s,
        abv: abv.to_f,
        ibu: ibu.to_f,
        price: price,
        search: full_text_search
    }
  end
  # puts gimme_what_you_got.inspect

  gimme_what_you_got
end

#send_response(tap, datum, response) ⇒ Object



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

def send_response(tap, datum, response)
  reply = "Loyal Legion 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 += "#{datum[:remaining]}"
  reply += "#{datum[:abv]}%, "
  reply += "#{datum[:ibu]} IBU, "
  reply += "$#{datum[:price].to_s}"

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

  response.reply reply
end