beer_recipe 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 041d468f9dd867b90a3cb4578ea1a585e033d904
4
- data.tar.gz: afc83564964bbe91301631ce585b41b202b565f0
3
+ metadata.gz: 59854fd37ab30f4018a7971da8615e820886e166
4
+ data.tar.gz: a023d8a675a187fcc9acd681e680f0c0e6b11485
5
5
  SHA512:
6
- metadata.gz: eeb35af41305b7daa1c1916e0478ebf8e1f9d8c9c3ad462e67a6b9688b623e5b50806f2d253f688ee23ea8c8980129d0aa03356726bdf2b90247392725ab1fb0
7
- data.tar.gz: 8b3a4f55a1bef64ea1b8a02341605f7fce27825bd07ef541a30657f487d12160c87e32f750ec47e48db09c9c13aa677dcad1dbabfeca45b5b92546f4f58ad8cb
6
+ metadata.gz: d7c4041ab2c42bb4d54a99efca92d62496ce8a5f55741772f49b87e25c77fa2348c775445b1002f1ce7cba751960f85fee4a36a767e5a9e86944552e5e7c2e16
7
+ data.tar.gz: 19112e1ac28a6043fdbb0198d672f6df39161b3504dc9ffd9f0c6f4eb7b58cfce04c64dc257c245e6e4013f7f5f1a7cd7acb0a788e784f6e5ff9557b588fd0e4
data/bin/beer_recipe CHANGED
@@ -9,25 +9,23 @@ doc = <<DOCOPT
9
9
  Beer Recipe
10
10
 
11
11
  Usage:
12
- #{__FILE__} <beer.xml> [--format=<format>]
12
+ #{__FILE__} [--file <beer.xml>] [--format=<format>] [--template=<path>]
13
13
  #{__FILE__} -h | --help
14
14
  #{__FILE__} --version
15
15
 
16
16
  Options:
17
17
  -h --help Show this screen.
18
18
  --version Show version.
19
- --format=<format> Output format [default: html].
19
+ --format=<format> Output format: text|html [default: html].
20
+ --template=<path> Path to ERB html template file to use instead of default.
21
+ --file=<beerxml> Path to beerxml file to read (default STDIN).
20
22
 
21
23
  DOCOPT
22
24
 
23
25
  begin
24
- opts = Docopt::docopt(doc, version: "BeerRecipe v#{BeerRecipe::VERSION}")
25
- formatter = if opts['--format'].downcase.start_with? 't'
26
- BeerRecipe::TextFormatter.new
27
- else
28
- BeerRecipe::HtmlFormatter.new
29
- end
30
- BeerRecipe::Reader.new(file: ARGV[0], formatter: formatter).read.output
26
+ doc_opts = Docopt::docopt(doc, version: "BeerRecipe v#{BeerRecipe::VERSION}")
27
+ options = BeerRecipe::Reader.parse_options(doc_opts)
28
+ BeerRecipe::Reader.new(options).read.parse
31
29
  rescue Docopt::Exit => e
32
30
  puts e.message
33
31
  end
data/lib/beer_recipe.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  require 'nrb/beerxml'
2
2
  require 'erb'
3
+ require 'pathname'
3
4
 
4
5
  module BeerRecipe
5
6
  end
6
7
 
8
+ require 'beer_recipe/version'
9
+ require 'beer_recipe/errors'
7
10
  require 'beer_recipe/reader'
8
11
  require 'beer_recipe/wrapper'
9
12
  require 'beer_recipe/mash_wrapper'
@@ -0,0 +1,4 @@
1
+ module BeerRecipe
2
+ class BeerRecipeError < StandardError; end
3
+ class NotImplentedError < BeerRecipeError; end
4
+ end
@@ -1,6 +1,28 @@
1
1
  class BeerRecipe::FermentableWrapper < BeerRecipe::Wrapper
2
2
  def formatted_amount
3
- "#{'%.2f' % @record.amount} kg"
3
+ "#{'%.2f' % amount} kg"
4
+ end
5
+
6
+ #@type="Grain", @yield=82.5, @color=5.91,
7
+ def formatted_color
8
+ "#{'%.0f' % color} EBC"
9
+ end
10
+
11
+ def color_ebc
12
+ color
13
+ end
14
+
15
+ def color_srm
16
+ color / 1.97
17
+ end
18
+
19
+ def color_class
20
+ "srm#{'%.0f' % color_srm}"
21
+ end
22
+
23
+ def mcu
24
+ # MCU = (weight kg * lovibond * 2.205) / (volume * 0.264)
25
+ (amount * color_srm * 2.205) / (@recipe.batch_size * 0.264)
4
26
  end
