jsoncache 0.5.3 → 0.5.4
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/jsoncache.rb +22 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 451ecce81c2292abf00ea4b993b08ac32236b592
|
4
|
+
data.tar.gz: 10f0b235b42b7b291db910eeee69d25f29687fc9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9002060bd1ef998f9ffbb8ed10243d024ea9da6ca9407cbe10cdc3da3f966514a074a1b8cc0a91334c53c3a54275bf26a4c702e964a05078a0e3deed28423daa
|
7
|
+
data.tar.gz: e6814adf6f6d8fba88c676c6eaa6dcacf64a85690cad8f21db0154e319bcc7fbe9bb7db89fd3cd1066fa2e3d75d93bb12f26832bc4fdfb9b1df429edb1cce019
|
data/lib/jsoncache.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'pp'
|
2
3
|
|
3
4
|
# JSONCache is a simple interface to cache JSON based API calls
|
4
5
|
#
|
@@ -25,16 +26,17 @@ module JSONCache
|
|
25
26
|
#
|
26
27
|
# +:symbolize+:: +Boolean+ Symbolize keys while parsing JSON.
|
27
28
|
# +:cache_directory+:: +String+ The folder name in /tmp to use as the cache.
|
28
|
-
# +:
|
29
|
+
# +:expiry+:: +Fixnum+ The validity time of the cache in seconds.
|
29
30
|
#
|
30
31
|
# ==== Examples
|
31
32
|
#
|
32
33
|
# def get_response(uri)
|
33
|
-
# JSONCache.cache(uri_to_key(uri),
|
34
|
+
# JSONCache.cache(uri_to_key(uri), expiry: 120) do
|
34
35
|
# query_some_json_api(uri)
|
35
36
|
# end
|
36
37
|
# end
|
37
38
|
def cache(key, options = {})
|
39
|
+
options = defaults.merge(options)
|
38
40
|
return retrieve_cache(key, options) if cached?(key, options)
|
39
41
|
result = yield
|
40
42
|
cache_file(key, result, options)
|
@@ -54,14 +56,15 @@ module JSONCache
|
|
54
56
|
# +key+:: +String+ The key in which to check for cached data.
|
55
57
|
# +options+:: +Hash+ A hash of the parameters to use when caching.
|
56
58
|
def cached?(key, options = {})
|
59
|
+
options = defaults.merge(options)
|
57
60
|
timestamp = timestamp_from_key(key, options[:cache_directory])
|
58
|
-
|
61
|
+
expiry = options[:expiry]
|
59
62
|
if timestamp.zero?
|
60
63
|
false
|
61
|
-
elsif
|
64
|
+
elsif expiry.zero?
|
62
65
|
true
|
63
66
|
else
|
64
|
-
(Time.now.to_i - timestamp) <
|
67
|
+
(Time.now.to_i - timestamp) < expiry
|
65
68
|
end
|
66
69
|
end
|
67
70
|
|
@@ -73,12 +76,12 @@ module JSONCache
|
|
73
76
|
# +data+:: +Hash+ The data to cache.
|
74
77
|
# +options+:: +Hash+ A hash of the parameters to use when caching.
|
75
78
|
def cache_file(key, data, options = {})
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
79
|
+
options = defaults.merge(options)
|
80
|
+
begin
|
81
|
+
content = JSON.generate(data)
|
82
|
+
rescue
|
83
|
+
return
|
84
|
+
end
|
82
85
|
|
83
86
|
cache_path = cache_dir(options[:cache_directory])
|
84
87
|
existing_file = filename_from_key(key, options[:cache_directory])
|
@@ -87,7 +90,7 @@ module JSONCache
|
|
87
90
|
File.delete(last_path) if existing_file && File.exist?(last_path)
|
88
91
|
File.write(
|
89
92
|
"#{cache_path}/#{key}#{Time.now.to_i}.json",
|
90
|
-
|
93
|
+
content)
|
91
94
|
end
|
92
95
|
|
93
96
|
# Retrieves a cached value from a key
|
@@ -97,13 +100,13 @@ module JSONCache
|
|
97
100
|
# +key+:: +String+ The key in which to check for cached data.
|
98
101
|
# +options+:: +Hash+ A hash of the parameters to use when caching.
|
99
102
|
def retrieve_cache(key, options = {})
|
103
|
+
options = defaults.merge(options)
|
100
104
|
directory = options[:cache_directory]
|
101
105
|
filename = filename_from_key(key, directory)
|
102
106
|
return nil if filename.nil?
|
103
|
-
symbolize = options[:symbolize] || false
|
104
107
|
JSON.parse(
|
105
108
|
File.read("#{cache_dir(directory)}/#{filename}"),
|
106
|
-
symbolize_names: symbolize)
|
109
|
+
symbolize_names: options[:symbolize])
|
107
110
|
end
|
108
111
|
|
109
112
|
########################################################################
|
@@ -116,7 +119,7 @@ module JSONCache
|
|
116
119
|
#
|
117
120
|
# +directory+:: +String+ The name of the cache directory.
|
118
121
|
def cache_dir(directory)
|
119
|
-
directory ||=
|
122
|
+
directory ||= defaults[:cache_directory]
|
120
123
|
cache_path = File.join('/tmp', directory)
|
121
124
|
Dir.mkdir(cache_path) unless Dir.exist?(cache_path)
|
122
125
|
cache_path
|
@@ -129,7 +132,6 @@ module JSONCache
|
|
129
132
|
# +key+:: +String+ The key in which to check for cached data.
|
130
133
|
# +directory+:: +String+ The name of the cache directory.
|
131
134
|
def filename_from_key(key, directory)
|
132
|
-
directory ||= 'jsoncache'
|
133
135
|
Dir.foreach(cache_dir(directory)) do |filename|
|
134
136
|
next unless filename.include?(key)
|
135
137
|
return filename
|
@@ -150,4 +152,8 @@ module JSONCache
|
|
150
152
|
.chomp('.json')
|
151
153
|
.to_i
|
152
154
|
end
|
155
|
+
|
156
|
+
def defaults
|
157
|
+
{ symbolize: false, cache_directory: 'jsoncache', expiry: 0 }
|
158
|
+
end
|
153
159
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsoncache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derek Stride
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|