jsoncache 0.3.1 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsoncache.rb +53 -41
- 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: 0e485494df98519e1a66e6af9ed06704fe7aee85
|
4
|
+
data.tar.gz: fea11662810ce6e0b640a75fa90ea0728c1805b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5a63eec2084fef1a06d4237b75c2cf1e48aa27b5d62d570b461efd0024092d777fa63c644492c87e58cf7337ab8f738490f74de2157e66f73309142a9906242
|
7
|
+
data.tar.gz: 66dc29dfc029f62c6c8531ee12f0336c3bb98c1f2a8b6720d91f6291bb7c13346e27a632e2b94c2e53861eda3ee4c62fe1698b6c89067c2118ed7e94069b0cac
|
data/lib/jsoncache.rb
CHANGED
@@ -2,73 +2,85 @@ require 'json'
|
|
2
2
|
|
3
3
|
# JSONCache is a simple interface to cache JSON based API calls
|
4
4
|
module JSONCache
|
5
|
-
attr_accessor :cache_directory
|
5
|
+
attr_accessor :cache_directory, :symbolize_json
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
# Retrieves cached data for the specified key and caches the data provided
|
10
|
+
# if the cache isn't valid.
|
11
|
+
# @param [String] key
|
12
|
+
# @param [Fixnum] delta The upperbound timestamp difference of a valid
|
13
|
+
# cache, 0 if the result doesn't go stale.
|
14
|
+
def cache(key, delta = 0)
|
15
|
+
return retrieve_cache(key) if cached?(key, delta)
|
16
|
+
result = yield
|
17
|
+
cache_file(key, result)
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
21
|
+
# Create, if necessary, and return a cache directory
|
22
|
+
def cache_dir
|
23
|
+
cache_path = File.join('/tmp', @cache_directory || 'jsoncache')
|
24
|
+
Dir.mkdir(cache_path) unless Dir.exist?(cache_path)
|
25
|
+
cache_path
|
26
|
+
end
|
6
27
|
|
7
28
|
# Determine whether a file is cached and healthy
|
8
|
-
# @param [String]
|
9
|
-
# @param [
|
10
|
-
#
|
11
|
-
def cached?(
|
12
|
-
timestamp =
|
29
|
+
# @param [String] key The key in which to check for cached data.
|
30
|
+
# @param [Fixnum] delta The upperbound timestamp difference of a valid
|
31
|
+
# cache, 0 if the result doesn't go stale.
|
32
|
+
def cached?(key, delta = 0)
|
33
|
+
timestamp = timestamp_from_key(key)
|
13
34
|
if timestamp.zero?
|
14
35
|
false
|
15
|
-
elsif
|
16
|
-
(Time.now.to_i - timestamp) < delta
|
17
|
-
else
|
36
|
+
elsif delta.zero?
|
18
37
|
true
|
38
|
+
else
|
39
|
+
(Time.now.to_i - timestamp) < delta
|
19
40
|
end
|
20
41
|
end
|
21
42
|
|
22
43
|
# Cache the result from the uri
|
23
|
-
# @param [
|
24
|
-
# @param [
|
25
|
-
def cache_file(
|
44
|
+
# @param [String] key The key in which to check for cached data.
|
45
|
+
# @param [Hash] data The response to cache.
|
46
|
+
def cache_file(key, data)
|
26
47
|
cache_path = cache_dir
|
27
|
-
existing_file =
|
48
|
+
existing_file = filename_from_key(key)
|
28
49
|
last_path = "#{cache_path}/#{existing_file}"
|
50
|
+
return unless data.respond_to?(:to_h)
|
29
51
|
File.delete(last_path) if existing_file && File.exist?(last_path)
|
30
52
|
File.write(
|
31
|
-
"#{cache_path}/#{
|
32
|
-
JSON.generate(
|
53
|
+
"#{cache_path}/#{key}#{Time.now.to_i}.json",
|
54
|
+
JSON.generate(data.to_h))
|
33
55
|
end
|
34
56
|
|
35
|
-
|
57
|
+
# Retrieves a cached value from a key
|
58
|
+
# @param [String] key The key in which to check for cached data.
|
59
|
+
def retrieve_cache(key)
|
60
|
+
filename = filename_from_key(key)
|
61
|
+
return nil if filename.nil?
|
62
|
+
@symbolize_json = false if @symbolize_json.nil?
|
36
63
|
JSON.parse(
|
37
|
-
File.read("#{cache_dir}/#{
|
38
|
-
|
64
|
+
File.read("#{cache_dir}/#{filename}"),
|
65
|
+
symbolize_names: @symbolize_json)
|
39
66
|
end
|
40
67
|
|
41
|
-
private
|
42
|
-
|
43
|
-
# Create, if necessary, and return a cache directory
|
44
|
-
def cache_dir
|
45
|
-
cache_path = File.join('/tmp', @cache_directory)
|
46
|
-
Dir.mkdir(cache_path) unless Dir.exist?(cache_path)
|
47
|
-
cache_path
|
48
|
-
end
|
49
|
-
|
50
|
-
# Converts uri to the base portion of the filename
|
51
|
-
# TODO requires overwrite
|
52
|
-
# def uri_to_file_path_root(uri)
|
53
|
-
# uri.gsub(%r{[\.\/]|https:\/\/.*v\d\.\d|\?api=.*}, '')
|
54
|
-
# end
|
55
|
-
|
56
68
|
# Gets an existing file from a uri if it exists
|
57
|
-
|
58
|
-
|
69
|
+
# @param [String] key The key in which to check for cached data.
|
70
|
+
def filename_from_key(key)
|
59
71
|
Dir.foreach(cache_dir) do |filename|
|
60
|
-
next unless filename.include?(
|
72
|
+
next unless filename.include?(key)
|
61
73
|
return filename
|
62
74
|
end
|
63
75
|
end
|
64
76
|
|
65
77
|
# Extracts a timestamp from an existing file
|
66
|
-
|
67
|
-
|
78
|
+
# @param [String] key The key in which to check for cached data.
|
79
|
+
def timestamp_from_key(key)
|
80
|
+
path = filename_from_key(key)
|
68
81
|
return 0 if path.nil?
|
69
|
-
|
70
|
-
|
71
|
-
.gsub(/^#{last_pattern}/, '')
|
82
|
+
path.slice(/#{key}.*/)
|
83
|
+
.gsub(/^#{key}/, '')
|
72
84
|
.chomp('.json')
|
73
85
|
.to_i
|
74
86
|
end
|