5
27
  end
6
28
 
@@ -3,8 +3,8 @@ class BeerRecipe::HtmlFormatter < BeerRecipe::RecipeFormatter
3
3
  puts parse.read
4
4
  end
5
5
 
6
- def template_path
7
- 'template/html.erb'
6
+ def template_file
7
+ @options.fetch(:template, template_path('html.erb'))
8
8
  end
9
9
 
10
10
  end
@@ -3,16 +3,33 @@ class BeerRecipe::Reader
3
3
 
4
4
  def initialize(options = {})
5
5
  @options = options
6
+ @options[:recipe_wrapper] ||= BeerRecipe::RecipeWrapper
7
+ @options[:formatter] ||= BeerRecipe::HtmlFormatter
8
+ end
9
+
10
+ def self.parse_options(options)
11
+ opts = {}
12
+ opts[:formatter] = if options.fetch('--format', 'html').downcase.start_with? 't'
13
+ BeerRecipe::TextFormatter
14
+ else
15
+ BeerRecipe::HtmlFormatter
16
+ end
17
+ opts[:template] = options['--template'] if options['--template']
18
+ opts[:file] = options['--file'] if options['--file']
19
+ opts
6
20
  end
7
21
 
8
22
  def read
9
23
  @parser ||= NRB::BeerXML::Parser.new
10
- @beerxml = parser.parse @options[:file]
24
+ @beerxml = parser.parse @options.fetch(:file, STDIN)
11
25
  self
12
26
  end
13
27
 
14
- def output
15
- @options[:formatter].format(BeerRecipe::RecipeWrapper.new(@beerxml.records.first)).output
28
+ def parse
29
+ @beerxml.records.each do |record|
30
+ recipe = @options[:recipe_wrapper].new(record)
31
+ @options[:formatter].new(@options).format(recipe).output
32
+ end
16
33
  end
17
34
  end
18
35
 
@@ -1,15 +1,23 @@
1
1
  class BeerRecipe::RecipeFormatter
2
+ def initialize(options = {})
3
+ @options = options
4
+ end
5
+
2
6
  def format(recipe)
3
7
  @recipe = recipe
4
8
  self
5
9
  end
6
10
 
7
- def template_path
8
- ''
11
+ def template_path(file)
12
+ Pathname.new(File.join(File.dirname(__FILE__), '..', '..', 'template', file)).realpath
13
+ end
14
+
15
+ def template_file
16
+ raise BeerRecipe::NotImplementedError.new
9
17
  end
10
18
 
11
19
  def template
12
- IO.read(template_path)
20
+ IO.read(template_file)
13
21
  end
14
22
 
15
23
  def parse
@@ -30,6 +30,10 @@ class BeerRecipe::RecipeWrapper < BeerRecipe::Wrapper
30
30
  @mash ||= BeerRecipe::MashWrapper.new(recipe.mash, self)
31
31
  end
32
32
 
33
+ def style_code
34
+ "#{recipe.style.category_number} #{recipe.style.style_letter}"
35
+ end
36
+
33
37
  def abv
34
38
  return @abv if @abv
35
39
  og = recipe.og
@@ -50,5 +54,60 @@ class BeerRecipe::RecipeWrapper < BeerRecipe::Wrapper
50
54
  @ibu
51
55
  end
52
56
 
57
+ def grains
58
+ fermentables.select { |f| f.type == 'Grain' }
59
+ end
60
+
61
+ def color
62
+ color_srm
63
+ end
64
+
65
+ def color_mcu
66
+ mcu = 0
67
+ fermentables.each do |f|
68
+ mcu += f.mcu
69
+ end
70
+ mcu
71
+ end
72
+
73
+ def color_srm
74
+ # SRM color = 1.4922 * (MCU ** 0.6859)
75
+ mcu = color_mcu
76
+ srm = 1.4922 * (mcu ** 0.6859)
77
+ if srm > 8
78
+ srm
79
+ else
80
+ mcu
81
+ end
82
+ end
83
+
84
+ def color_ebc
85
+ color_srm * 1.97
86
+ end
87
+
88
+ def color_class
89
+ "srm#{'%.0f' % color}"
90
+ end
91
+
92
+ def formatted_color
93
+ "#{'%.0f' % color} °L"
94
+ end
95
+
96
+ def total_grains
97
+ total_grains = 0
98
+ fermentables.each do |f|
99
+ total_grains += f.amount
100
+ end
101
+ total_grains
102
+ end
103
+
104
+ def total_hops
105
+ hop_weight = 0
106
+ hops.each do |hop|
107
+ hop_weight += hop.amount
108
+ end
109
+ hop_weight
110
+ end
111
+
53
112
  end
