env_parser 0.1.0 → 0.2.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: 996c5fec8869ff3a8103aba00de5f1a24e342b49
4
- data.tar.gz: 31edfa384c9241b835856b3e80424c202c0b5a84
3
+ metadata.gz: 9709d56b0998b3cddaed366bbcbeec3c58a1eabf
4
+ data.tar.gz: 8d6566e451cfb918e26fd686582bb3dc2f106a8e
5
5
  SHA512:
6
- metadata.gz: 1a60f7d00a885caa1ccc91adb3beaed8bcc010262e7d5c82b9524979e6919d7ffe8aea5ceed53407388b62f09c7d4d684a2bd259c84cce7e3ba0da7cdd711623
7
- data.tar.gz: e1d19bce6fd537377c1a71458021883870ab3051869a4b410bbf1e1edf1fa3044cc736ebd86af62eb025098c284b3c7e87c33222c23fcfb6ba32538fdd3c5eed
6
+ metadata.gz: 7618c2104a3e48b625c9e6f9999fd1fb7308434b7c83b66c4f8880f1be369ecd4a96d1c9a4caba2bbcba50323e9f6aaefb2cfd7592a3664c61dacc165e03ba93
7
+ data.tar.gz: b4f4c4333d98ba7a20c9aa876fc649e66a127f6784b3bc770079f365c5def4ef70bcaae006cf21ca1d0beac09c5f20089f66123640386636d90fb24fefbff433
data/docs/EnvParser.html CHANGED
@@ -141,7 +141,7 @@ different data types.</p>
141
141
  <li class="public ">
142
142
  <span class="summary_signature">
143
143
 
144
- <a href="#parse-class_method" title="parse (class method)">.<strong>parse</strong>(value, as: nil) &#x21d2; Object </a>
144
+ <a href="#parse-class_method" title="parse (class method)">.<strong>parse</strong>(value, options = {}) &#x21d2; Object </a>
145
145
 
146
146
 
147
147
 
@@ -174,7 +174,7 @@ different data types.</p>
174
174
  <div class="method_details first">
175
175
  <h3 class="signature first" id="parse-class_method">
176
176
 
177
- .<strong>parse</strong>(value, as: nil) &#x21d2; <tt>Object</tt>
177
+ .<strong>parse</strong>(value, options = {}) &#x21d2; <tt>Object</tt>
178
178
 
179
179
 
180
180
 
@@ -212,15 +212,40 @@ be used.</p>
212
212
 
213
213
  <li>
214
214
 
215
- <span class='name'>:as</span>
215
+ <span class='name'>options</span>
216
216
 
217
217
 
218
- <span class='type'>(<tt>Symbol</tt>)</span>
218
+ <span class='type'>(<tt>Hash</tt>)</span>
219
219
 
220
220
 
221
+ <em class="default">(defaults to: <tt>{}</tt>)</em>
222
+
221
223
 
222
224
  &mdash;
223
225
  <div class='inline'>
226
+ <p>a customizable set of options</p>
227
+ </div>
228
+
229
+ </li>
230
+
231
+ </ul>
232
+
233
+
234
+
235
+
236
+
237
+
238
+ <p class="tag_title">Options Hash (<tt>options</tt>):</p>
239
+ <ul class="option">
240
+
241
+ <li>
242
+ <span class="name">as</span>
243
+ <span class="type">(<tt>Symbol</tt>)</span>
244
+ <span class="default">
245
+
246
+ </span>
247
+
248
+ &mdash; <div class='inline'>
224
249
  <p>The expected return type. A best-effort attempt is made to convert the
225
250
  source String to the requested type. Valid “as” types are:</p>
226
251
  <ul><li>
@@ -241,10 +266,28 @@ source String to the requested type. Valid “as” types are:</p>
241
266
  <p><code>:hash</code></p>
242
267
  </li></ul>
243
268
  </div>
269
+
270
+ </li>
244
271
 
