punky_brewster 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ccaa60e6fd75389c787538911dc816419c55e2c
4
- data.tar.gz: 1dd6f6f5e4a3bb929741b83013416adec9a84642
3
+ metadata.gz: 10745cdefe9b6de234e68aa53135e5bba7d89c41
4
+ data.tar.gz: 5c517479b5e736ac8c6d0f687be46e62fd2e89a9
5
5
  SHA512:
6
- metadata.gz: 19b35e7f364c63046a5d71e06e97c099e818a56e65892d3b1b83f684a6a2a0d1bead5ef154f830e4cc276a8b79bc017a622ae76ba7d38c36bf9f25db33e7f34d
7
- data.tar.gz: 356ef32df80c134cf234f59340b4c268692b52e75c418ab8134b919092b1d7d21564bce6b45d1abc73a103ea1a32f42f3a14088446129bae7d6518bc6e0b406d
6
+ metadata.gz: 9d698a5e0379d6b26d5ab8f726db3d227b24e11bb1eaeecf8fcf75ca99507b104cd719219782e4f46a8bc858926f77e4bc90ef5e6047f95ffd4c3f846b65b12c
7
+ data.tar.gz: ad7846f329c892c5f834e88ed27f258d2ecc0277fbb22d8fc2184c0a12fd3c587bb794fa80d85445cce46168dd905efcbea3a5831bfcda91b2c4338f258f2676
data/README.md CHANGED
@@ -23,7 +23,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
23
23
 
24
24
  ## Contributing
25
25
 
26
- 1. Fork it ( https://github.com/[my-github-username]/punky_brewster/fork )
26
+ 1. Fork it ( https://github.com/Aupajo/punky_brewster/fork )
27
27
  2. Create your feature branch (`git checkout -b my-new-feature`)
28
28
  3. Commit your changes (`git commit -am 'Add some feature'`)
29
29
  4. Push to the branch (`git push origin my-new-feature`)
@@ -0,0 +1,16 @@
1
+ require "net/http"
2
+
3
+ module PunkyBrewster
4
+ class BeerListRequest
5
+ URL = "http://www.punkybrewster.co.nz"
6
+
7
+ def response
8
+ raw_response = Net::HTTP.get_response(URI(URL))
9
+ BeerListResponse.new(raw_response)
10
+ end
11
+
12
+ def beers
13
+ response.beers
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ module PunkyBrewster
2
+ class BeerListResponse
3
+ def initialize(raw_response)
4
+ @raw_response = raw_response
5
+ end
6
+
7
+ def beers
8
+ @beers ||= begin
9
+ content = document.at_css('#content')
10
+ divide = content.at_css('.styled-hr')
11
+ past_divide = false
12
+ beer_list = []
13
+
14
+ content.traverse do |node|
15
+ past_divide = true if node == divide
16
+ next unless past_divide
17
+
18
+ if node.name == 'h2'
19
+ beer_list << Beer.new
20
+ # Includes non-breaking spaces
21
+ beer_list.last.name = node.text.upcase.gsub(/[\s\u00A0]+/, ' ')
22
+ elsif price = node.text.scan(/^\$(\d+\.\d+)\/L$/).flatten.first
23
+ beer_list.last.price = price.to_f
24
+ elsif abv = node.text.scan(/(\d+\.\d+)\s*%/).flatten.first
25
+ beer_list.last.abv = abv.to_f
26
+ end
27
+ end
28
+
29
+ beer_list
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def document
36
+ @document ||= Nokogiri::HTML(@raw_response.body)
37
+ end
38
+ end
39
+ end
@@ -1,6 +1,5 @@
1
1
  require "thor"
2
2
  require "punky_brewster"
3
- require "ostruct"
4
3
  require "nokogiri"
5
4
  require "open-uri"
6
5
 
@@ -15,26 +14,7 @@ module PunkyBrewster
15
14
 
16
15
  desc "list", "Show current beer list"
17
16
  def list
18
- response = open("http://www.punkybrewster.co.nz")
19
- document = Nokogiri::HTML(response)
20
- content = document.at_css('#content')
21
- divide = content.at_css('.styled-hr')
22
- past_divide = false
23
- beers = []
24
-
25
- content.traverse do |node|
26
- past_divide = true if node == divide
27
- next unless past_divide
28
-
29
- if node.name == 'h2'
30
- beers << OpenStruct.new
31
- beers.last.name = node.text.upcase.gsub(/\s+/, ' ')
32
- elsif price = node.text.scan(/^\$(\d+\.\d+)\/L$/).flatten.first
33
- beers.last.price = price.to_f
34
- elsif abv = node.text.scan(/(\d+\.\d+)\s*%/).flatten.first
35
- beers.last.abv = abv.to_f
36
- end
37
- end
17
+ beers = BeerListRequest.new.beers
38
18
 
39
19
  rows = beers.sort_by(&:name).map do |beer|
40
20
  [beer.name, "$%0.2f/L" % beer.price, "%0.1f%" % beer.abv]
@@ -1,3 +1,3 @@
1
1
  module PunkyBrewster
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,5 +1,7 @@
1
1
  require "punky_brewster/version"
2
+ require "punky_brewster/beer_list_request"
3
+ require "punky_brewster/beer_list_response"
2
4
 
3
5
  module PunkyBrewster
4
- # Your code goes here...
6
+ Beer = Struct.new(:name, :price, :abv)
5
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: punky_brewster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Nicholls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-18 00:00:00.000000000 Z
11
+ date: 2015-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -127,6 +127,8 @@ files:
127
127
  - bin/setup
128
128
  - exe/punky_brewster
129
129
  - lib/punky_brewster.rb
130
+ - lib/punky_brewster/beer_list_request.rb
131
+ - lib/punky_brewster/beer_list_response.rb
130
132
  - lib/punky_brewster/cli.rb
131
133
  - lib/punky_brewster/version.rb
132
134
  - punky_brewster.gemspec