54
113
 
@@ -1,3 +1,3 @@
1
1
  module BeerRecipe
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
data/template/html.erb ADDED
@@ -0,0 +1,201 @@
1
+ <html>
2
+ <head>
3
+ <title><%= name %></title>
4
+ <style>
5
+ .colorbox {
6
+ width: 1em;
7
+ height: 1em;
8
+ -webkit-border-radius: 3px;
9
+ -moz-border-radius: 3px;
10
+ border-radius: 3px;
11
+ border-width: 1px;
12
+ border-color: beige;
13
+ border-style: solid;
14
+ display: inline-block;
15
+ }
16
+ .srm1 { background-color: #FFE699; }
17
+ .srm2 { background-color: #FFD878; }
18
+ .srm3 { background-color: #FFCA5A; }
19
+ .srm4 { background-color: #FFBF42; }
20
+ .srm5 { background-color: #FBB123; }
21
+ .srm6 { background-color: #F8A600; }
22
+ .srm7 { background-color: #F39C00; }
23
+ .srm8 { background-color: #EA8F00; }
24
+ .srm9 { background-color: #E58500; }
25
+ .srm10 { background-color: #DE7C00; }
26
+ .srm11 { background-color: #D77200; }
27
+ .srm12 { background-color: #CF6900; }
28
+ .srm13 { background-color: #CB6200; }
29
+ .srm14 { background-color: #C35900; }
30
+ .srm15 { background-color: #BB5100; }
31
+ .srm16 { background-color: #B54C00; }
32
+ .srm17 { background-color: #B04500; }
33
+ .srm18 { background-color: #A63E00; }
34
+ .srm19 { background-color: #A13700; }
35
+ .srm20 { background-color: #9B3200; }
36
+ .srm21 { background-color: #952D00; }
37
+ .srm22 { background-color: #8E2900; }
38
+ .srm23 { background-color: #882300; }
39
+ .srm24 { background-color: #821E00; }
40
+ .srm25 { background-color: #7B1A00; }
41
+ .srm26 { background-color: #771900; }
42
+ .srm27 { background-color: #701400; }
43
+ .srm28 { background-color: #6A0E00; }
44
+ .srm29 { background-color: #660D00; }
45
+ .srm30 { background-color: #5E0B00; }
46
+ .srm31 { background-color: #5A0A02; }
47
+ .srm32 { background-color: #600903; }
48
+ .srm33 { background-color: #520907; }
49
+ .srm34 { background-color: #4C0505; }
50
+ .srm35 { background-color: #470606; }
51
+ .srm36 { background-color: #440607; }
52
+ .srm37 { background-color: #3F0708; }
53
+ .srm38 { background-color: #3B0607; }
54
+ .srm39 { background-color: #3A070B; }
55
+ .srm40 { background-color: #36080A; }
56
+ </style>
57
+ </head>
58
+ <body>
59
+ <article class="beer_recipe">
60
+ <h1><%= name %></h1>
61
+ <aside>
62
+ <h2>Information</h2>
63
+
64
+ <h3>Style</h3>
65
+ <p><%= style.name %> (<%= style_code %> / <%= style.style_guide %>)</p>
66
+
67
+ <h3>Type</h3>
68
+ <p><%= style.type %></p>
69
+
70
+ <h3>Grains</h3>
71
+ <p><%= '%.2f' % total_grains %> kg</p>
72
+
73
+ <h3>Hops</h3>
74
+ <p><%= '%.0f' % total_hops %> g</p>
75
+
76
+ <h3>Equipment</h3>
77
+ <p><%= equipment.name %> (<%= equipment.tun_volume %>L)</p>
78
+
79
+ <h3>Notes</h3>
80
+ <p><%= notes %></p>
81
+
82
+ </aside>
83
+
84
+ <section class="recipe_details">
85
+ <h2>Recipe Details</h2>
86
+ <table>
87
+ <thead>
88
+ <tr>
89
+ <th>Batch Size</th>
90
+ <th>Boil Time</th>
91
+ <th>IBU</th>
92
+ <th>OG</th>
93
+ <th>FG</th>
94
+ <th>ABV</th>
95
+ <th>Color</th>
96
+ </tr>
97
+ </thead>
98
+ <tbody>
99
+ <tr>
100
+ <td><%= '%.1f' % batch_size %> L</td>
101
+ <td><%= '%.0f' % boil_time %> min</td>
102
+ <td><%= '%.0f' % ibu %></td>
103
+ <td><%= '%.3f' % og %> SG</td>
104
+ <td><%= '%.3f' % fg %> SG</td>
105
+ <td><%= '%.1f' % abv %>%</td>
106
+ <td><%= '%.0f' % color_ebc %> EBC
107
+ <div class="colorbox <%= color_class %>">&nbsp;</span></td>
108
+ </tr>
109
+ </tbody>
110
+ </table>
111
+ </section>
112
+
113
+ <section class="fermentables">
114
+ <h2>Fermentables</h2>
115
+ <table>
116
+ <thead>
117
+ <tr>
118
+ <th>Name</th>
119
+ <th>Amount</th>
120
+ <th>Type</th>
121
+ <th>Color</th>
122
+ </tr>
123
+ </thead>
124
+ <tbody>
125
+ <% fermentables.each do |f| %>
126
+ <tr>
127
+ <td><%= f.name %></td>
128
+ <td><%= f.formatted_amount %></td>
129
+ <td><%= f.type %></td>
130
+ <td><%= '%.0f' % f.color_ebc %> EBC
131
+ <div class="colorbox <%= f.color_class %>">&nbsp;</span></td>
132
+ </tr>
133
+ <% end %>
134
+ </tbody>
135
+ </table>
136
+ </section>
137
+
138
+ <section class="hops">
139
+ <h2>Hops</h2>
140
+ <table>
141
+ <thead>
142
+ <tr>
143
+ <th>Name</th>
144
+ <th>Amount</th>
145
+ <th>Time</th>
146
+ <th>Use</th>
147
+ <th>Form</th>
148
+ <th>Alpha</th>
149
+ <th>IBU</th>
150
+ </tr>
151
+ </thead>
152
+ <tbody>
153
+ <% hops.each do |hop| %>
154
+ <tr>
155
+ <td><%= hop.name %></td>
156
+ <td><%= hop.formatted_amount %></td>
157
+ <td><%= hop.formatted_time %></td>
158
+ <td><%= hop.use %></td>
159
+ <td><%= hop.form %></td>
160
+ <td><%= hop.alpha %>%</td>
161
+ <td><%= '%.1f' % hop.ibu %></td>
162
+ </tr>
163
+ <% end %>
164
+ </tbody>
165
+ </table>
166
+ </section>
167
+
168
+ <section class="yeasts">
169
+ <h2>Yeasts</h2>
170
+ <table>
171
+ <thead>
172
+ <tr>
173
+ <th>Name</th>
174
+ <th>Product ID</th>
175
+ <th>Lab</th>
176
+ <th>Form</th>
177
+ <th>Attenuation</th>
178
+ <th>Temperature</th>
179
+ </tr>
180
+ </thead>
181
+ <tbody>
182
+ <% yeasts.each do |yeast| %>
183
+ <tr>
184
+ <td><%= yeast.name %></td>
185
+ <td><%= yeast.product_id %></td>
186
+ <td><%= yeast.laboratory %></td>
187
+ <td><%= yeast.form %></td>
188
+ <td><%= yeast.formatted_attenuation %></td>
189
+ <td><%= yeast.formatted_temperatures %></td>
190
+ </tr>
191
+ <% end %>
192
+ </tbody>
193
+ </table>
194
+ </section>
195
+ </article>
196
+
197
+ <footer>
198
+ <p><small>Generated by <a href="http://github.com/ollej/beer_recipe">BeerRecipe</a> v<%= BeerRecipe::VERSION %></small></p>
199
+ </footer>
200
+ </body>
201
+ </html>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beer_recipe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olle Johansson
@@ -75,6 +75,7 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - bin/beer_recipe
77
77
  - lib/beer_recipe.rb
78
+ - lib/beer_recipe/errors.rb
78
79
  - lib/beer_recipe/fermentable_wrapper.rb
79
80
  - lib/beer_recipe/hop_wrapper.rb
80
81
  - lib/beer_recipe/html_formatter.rb
@@ -88,6 +89,7 @@ files:
88
89
  - lib/beer_recipe/version.rb
89
90
  - lib/beer_recipe/wrapper.rb
90
91
  - lib/beer_recipe/yeast_wrapper.rb
92
+ - template/html.erb
91
93
  homepage: https://github.com/ollej/beer_recipe
92
94
  licenses:
93
95
  - MIT