hashicorp-checkpoint 0.1.0 → 0.1.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: 5f649f85c58122700694ccd0957e6d7565fa6521
4
- data.tar.gz: f9d9013fceefc71126aa8aa6b6e38a7bb8e9d0d2
3
+ metadata.gz: 4ecb84fe275a46ccb1c809131973885a3b4c9574
4
+ data.tar.gz: 821397cdf04825128fd2b84ebebb9b7c550dc8b2
5
5
  SHA512:
6
- metadata.gz: c645d470aa5589d03aaa900ebd4637d30a4fcf5a72db74cad5fd94d80c6238e7125538b17dc151346923dca539e2a63254c76eb106dd5ccc6014508b6f95d5ab
7
- data.tar.gz: 19d0e044f83a12555757a2d403dcf13e058043fc5e22438d1a6591e723ab52f192570b0c7e25d48a23fd616c7a036ff5a167e3cef9473394edc071057f1f5859
6
+ metadata.gz: 1b5add6e2913e2edd29ec8e7c7bee74fd5ad0b48bf1ea3105f75abcbbf9a1ab4e33a0e623cc27a2914b9eae7feeebe7c6471160d9d5ff3f5af64969c7bd712ba
7
+ data.tar.gz: ee8ae910a45245286fd18e2846cefaa13e8712b020e0567ffd0117b9faa342e456712317c4563f33259c3eec7d175d6fc41e68a002856b9912c3c38197f499ad
@@ -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
- JSON.parse(resp.body).tap do |result|
71
- result["outdated"] = !!result["outdated"]
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
@@ -1,3 +1,3 @@
1
1
  module Checkpoint
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashicorp-checkpoint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitchell Hashimoto