Class: PunkyBrewster::BeerListResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/punky_brewster/beer_list_response.rb

Constant Summary collapse

IMAGE_URL_BASE =
'http://www.punkybrewster.co.nz'

Instance Method Summary collapse

Constructor Details

#initialize(raw_response) ⇒ BeerListResponse

Returns a new instance of BeerListResponse.



7
8
9
# File 'lib/punky_brewster/beer_list_response.rb', line 7

def initialize(raw_response)
  @raw_response = raw_response
end

Instance Method Details

#beersObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/punky_brewster/beer_list_response.rb', line 11

def beers
  @beers ||= begin
    content = document.at_css('#content')
    list = []

    content.traverse do |node|
      if node.name == 'h2'
        list << Beer.new
        # Includes non-breaking spaces
        list.last.name = node.text.upcase.gsub(/[\s\u00A0]+/, ' ')
      elsif node.name == 'img'
        list.last.image_url = IMAGE_URL_BASE + node[:src]
      elsif price = node.text.scan(/^\$(\d+\.\d+)\/L$/).flatten.first
        list.last.price = price.to_f
      elsif abv = node.text.scan(/(\d+\.\d+)\s*%/).flatten.first
        list.last.abv = abv.to_f
      end
    end

    BeerList.new(list.select(&:valid?))
  end
end