Class: RateBeer::Style

Inherits:
Object
  • Object
show all
Extended by:
URLs
Includes:
Scraping, URLs
Defined in:
lib/ratebeer/style.rb

Constant Summary

Constants included from URLs

URLs::BASE_URL, URLs::SEARCH_URL

Instance Attribute Summary collapse

Attributes included from Scraping

#id

Class Method Summary collapse

Methods included from URLs

beer_url, brewery_beers_url, brewery_url, country_url, region_url, review_url, style_beers_url, style_url

Methods included from Scraping

#==, #fix_characters, #full_details, #id_from_link, included, #initialize, #inspect, nbsp, noko_doc, #page_count, #pagination?, #post_request, #symbolize_text, #to_s, #url

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



20
21
22
# File 'lib/ratebeer/style.rb', line 20

def category
  @category
end

Class Method Details

.all_styles(include_hidden = false) ⇒ Array<RateBeer::Style>

Scrape all styles.

RateBeer provides a styles landing page, with links through to info on each style listed thereon. This method scrapes style info with links to the more detailed pages.

Parameters:

  • hidden_styles (Boolean)

    Flag for whether to include hidden styles.

Returns:

  • (Array<RateBeer::Style>)

    List of styles with links etc. to detailed pages



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ratebeer/style.rb', line 36

def all_styles(include_hidden = false)
  doc  = Scraping.noko_doc(URI.join(BASE_URL, '/beerstyles/'))
  root = doc.at_css('div.container-fluid')

  categories = root.css('h3').map(&:text)
  style_node = root.css('.styleGroup')

  styles = style_node.flat_map.with_index do |list, i|
    list.css('a').map do |x|
      category = categories[i]
      Style.new(x['href'].split('/').last.to_i, name: x.text).tap do |s|
        s.category = category
      end
    end
  end
  if include_hidden
    styles + hidden_styles
  else
    styles
  end
end

.data_keysObject

Each key represents an item of data accessible for each beer, and defines dynamically a series of methods for accessing this data.



10
11
12
13
14
15
# File 'lib/ratebeer/style.rb', line 10

def self.data_keys
  [:name,
   :description,
   :glassware,
   :beers]
end

.hidden_stylesArray<Hash>

Scrape hidden style information

RateBeer has a number of styles not accessible from the “beerstyles” landing page. This method scrapes these.

Returns:

  • (Array<Hash>)

    List of hidden styles



65
66
67
68
69
70
71
# File 'lib/ratebeer/style.rb', line 65

def hidden_styles
  hidden_ids = [40, 41, 57, 59, 66, 67, 68, 69, 70,
                75, 83, 99, 104, 106, 116, 119, 120]
  hidden_ids.map do |id|
    Style.new(id)
  end
end