245
- </li>
272
+ <li>
273
+ <span class="name">if_unset</span>
274
+ <span class="type">(<tt>Object</tt>)</span>
275
+ <span class="default">
276
+
277
+ </span>
278
+
279
+ &mdash; <div class='inline'>
280
+ <p>Specifies the default value to return if the given “value” is either nil or
281
+ an empty String (&#39;&#39;). Any “if_unset” value given will be returned
282
+ as-is, with no type conversion or other change having been made. If
283
+ unspecified, the “default” value for nil/&#39;&#39; input will depend on
284
+ the “as” type.</p>
285
+ </div>
286
+
287
+ </li>
288
+
289
+ </ul>
246
290
 
247
- </ul>
248
291
 
249
292
 
250
293
  </div><table class="source_code">
@@ -253,13 +296,6 @@ source String to the requested type. Valid “as” types are:</p>
253
296
  <pre class="lines">
254
297
 
255
298
 
256
- 26
257
- 27
258
- 28
259
- 29
260
- 30
261
- 31
262
- 32
263
299
  33
264
300
  34
265
301
  35
@@ -271,19 +307,30 @@ source String to the requested type. Valid “as” types are:</p>
271
307
  41
272
308
  42
273
309
  43
274
- 44</pre>
310
+ 44
311
+ 45
312
+ 46
313
+ 47
314
+ 48
315
+ 49
316
+ 50
317
+ 51
318
+ 52
319
+ 53</pre>
275
320
  </td>
276
321
  <td>
277
- <pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 26</span>
322
+ <pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 33</span>
278
323
 
279
- <span class='kw'>def</span> <span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='kw'>nil</span><span class='rparen'>)</span>
324
+ <span class='kw'>def</span> <span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
280
325
  <span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span> <span class='const'>Symbol</span>
281
326
  <span class='const'>ENV</span><span class='lbracket'>[</span><span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='rbracket'>]</span>
282
327
  <span class='kw'>else</span>
283
328
  <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span>
284
329
  <span class='kw'>end</span>
285
330
 
286
- <span class='kw'>case</span> <span class='id identifier rubyid_as'>as</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
331
+ <span class='kw'>return</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:if_unset</span><span class='rbracket'>]</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span><span class='lparen'>(</span><span class='symbol'>:if_unset</span><span class='rparen'>)</span>
332
+
333
+ <span class='kw'>case</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:as</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
287
334
  <span class='kw'>when</span> <span class='symbol'>:string</span> <span class='kw'>then</span> <span class='id identifier rubyid_parse_string'>parse_string</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
288
335
  <span class='kw'>when</span> <span class='symbol'>:symbol</span> <span class='kw'>then</span> <span class='id identifier rubyid_parse_symbol'>parse_symbol</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
289
336
  <span class='kw'>when</span> <span class='symbol'>:boolean</span> <span class='kw'>then</span> <span class='id identifier rubyid_parse_boolean'>parse_boolean</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
@@ -292,7 +339,7 @@ source String to the requested type. Valid “as” types are:</p>
292
339
  <span class='kw'>when</span> <span class='symbol'>:json</span> <span class='kw'>then</span> <span class='id identifier rubyid_parse_json'>parse_json</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
293
340
  <span class='kw'>when</span> <span class='symbol'>:array</span> <span class='kw'>then</span> <span class='id identifier rubyid_parse_array'>parse_array</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
294
341
  <span class='kw'>when</span> <span class='symbol'>:hash</span> <span class='kw'>then</span> <span class='id identifier rubyid_parse_hash'>parse_hash</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
295
- <span class='kw'>else</span> <span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>invalid `as` parameter: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_as'>as</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span>
342
+ <span class='kw'>else</span> <span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>invalid `as` parameter: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:as</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span>
296
343
  <span class='kw'>end</span>
297
344
  <span class='kw'>end</span></pre>
298
345
  </td>
@@ -305,7 +352,7 @@ source String to the requested type. Valid “as” types are:</p>
305
352
  </div>
306
353
 
307
354
  <div id="footer">
308
- Generated on Mon Nov 27 21:58:12 2017 by
355
+ Generated on Wed Nov 29 06:44:05 2017 by
309
356
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
310
357
  0.9.11 (ruby-2.4.2).
311
358
  </div>
data/docs/_index.html CHANGED
@@ -97,7 +97,7 @@
97
97
  </div>
98
98
 
99
99
  <div id="footer">
100
- Generated on Mon Nov 27 21:58:11 2017 by
100
+ Generated on Wed Nov 29 06:44:04 2017 by
101
101
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
102
102
  0.9.11 (ruby-2.4.2).
103
103
  </div>
@@ -172,7 +172,7 @@ href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
172
172
  </div></div>
173
173
 
174
174
  <div id="footer">
