Sha256: 5c43ca909f68553f9d08e2f912c4314a7639852d59340b22637652b20babc9ec

Contents?: true

Size: 1003 Bytes

Versions: 2

Compression:

Stored size: 1003 Bytes

Contents

module PunkyBrewster
  class BeerListResponse
    def initialize(raw_response)
      @raw_response = raw_response
    end

    def beers
      @beers ||= begin
        content = document.at_css('#content')
        divide = content.at_css('.styled-hr')
        past_divide = false
        beer_list = []

        content.traverse do |node|
          past_divide = true if node == divide
          next unless past_divide

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

        beer_list
      end
    end

    private

    def document
      @document ||= Nokogiri::HTML(@raw_response.body)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
punky_brewster-1.1.0 lib/punky_brewster/beer_list_response.rb
punky_brewster-1.0.1 lib/punky_brewster/beer_list_response.rb