Class: RateBeer::Search
- Inherits:
-
Object
- Object
- RateBeer::Search
- 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
-
#query ⇒ Object
Returns the value of attribute query.
Attributes included from Scraping
Class Method Summary collapse
-
.data_keys ⇒ Object
Keys for fields scraped on RateBeer.
-
.search(query) ⇒ Object
Create method which generates new search instance and immediately runs a search.
Instance Method Summary collapse
-
#initialize(query) ⇒ Search
constructor
Create a RateBeer::Search instance.
- #inspect ⇒ Object
-
#run_search ⇒ Hash
(also: #retrieve_details)
Search RateBeer for beers, brewers, etc.
Methods included from URLs
#beer_url, #brewery_url, #country_url, #region_url, #review_url, #style_beers_url, #style_url
Methods included from Scraping
#==, #fix_characters, #full_details, included, nbsp, noko_doc, #page_count, #pagination?, #post_request, #symbolize_text, #to_s, #url
Constructor Details
#initialize(query) ⇒ Search
Create a RateBeer::Search instance.
43 44 45 |
# File 'lib/ratebeer/search.rb', line 43 def initialize(query) self.query = query end |
Instance Attribute Details
#query ⇒ Object
Returns the value of attribute query.
37 38 39 |
# File 'lib/ratebeer/search.rb', line 37 def query @query end |
Class Method Details
.data_keys ⇒ Object
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 = self.new(query) { beers: s.beers, breweries: s.breweries } end |
Instance Method Details
#inspect ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/ratebeer/search.rb', line 54 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_search ⇒ Hash 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ratebeer/search.rb', line 71 def run_search @beers, @breweries = nil doc = post_request(URI.join(BASE_URL, SEARCH_URL), post_params) 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 return self end |