175
- Generated on Mon Nov 27 21:58:12 2017 by
175
+ Generated on Wed Nov 29 06:44:05 2017 by
176
176
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
177
177
  0.9.11 (ruby-2.4.2).
178
178
  </div>
data/docs/index.html CHANGED
@@ -172,7 +172,7 @@ href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
172
172
  </div></div>
173
173
 
174
174
  <div id="footer">
175
- Generated on Mon Nov 27 21:58:11 2017 by
175
+ Generated on Wed Nov 29 06:44:05 2017 by
176
176
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
177
177
  0.9.11 (ruby-2.4.2).
178
178
  </div>
@@ -100,7 +100,7 @@
100
100
  </div>
101
101
 
102
102
  <div id="footer">
103
- Generated on Mon Nov 27 21:58:12 2017 by
103
+ Generated on Wed Nov 29 06:44:05 2017 by
104
104
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
105
105
  0.9.11 (ruby-2.4.2).
106
106
  </div>
@@ -1,3 +1,3 @@
1
1
  class EnvParser
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/env_parser.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'env_parser/version'
2
+ require 'active_support/all'
2
3
 
3
4
  ## The EnvParser class simplifies parsing of environment variables as different data types.
4
5
  ##
@@ -10,7 +11,7 @@ class EnvParser
10
11
  ## The value to parse/interpret. If a String is given, the value will be used as-is. If a
11
12
  ## Symbol is given, the ENV value for the matching string key will be used.
12
13
  ##
13
- ## @param :as [Symbol]
14
+ ## @option options as [Symbol]
14
15
  ## The expected return type. A best-effort attempt is made to convert the source String to the
15
16
  ## requested type. Valid "as" types are:
16
17
  ##
@@ -23,14 +24,22 @@ class EnvParser
23
24
  ## - `:array`
24
25
  ## - `:hash`
25
26
  ##
26
- def parse(value, as: nil)
27
+ ## @option options if_unset
28
+ ## Specifies the default value to return if the given "value" is either nil or an empty String
29
+ ## (''). Any "if_unset" value given will be returned as-is, with no type conversion or other
30
+ ## change having been made. If unspecified, the "default" value for nil/'' input will depend
31
+ ## on the "as" type.
32
+ ##
33
+ def parse(value, options = {})
27
34
  value = if value.is_a? Symbol
28
35
  ENV[value.to_s]
29
36
  else
30
37
  value.to_s
31
38
  end
32
39
 
33
- case as.to_sym
40
+ return options[:if_unset] if value.blank? && options.key?(:if_unset)
41
+
42
+ case options[:as].to_sym
34
43
  when :string then parse_string(value)
35
44
  when :symbol then parse_symbol(value)
36
45
  when :boolean then parse_boolean(value)
@@ -39,7 +48,7 @@ class EnvParser
39
48
  when :json then parse_json(value)
40
49
  when :array then parse_array(value)
41
50
  when :hash then parse_hash(value)
42
- else raise ArgumentError, "invalid `as` parameter: #{as.inspect}"
51
+ else raise ArgumentError, "invalid `as` parameter: #{options[:as].inspect}"
43
52
  end
44
53
  end
45
54
 
@@ -70,16 +79,15 @@ class EnvParser
70
79
 
71
80
  def parse_json(value)
72
81
  require 'json'
73
- require 'active_support/all'
74
82
 
75
- return nil if value.nil? || (value == '')
83
+ return nil if value.blank?
76
84
 
77
85
  decoded_json = JSON.parse(value, quirks_mode: true)
78
86
  { decoded_json: decoded_json }.with_indifferent_access[:decoded_json]
79
87
  end
80
88
 
81
89
  def parse_array(value)
82
- return [] if value.nil? || (value == '')
90
+ return [] if value.blank?
83
91
 
84
92
  decoded_json = parse_json(value)
85
93
  raise(ArgumentError, 'non-array value') unless decoded_json.is_a? Array
@@ -88,7 +96,7 @@ class EnvParser
88
96
  end
89
97
 
90
98
  def parse_hash(value)
91
- return {} if value.nil? || (value == '')
99
+ return {} if value.blank?
92
100
 
93
101
  decoded_json = parse_json(value)
94
102
  raise(ArgumentError, 'non-hash value') unless decoded_json.is_a? Hash
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nestor Custodio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-28 00:00:00.000000000 Z
11
+ date: 2017-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler