jsoncache 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jsoncache.rb +53 -41
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ac4af13f9b13e89db67a6b9767e1fbfb3e9915a
4
- data.tar.gz: d9ad825180586ebfc54fe89cd67adb497153cc75
3
+ metadata.gz: 0e485494df98519e1a66e6af9ed06704fe7aee85
4
+ data.tar.gz: fea11662810ce6e0b640a75fa90ea0728c1805b1
5
5
  SHA512:
6
- metadata.gz: 5462932d8627ad9efce646253cd17d6121ad626228fd13933cd3f95a06b5c6ed2ab438d1f8d87d2e8eefa0c70e04cd5012599113057bee21e5ff6306a84d10aa
7
- data.tar.gz: 040e9587050f419bd36c9f3adaf1494addbf98dd3a2d8ec25e664274a977cf4005a21d1e5e48319b64a0300ef3fe3dd6bfc3642a82e927701c09988ab633573c
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] uri the uri in which to check for a cached call.
9
- # @param [boolean] stale whether or not a cached file can go stale.
10
- # @param [Fixnum] delta the upperbound timestamp difference of a valid cache.
11
- def cached?(uri, delta = 0)
12
- timestamp = timestamp_from_uri(uri)
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 !delta.zero?
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 [Hash] response the response to cache.
24
- # @param [String] uri the uri in which to check for a cached call.
25
- def cache_file(response, uri)
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 = filename_from_uri(uri)
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}/#{uri_to_file_path_root(uri)}#{Time.now.to_i}.json",
32
- JSON.generate(response))
53
+ "#{cache_path}/#{key}#{Time.now.to_i}.json",
54
+ JSON.generate(data.to_h))
33
55
  end
34
56
 
35
- def retrieve_cache(uri, params = {})
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}/#{filename_from_uri(uri)}"),
38
- params)
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
- def filename_from_uri(uri)
58
- root_path = uri_to_file_path_root(uri)
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?(root_path)
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
- def timestamp_from_uri(uri)
67
- path = filename_from_uri(uri)
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
- last_pattern = uri_to_file_path_root(uri)
70
- path.slice(/#{last_pattern}.*/)
71
- .gsub(/^#{last_pattern}/, '')
82
+ path.slice(/#{key}.*/)
83
+ .gsub(/^#{key}/, '')
72
84
  .chomp('.json')
73
85
  .to_i
74
86
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsoncache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Stride