vault-tools 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -65,3 +65,4 @@ require 'vault-tools/user'
65
65
  require 'vault-tools/web'
66
66
  require 'vault-tools/pipeline'
67
67
  require 'vault-tools/text_processor'
68
+ require 'vault-tools/time'
@@ -133,7 +133,7 @@ module Vault
133
133
  # @return [Fixnum] The number or nil if the value couldn't be coerced to a
134
134
  # Fixnum.
135
135
  def self.int(name)
136
- self[name] ? self[name].to_i : nil
136
+ self[name] && self[name].to_i
137
137
  end
138
138
 
139
139
  # Comma-separated words converted to an array.
@@ -155,8 +155,19 @@ module Vault
155
155
  self[name] == 'true'
156
156
  end
157
157
 
158
+ # An environment variable converted to a time.
159
+ #
160
+ # @param name [String|Symbol] The name of the environment variable to fetch a
161
+ # boolean for.
162
+ # @return [Time] Time if the value is parseable, otherwise false.
158
163
  def self.time(name)
159
- Time.parse(self[name])
164
+ if self[name]
165
+ Time.parse(self[name]) rescue Time.utc(*self[name].split('-'))
166
+ end
167
+ end
168
+
169
+ def self.uri(name)
170
+ self[name] && URI.parse(self[name])
160
171
  end
161
172
 
162
173
  # The number of threads to use in Sidekiq workers.
@@ -0,0 +1,30 @@
1
+ class Time
2
+ def last_month
3
+ (self.to_datetime << 1).to_time
4
+ end
5
+
6
+ def next_month
7
+ (self.to_datetime >> 1).to_time
8
+ end
9
+
10
+ def tomorrow
11
+ (self.to_datetime + 1).to_time
12
+ end
13
+
14
+ def yesterday
15
+ (self.to_datetime - 1).to_time
16
+ end
17
+ end
18
+
19
+ class Fixnum
20
+ def weeks
21
+ self.days * 7
22
+ end
23
+ alias :week :weeks
24
+
25
+ def days
26
+ self * 24 * 60 * 60
27
+ end
28
+ alias :day :days
29
+ end
30
+
@@ -1,5 +1,5 @@
1
1
  module Vault
2
2
  module Tools
3
- VERSION = '0.4.3'
3
+ VERSION = '0.4.4'
4
4
  end
5
5
  end
@@ -133,6 +133,33 @@ class ConfigTest < Vault::TestCase
133
133
  assert_equal(3000, Config.int('FOO'))
134
134
  end
135
135
 
136
+ # Config.time returns nil or VAR as time
137
+ def test_time
138
+ assert_equal(nil, Config.time('T'))
139
+ set_env 'T', '2000'
140
+ assert_equal(Time.utc(2000), Config.time(:t))
141
+ set_env 'T', '2000-2'
142
+ assert_equal(Time.utc(2000,2), Config.time(:t))
143
+ set_env 'T', '2000-2-2'
144
+ assert_equal(Time.utc(2000,2,2), Config.time(:t))
145
+ set_env 'T', '2000-2-2T11:11'
146
+ assert_equal(Time.utc(2000,2,2,11,11), Config.time(:t))
147
+ end
148
+
149
+ # Config.time returns nil or VAR as URI
150
+ def test_uri
151
+ assert_equal(nil, Config.uri('URL'))
152
+ set_env 'URL', 'http://user:password@the-web.com/path/to/greatness?foo=bar'
153
+ uri = Config.uri('URL')
154
+ assert_equal('http', uri.scheme)
155
+ assert_equal('the-web.com', uri.host)
156
+ assert_equal('/path/to/greatness', uri.path)
157
+ assert_equal('foo=bar', uri.query)
158
+ assert_equal(80, uri.port)
159
+ assert_equal('user', uri.user)
160
+ assert_equal('password', uri.password)
161
+ end
162
+
136
163
  # Config.array loads a comma-separated list of words into an array of
137
164
  # strings.
138
165
  def test_array
@@ -1,17 +1,95 @@
1
1
  require 'helper'
2
2
 
3
+ # TODO: Move this an the implementation to Vault::Tools
3
4
  class TimeTest < Vault::TestCase
