hashicorp-checkpoint 0.1.0 → 0.1.1
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 +4 -4
- data/lib/checkpoint.rb +30 -2
- data/lib/checkpoint/version.rb +1 -1
- data/spec/checkpoint_spec.rb +21 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ecb84fe275a46ccb1c809131973885a3b4c9574
|
4
|
+
data.tar.gz: 821397cdf04825128fd2b84ebebb9b7c550dc8b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b5add6e2913e2edd29ec8e7c7bee74fd5ad0b48bf1ea3105f75abcbbf9a1ab4e33a0e623cc27a2914b9eae7feeebe7c6471160d9d5ff3f5af64969c7bd712ba
|
7
|
+
data.tar.gz: ee8ae910a45245286fd18e2846cefaa13e8712b020e0567ffd0117b9faa342e456712317c4563f33259c3eec7d175d6fc41e68a002856b9912c3c38197f499ad
|
data/lib/checkpoint.rb
CHANGED
@@ -21,7 +21,22 @@ module Checkpoint
|
|
21
21
|
# @option opts [String] :signature_file If specified, a signature will
|
22
22
|
# be read from this path. If it doesn't exist, it will be created with
|
23
23
|
# a new random signature.
|
24
|
+
# @option opts [String] :cache_file If specified, the response will be
|
25
|
+
# cached here for cache_time period (defaults to 48 hours).
|
24
26
|
def self.check(**opts)
|
27
|
+
# If we have the cache file, then just return the contents.
|
28
|
+
if opts[:cache_file] && File.file?(opts[:cache_file])
|
29
|
+
# If the cache file is too old, then delete it
|
30
|
+
mtime = File.mtime(opts[:cache_file]).to_i
|
31
|
+
limit = Time.now.to_i - (60 * 60 * 24 * 2)
|
32
|
+
if mtime > limit
|
33
|
+
return build_check(File.read(opts[:cache_file]))
|
34
|
+
end
|
35
|
+
|
36
|
+
# Delete the file
|
37
|
+
File.unlink(opts[:cache_file])
|
38
|
+
end
|
39
|
+
|
25
40
|
# Build the query parameters
|
26
41
|
query = {
|
27
42
|
version: opts[:version],
|
@@ -67,11 +82,24 @@ module Checkpoint
|
|
67
82
|
return nil
|
68
83
|
end
|
69
84
|
|
70
|
-
|
71
|
-
|
85
|
+
# If we have a cache file, write it
|
86
|
+
if opts[:cache_file]
|
87
|
+
File.open(opts[:cache_file], "w+") do |f|
|
88
|
+
f.write(resp.body)
|
89
|
+
end
|
72
90
|
end
|
91
|
+
|
92
|
+
build_check(resp.body)
|
73
93
|
rescue Exception
|
74
94
|
# We don't want check to fail for any reason, so just return nil
|
75
95
|
return nil
|
76
96
|
end
|
97
|
+
|
98
|
+
protected
|
99
|
+
|
100
|
+
def self.build_check(response)
|
101
|
+
JSON.parse(response).tap do |result|
|
102
|
+
result["outdated"] = !!result["outdated"]
|
103
|
+
end
|
104
|
+
end
|
77
105
|
end
|
data/lib/checkpoint/version.rb
CHANGED
data/spec/checkpoint_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "tempfile"
|
2
|
+
|
1
3
|
require "helper"
|
2
4
|
|
3
5
|
describe Checkpoint do
|
@@ -15,5 +17,24 @@ describe Checkpoint do
|
|
15
17
|
its(["outdated"]) { should eq(false) }
|
16
18
|
its(["product"]) { should eq("test") }
|
17
19
|
end
|
20
|
+
|
21
|
+
it "should cache things with cache_file" do
|
22
|
+
tf = Tempfile.new("checkpoint")
|
23
|
+
path = tf.path
|
24
|
+
tf.close
|
25
|
+
File.unlink(path)
|
26
|
+
|
27
|
+
opts = {
|
28
|
+
product: "test",
|
29
|
+
version: "1.0",
|
30
|
+
cache_file: path,
|
31
|
+
}
|
32
|
+
|
33
|
+
# Just run it twice
|
34
|
+
c = described_class.check(opts)
|
35
|
+
c = described_class.check(opts)
|
36
|
+
|
37
|
+
expect(c["product"]).to eq("test")
|
38
|
+
end
|
18
39
|
end
|
19
40
|
end
|