Class: RateBeer::Search

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

Overview

This class provides functionality for searching RateBeer.com for a specific beer or brewery.

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

Instance 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, nbsp, noko_doc, #page_count, #pagination?, #post_request, #symbolize_text, #to_s, #url

Constructor Details

#initialize(query, scrape_beer_brewers = false) ⇒ Search

Create a RateBeer::Search instance.

Parameters:

  • query (String)

    Term to use to search RateBeer



43
44
45
46
# File 'lib/ratebeer/search.rb', line 43

def initialize(query, scrape_beer_brewers = false)
  self.query = query
  @scrape_breweries = scrape_beer_brewers
end

Instance Attribute Details

#queryObject

Returns the value of attribute query.



37
38
39
# File 'lib/ratebeer/search.rb', line 37

def query
  @query
end

Class Method Details

.data_keysObject

Keys for fields scraped on RateBeer



17
18
19
20
21
# File 'lib/ratebeer/search.rb', line 17

def self.data_keys
  [:query,
   :beers,
   :breweries]
end

.search(query) ⇒ Object

Create method which generates new search instance and immediately runs a search.



30
31
32
33
34
# File 'lib/ratebeer/search.rb', line 30

def search(query)
  s = new(query)
  { beers:      s.beers,
    breweries:  s.breweries }
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
# File 'lib/ratebeer/search.rb', line 55

def ==(other)
  query == other.query
end

#inspectObject



59
60
61
62
63
64
65
# File 'lib/ratebeer/search.rb', line 59

def inspect
  num_beers = @beers && @beers.count || 0
  num_breweries = @breweries && @breweries.count || 0
  val = "#<#{self.class} - #{@query}"
  val << " - #{num_beers} beers / #{num_breweries} breweries" if @beers || @breweries
  val << ">"
end

#run_searchHash Also known as: retrieve_details

Search RateBeer for beers, brewers, etc.

The search results page contains a series of tables each of which has the “results” class, containing data of matching brewers, beers, and places in that order. Only brewers and beers are extracted.

Returns:

  • (Hash)

    Results of the search, broken into breweries and beers, with the attributes of these results contained therein.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ratebeer/search.rb', line 76

def run_search
  @beers, @breweries = nil
  tables             = doc.css('h2').map(&:text).zip(doc.css('table'))
  beers, breweries   = nil
  tables.each do |(heading, table)|
    case heading
    when 'brewers'
      @breweries = process_breweries_table(table)
    when 'beers'
      @beers = process_beers_table(table)
    end
  end

  # RateBeer is inconsistent with searching for IPAs. If IPA is in the name
  # of the beer, replace IPA with India Pale Ale, and add the additional
  # results to these results.
  if query.downcase.include?(' ipa')
    alt_query = query.downcase.gsub(' ipa', ' india pale ale')
    extra_beers = self.class.new(alt_query).run_search.beers
    @beers = ((@beers || []) + (extra_beers || [])).uniq
  end
  self
end