jsoncache 0.4.1 → 0.5.0

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 -32
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e485494df98519e1a66e6af9ed06704fe7aee85
4
- data.tar.gz: fea11662810ce6e0b640a75fa90ea0728c1805b1
3
+ metadata.gz: b8b7e2aa8007592f2c122d828b77bf832c495d93
4
+ data.tar.gz: e7edbb2504cb92aac6ac04dcf12802c71487f152
5
5
  SHA512:
6
- metadata.gz: a5a63eec2084fef1a06d4237b75c2cf1e48aa27b5d62d570b461efd0024092d777fa63c644492c87e58cf7337ab8f738490f74de2157e66f73309142a9906242
7
- data.tar.gz: 66dc29dfc029f62c6c8531ee12f0336c3bb98c1f2a8b6720d91f6291bb7c13346e27a632e2b94c2e53861eda3ee4c62fe1698b6c89067c2118ed7e94069b0cac
6
+ metadata.gz: 34ed662640fb70a27ee10b8cd42a2e938cb5a5626e1f4baf97fa9f637aab3b925645850da4a6ee7deb653f9cd9b60a14684644c65ba595c5ae77d67d9e79187d
7
+ data.tar.gz: 907ea8767eb4b034b087c9b8de095614c9c35167e103b21ca5c1a49c869d2752e41b62f5e3b4c33ef28c80d775ba2fef70ddf6b510e4e8da5114c9ca939bf9df
data/lib/jsoncache.rb CHANGED
@@ -2,35 +2,35 @@ 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, :symbolize_json
6
-
7
- private
5
+ extend self
8
6
 
9
7
  # Retrieves cached data for the specified key and caches the data provided
10
8
  # 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)
9
+ # @param [String] key The key in which to check for cached data.
10
+ # @param [Hash] options A hash of the parameters to use when caching.
11
+ # Accepted options parameters
12
+ # => [Boolean] symbolize
13
+ # => [String] cache_directory
14
+ # => [Fixnum] delta
15
+ def cache(key, options = {})
16
+ return retrieve_cache(key, options) if cached?(key, options)
16
17
  result = yield
17
- cache_file(key, result)
18
+ cache_file(key, result, options)
18
19
  result
19
20
  end
20
21
 
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
22
+ private
23
+
24
+ ########################################################################
25
+ # Core Caching
26
+ ########################################################################
27
27
 
28
28
  # Determine whether a file is cached and healthy
29
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)
30
+ # @param [Hash] options A hash of the parameters to use when caching.
31
+ def cached?(key, options = {})
32
+ timestamp = timestamp_from_key(key, options[:cache_directory])
33
+ delta = options[:delta] || 0
34
34
  if timestamp.zero?
35
35
  false
36
36
  elsif delta.zero?
@@ -43,11 +43,14 @@ module JSONCache
43
43
  # Cache the result from the uri
44
44
  # @param [String] key The key in which to check for cached data.
45
45
  # @param [Hash] data The response to cache.
46
- def cache_file(key, data)
47
- cache_path = cache_dir
48
- existing_file = filename_from_key(key)
49
- last_path = "#{cache_path}/#{existing_file}"
46
+ # @param [Hash] options A hash of the parameters to use when caching.
47
+ def cache_file(key, data, options = {})
50
48
  return unless data.respond_to?(:to_h)
49
+
50
+ cache_path = cache_dir(options[:cache_directory])
51
+ existing_file = filename_from_key(key, options[:cache_directory])
52
+ last_path = "#{cache_path}/#{existing_file}"
53
+
51
54
  File.delete(last_path) if existing_file && File.exist?(last_path)
52
55
  File.write(
53
56
  "#{cache_path}/#{key}#{Time.now.to_i}.json",
@@ -56,19 +59,36 @@ module JSONCache
56
59
 
57
60
  # Retrieves a cached value from a key
58
61
  # @param [String] key The key in which to check for cached data.
59
- def retrieve_cache(key)
60
- filename = filename_from_key(key)
62
+ # @param [Hash] options A hash of the parameters to use when caching.
63
+ def retrieve_cache(key, options = {})
64
+ directory = options[:cache_directory]
65
+ filename = filename_from_key(key, directory)
61
66
  return nil if filename.nil?
62
- @symbolize_json = false if @symbolize_json.nil?
67
+ symbolize = options[:symbolize] || false
63
68
  JSON.parse(
64
- File.read("#{cache_dir}/#{filename}"),
65
- symbolize_names: @symbolize_json)
69
+ File.read("#{cache_dir(directory)}/#{filename}"),
70
+ symbolize_names: symbolize)
71
+ end
72
+
73
+ ########################################################################
74
+ # Helpers
75
+ ########################################################################
76
+
77
+ # Create, if necessary, and return a cache directory
78
+ # @param [String] directory The name of the cache directory.
79
+ def cache_dir(directory)
80
+ directory ||= 'jsoncache'
81
+ cache_path = File.join('/tmp', directory)
82
+ Dir.mkdir(cache_path) unless Dir.exist?(cache_path)
83
+ cache_path
66
84
  end
67
85
 
68
86
  # Gets an existing file from a uri if it exists
69
87
  # @param [String] key The key in which to check for cached data.
70
- def filename_from_key(key)
71
- Dir.foreach(cache_dir) do |filename|
88
+ # @param [String] directory The name of the cache directory.
89
+ def filename_from_key(key, directory)
90
+ directory ||= 'jsoncache'
91
+ Dir.foreach(cache_dir(directory)) do |filename|
72
92
  next unless filename.include?(key)
73
93
  return filename
74
94
  end
@@ -76,8 +96,9 @@ module JSONCache
76
96
 
77
97
  # Extracts a timestamp from an existing file
78
98
  # @param [String] key The key in which to check for cached data.
79
- def timestamp_from_key(key)
80
- path = filename_from_key(key)
99
+ # @param [String] directory The name of the cache directory.
100
+ def timestamp_from_key(key, directory)
101
+ path = filename_from_key(key, directory)
81
102
  return 0 if path.nil?
82
103
  path.slice(/#{key}.*/)
83
104
  .gsub(/^#{key}/, '')
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.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Stride