5
+ def test_next_month
6
+ # works on simple case
7
+ assert_equal(Time.utc(2000,2),
8
+ Time.utc(2000,1).next_month)
9
+ # preserves day
10
+ assert_equal(Time.utc(2000,2,5),
11
+ Time.utc(2000,1,5).next_month)
12
+ # year boundary
13
+ assert_equal(Time.utc(2011,1),
14
+ Time.utc(2010,12).next_month)
15
+ end
16
+
17
+ def test_last_month
18
+ # works on simple case
19
+ assert_equal(Time.utc(2000,1),
20
+ Time.utc(2000,2).last_month)
21
+ # preserves day
22
+ assert_equal(Time.utc(2000,1,5),
23
+ Time.utc(2000,2,5).last_month)
24
+ # year boundary
25
+ assert_equal(Time.utc(2010,12),
26
+ Time.utc(2011,1).last_month)
27
+ end
28
+
29
+ def test_month_chaining
30
+ start = Time.utc(2000)
31
+ assert_equal(start, start.next_month.last_month)
32
+ assert_equal(start, start.last_month.next_month)
33
+ assert_equal(start,
34
+ Time.utc(2000,3).last_month.last_month)
35
+ assert_equal(start,
36
+ Time.utc(1999,11).next_month.next_month)
37
+
38
+ end
39
+
40
+ def test_tomorrow
41
+ # works on simple case
42
+ assert_equal(Time.utc(2000,1,2),
43
+ Time.utc(2000,1,1).tomorrow)
44
+ # preserves hour
45
+ assert_equal(Time.utc(2000,1,6,11),
46
+ Time.utc(2000,1,5,11).tomorrow)
47
+ # month/year boundary
48
+ assert_equal(Time.utc(2011,1),
49
+ Time.utc(2010,12,31).tomorrow)
50
+ end
4
51
 
5
- # We're always using UTC time
6
- def test_now_is_utc
7
- local = Time.local(2012,1,1)
8
- utc = Time.utc(2012,1,1)
9
- assert_equal(utc, local)
52
+ def test_yesterday
53
+ # works on simple case
54
+ assert_equal(Time.utc(2000,1,2),
55
+ Time.utc(2000,1,1).tomorrow)
56
+ # preserves hour
57
+ assert_equal(Time.utc(2000,1,6,11),
58
+ Time.utc(2000,1,5,11).tomorrow)
59
+ # month boundary
60
+ assert_equal(Time.utc(2011,1),
61
+ Time.utc(2010,12,31).tomorrow)
10
62
  end
11
63
 
12
- # Default time format is iso8601
13
- def test_to_s_is_iso8601
14
- t = Time.utc(2012,1,1)
15
- assert_equal(t.to_s, t.iso8601)
64
+ def test_yesterday_tomorrow_chaining
65
+ start = Time.utc(2000)
66
+ assert_equal(start, start.tomorrow.yesterday)
67
+ assert_equal(start, start.yesterday.tomorrow)
68
+ assert_equal(start, Time.utc(1999,12,30).tomorrow.tomorrow)
69
+ assert_equal(start, Time.utc(2000,1,3).yesterday.yesterday)
16
70
  end
71
+
72
+ def test_macbeth
73
+ assert_equal(Time.utc(2000,1,4),
74
+ Time.utc(2000,1,1).tomorrow.tomorrow.tomorrow)
75
+ end
76
+
77
+ def test_days
78
+ assert_equal(24*60*60,1.days)
79
+ assert_equal(24*60*60,1.day)
80
+ assert_equal(Time.utc(2000,1,1) + 1.days,
81
+ Time.utc(2000,1,1).tomorrow)
82
+ assert_equal(Time.utc(2000,1,1) + 1.day,
83
+ Time.utc(2000,1,1).tomorrow)
84
+ end
85
+
86
+ def test_weeks
87
+ assert_equal(7*24*60*60*2,2.weeks)
88
+ assert_equal(7*24*60*60,1.week)
89
+ assert_equal(Time.utc(2000,1,8),
90
+ Time.utc(2000,1,1) + 1.week)
91
+ assert_equal(Time.utc(2000,1,15),
92
+ Time.utc(2000,1,1) + 2.weeks)
93
+ end
94
+
17
95
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vault-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-11-14 00:00:00.000000000 Z
13
+ date: 2013-11-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: scrolls
@@ -152,6 +152,7 @@ files:
152
152
  - lib/vault-tools/product.rb
153
153
  - lib/vault-tools/sinatra_helpers/html_serializer.rb
154
154
  - lib/vault-tools/text_processor.rb
155
+ - lib/vault-tools/time.rb
155
156
  - lib/vault-tools/user.rb
156
157
  - lib/vault-tools/version.rb
157
158
  - lib/vault-tools/web.rb