frothybeer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.2-p290@frothybeer"
8
+
9
+ #
10
+ # Uncomment following line if you want options to be set only for given project.
11
+ #
12
+ # PROJECT_JRUBY_OPTS=( --1.9 )
13
+
14
+ #
15
+ # First we attempt to load the desired environment directly from the environment
16
+ # file. This is very fast and efficient compared to running through the entire
17
+ # CLI and selector. If you want feedback on which environment was used then
18
+ # insert the word 'use' after --create as this triggers verbose mode.
19
+ #
20
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
21
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
22
+ then
23
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
24
+
25
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
26
+ then
27
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
28
+ fi
29
+ else
30
+ # If the environment file has not yet been created, use the RVM CLI to select.
31
+ if ! rvm --create "$environment_id"
32
+ then
33
+ echo "Failed to create RVM environment '${environment_id}'."
34
+ exit 1
35
+ fi
36
+ fi
37
+
38
+ #
39
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
40
+ # it be automatically loaded. Uncomment the following and adjust the filename if
41
+ # necessary.
42
+ #
43
+ # filename=".gems"
44
+ # if [[ -s "$filename" ]]
45
+ # then
46
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
47
+ # fi
48
+
49
+ # If you use bundler, this might be useful to you:
50
+ # if command -v bundle && [[ -s Gemfile ]]
51
+ # then
52
+ # bundle
53
+ # fi
54
+
55
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in frothybeer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Vidur Murali
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Frothybeer
2
+
3
+ Gets ratings, styles, abv content, breweries, and other information from a beer's name
4
+
5
+ Very simple and crude at the moment. Needs an exact match to the name to find the beer. However, it does return an array of other matches as a result. (The first element is the exact match, or 'nil' if an exact match was not found)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'frothybeer'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install frothybeer
20
+
21
+ ## Usage
22
+
23
+ test.rb:
24
+
25
+ require './frothybeer/lib/frothybeer'
26
+
27
+ exit if ARGV[0].nil?
28
+
29
+ beers = FrothyBeer::beersearch(ARGV[0])
30
+ if beers.empty?
31
+ puts "No results found for that beer"
32
+ elsif beers.first.nil?
33
+ puts "Did not find #{ARGV[0]}, but we did find these similarly named beers:"
34
+ beers.shift
35
+ beers.each do |beer|
36
+ puts "#{beer.name} with a rating of #{beer.rating}" if !beer.rating.nil?
37
+ end
38
+ else
39
+ beer = beers.first
40
+ puts "#{beer.name} has a rating of #{beer.rating}"
41
+ end
42
+
43
+ Sample output:
44
+
45
+ $ ruby test.rb "Sierra Nevada Pale Ale"
46
+ Sierra Nevada Pale Ale has a rating of 91/100
47
+ $ ruby test.rb "Anchor Steam"
48
+ Did not find Anchor Steam, but we did find these similarly named beers:
49
+ Anchor Steam Beer with a rating of 87/100
50
+ $ ruby test.rb "asdgaga"
51
+ No results found for that beer
52
+ $ ruby test.rb "Belgian Ale"
53
+ Did not find Belgian Ale, but we did find these similarly named beers:
54
+ Borman's Belgian Ale with a rating of 78/100
55
+ Friar's Belgian-Style White Ale with a rating of 82/100
56
+ W'09 Belgian Style Ale with a rating of 82/100
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/frothybeer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Vidur Murali"]
6
+ gem.email = ["vidur.murali@gmail.com"]
7
+ gem.description = %q{Finds beers and their attributes from BeerAdvocate}
8
+ gem.summary = %q{Gets ratings, styles, abv content, breweries, and other information from a beer's name}
9
+ gem.homepage = "https://github.com/vyder/frothybeer"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "frothybeer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = FrothyBeer::VERSION
17
+
18
+ gem.add_runtime_dependency "bundler"
19
+ gem.add_runtime_dependency "hpricot"
20
+ end
@@ -0,0 +1,92 @@
1
+ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
2
+
3
+ # rubygems
4
+ require 'rubygems'
5
+
6
+ # 3rd party
7
+ require 'hpricot'
8
+ require 'open-uri'
9
+
10
+ # internal requires
11
+ require 'frothybeer/beer' # yarrr!
12
+ require 'frothybeer/webpage'
13
+ require 'frothybeer/version'
14
+
15
+
16
+
17
+ module FrothyBeer
18
+
19
+ # Main search function used to find the beer
20
+ #
21
+ #
22
+ def self.beersearch(beerToFind)
23
+ return nil if beerToFind.nil? # TODO raise an exception
24
+
25
+ # Clean query string
26
+ query = beerToFind.gsub(" ", "+")
27
+ url = "http://beeradvocate.com/search?q=#{query}&qt=beer"
28
+
29
+ resultsPage = WebPage::getPage(url)
30
+ if WebPage::hasResults?(resultsPage)
31
+ results = WebPage::getResults(resultsPage)
32
+ else
33
+ # TODO raise an exception
34
+ return []
35
+ end
36
+
37
+ # find the best match beers
38
+ beer = nil
39
+ suggestions = []
40
+ results.each do |beer_name, beer_link|
41
+ beer_page = WebPage::getPage(beer_link)
42
+ b = Beer.new(self.makeBeerHash(beer_page))
43
+
44
+ if beer_name == beerToFind # TODO Figure out a better matching algorithm
45
+ beer = b
46
+ else
47
+ suggestions << b
48
+ end
49
+ end
50
+
51
+ return suggestions.unshift(beer)
52
+ end
53
+
54
+ def self.makeBeerHash(page)
55
+ hash = {}
56
+
57
+ hash[:name] = self.scrapeName(page)
58
+ hash[:brewery] = self.scrapeBrewery(page)
59
+ hash[:rating] = self.scrapeRating(page)
60
+ hash[:style] = self.scrapeStyle(page)
61
+ hash[:abv] = self.scrapeABV(page)
62
+
63
+ hash
64
+ end
65
+
66
+ # Methods to scrape the beer profile page.
67
+ def self.scrapeName(page)
68
+ page.search("h1[@class='norm']").html
69
+ end
70
+
71
+ def self.scrapeBrewery(page)
72
+ page.search("a/b")[2].html
73
+ end
74
+
75
+ # Given a beer page, returns rating for the beer
76
+ # (returns nil if rating N/A)
77
+ def self.scrapeRating(page)
78
+ rating = page.search("span[@class='BAscore_big']").first.html
79
+ (rating == "N/A") ? nil : "#{rating}/100"
80
+ end
81
+
82
+ def self.scrapeStyle(page)
83
+ page.search("a/b")[3].html
84
+ end
85
+
86
+ def self.scrapeABV(page)
87
+ raw = page.search("a/b")[3].parent().next().inspect
88
+ return nil if raw.match(/\?/) # There is no abv specified
89
+ abv = raw.match(/([\d\.]*)%/)[0]
90
+ end
91
+
92
+ end
@@ -0,0 +1,22 @@
1
+ module FrothyBeer
2
+
3
+ class Beer
4
+ attr_reader :name, :brewery, :rating, :style, :abv
5
+
6
+ # Beer hash params:
7
+ # :name
8
+ # :brewery
9
+ # :rating
10
+ # :style
11
+ # :abv (alcohol by volume)
12
+ def initialize(hash)
13
+ @name = hash[:name]
14
+ @brewery = hash[:brewery]
15
+ @rating = hash[:rating]
16
+ @style = hash[:style]
17
+ @abv = hash[:abv]
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,3 @@
1
+ module FrothyBeer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ module FrothyBeer
2
+
3
+ module WebPage
4
+
5
+ # Creates a new webpage from a url
6
+ def self.getPage(url)
7
+ open(url) {|f| Hpricot(f)}
8
+ end
9
+
10
+ def self.hasResults?(page)
11
+ # are there elements with 'no results' as the text
12
+ page.search("li[text()*='No results']").empty?
13
+ end
14
+
15
+ def self.getResults(page)
16
+ return {} if !self.hasResults?(page)
17
+
18
+ resultHash = {}
19
+ list_elements = page.search("li/a")
20
+ list_elements.each do |li|
21
+ # Beer names are in bold. Alternate links are breweries
22
+ if !li.at("b").nil?
23
+ resultHash[li.at("b").html] = "http://beeradvocate.com#{li.get_attribute("href")}"
24
+ end
25
+ end
26
+
27
+ resultHash
28
+ end
29
+
30
+ end # End of WebPage module
31
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: frothybeer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vidur Murali
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70129987888340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70129987888340
25
+ - !ruby/object:Gem::Dependency
26
+ name: hpricot
27
+ requirement: &70129987887920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70129987887920
36
+ description: Finds beers and their attributes from BeerAdvocate
37
+ email:
38
+ - vidur.murali@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rvmrc
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - frothybeer.gemspec
50
+ - lib/frothybeer.rb
51
+ - lib/frothybeer/beer.rb
52
+ - lib/frothybeer/version.rb
53
+ - lib/frothybeer/webpage.rb
54
+ homepage: https://github.com/vyder/frothybeer
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.10
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Gets ratings, styles, abv content, breweries, and other information from
78
+ a beer's name
79
+ test_files: []