beeradvocate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f1c6d4b4772fb31c2f0de4c74a4401e08359ac4
4
+ data.tar.gz: b9d5050a81d57a678f82b22e29c6b3d383d11599
5
+ SHA512:
6
+ metadata.gz: 7ab3ab5217a00220baf56c76bfd57386089fabd52c9cf285f9e19f97457f658548c3bd9818a7b1075ecc66637f47d6bb360671e5391be176d98dc22203139e9c
7
+ data.tar.gz: e960542038a1b7efeb1c1f3dfc1c90e33bec89ad8e2e915bed65356322a889748905ab1b5f1941076ff3acf5634c498f3d27023bd31876c3db2ca1579acf752c
data/bin/beer ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ require 'beer_advocate'
3
+ require 'json'
4
+
5
+ if ARGV.count == 0
6
+ STDERR.puts "Usage: #{$0} name of beer"
7
+ exit 1
8
+ end
9
+
10
+ beer_name = ARGV.join(' ')
11
+
12
+ url = BeerAdvocate.get_url(beer_name)
13
+
14
+ beer_details = BeerAdvocate.get_beer_details_from_url(url)
15
+
16
+ puts JSON.pretty_generate(beer_details)
17
+
18
+
@@ -0,0 +1,5 @@
1
+ module BeerAdvocate
2
+ VERSION = '0.0.1'
3
+ VERSION_DATE = '2015-07-01'
4
+ end
5
+
@@ -0,0 +1,67 @@
1
+ require 'nokogiri'
2
+ require 'mechanize'
3
+ require 'open-uri'
4
+
5
+ require 'beer_advocate/version'
6
+
7
+ ## A handful of utilities for making beer signs based on Beer Advocate data.
8
+ module BeerAdvocate
9
+ class << self
10
+ ## Given a beer name, return the URL for the top Google search result for
11
+ ## `site:beeradvocate.com beer name`.
12
+ def get_url(beer_name)
13
+ agent = Mechanize.new
14
+ page = agent.get('http://www.google.com')
15
+ form = page.form('f')
16
+ form.q = "site:beeradvocate.com #{beer_name}"
17
+
18
+ results = agent.submit(form, form.buttons.first)
19
+
20
+ urls = []
21
+ results.links.each do |link|
22
+ if link.href.to_s =~/url.q/
23
+ str=link.href.to_s
24
+ strList=str.split(%r{=|&})
25
+ urls << strList[1]
26
+ end
27
+ end
28
+ urls.select {|url| url.match(/beeradvocate.com/)}.first
29
+ end
30
+
31
+
32
+ ## Given a URL for a Beer Advocate beer page, return a hash containing
33
+ ## info about the beer, including `name`, `brewery`, `style`, `abv`,
34
+ ## and `score`.
35
+ def get_beer_details_from_url(url)
36
+ page = Nokogiri::HTML(open(url))
37
+
38
+ table = page.css('#ba-content table')[1]
39
+
40
+ beer_name = table.css('img')[0].attributes['alt'].value
41
+
42
+ beer_score = table.css('.ba-score').text
43
+
44
+ beer_style = table.css('a').
45
+ select {|tag| tag.attributes['href'].value.match(%r{^/beer/style/\d+/?$}) }.
46
+ map {|tag| tag.text}.
47
+ first
48
+
49
+ beer_brewery = table.css('a').
50
+ select {|tag| tag.attributes['href'].value.match(%r{^/beer/profile/\d+/?$}) }.
51
+ map {|tag| tag.text}.
52
+ first
53
+
54
+ m = table.to_s.match(/ \| [^\d]{0,9} (\d+ \. \d+ \%) /x)
55
+ beer_abv = m[1]
56
+
57
+ {
58
+ name: beer_name,
59
+ score: beer_score,
60
+ style: beer_style,
61
+ abv: beer_abv,
62
+ brewery: beer_brewery
63
+ }
64
+ end
65
+ end
66
+ end
67
+
@@ -0,0 +1,2 @@
1
+ require 'beer_advocate'
2
+
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beeradvocate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pete Gamache
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mechanize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.7'
69
+ description: |2
70
+ The BeerAdvocate gem contains several brittle, lightly-tested methods
71
+ for turning a beer name into its Beer Advocate URL (by scraping Google),
72
+ and turning a Beer Advocate beer page URL into a hash of attributes
73
+ about the beer (by scraping Beer Advocate). The gem also installs a
74
+ `beer` executable, which returns JSON-formatted beer information for
75
+ the beer named on the command line.
76
+ email: pete@gamache.org
77
+ executables:
78
+ - beer
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - bin/beer
83
+ - lib/beer_advocate.rb
84
+ - lib/beer_advocate/version.rb
85
+ - lib/beeradvocate.rb
86
+ homepage: https://github.com/gamache/beeradvocate
87
+ licenses:
88
+ - public domain
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 1.8.7
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Fragile, minimal utilities to scrape Beer Advocate
110
+ test_files: []