jsoncache 0.5.0 → 0.5.1

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jsoncache.rb +58 -18
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8b7e2aa8007592f2c122d828b77bf832c495d93
4
- data.tar.gz: e7edbb2504cb92aac6ac04dcf12802c71487f152
3
+ metadata.gz: 50c8df0787884ade8c2d7fa83e76b94301cb28b6
4
+ data.tar.gz: 39eef372973edf214e4f233e27d8eba77fd7cdad
5
5
  SHA512:
6
- metadata.gz: 34ed662640fb70a27ee10b8cd42a2e938cb5a5626e1f4baf97fa9f637aab3b925645850da4a6ee7deb653f9cd9b60a14684644c65ba595c5ae77d67d9e79187d
7
- data.tar.gz: 907ea8767eb4b034b087c9b8de095614c9c35167e103b21ca5c1a49c869d2752e41b62f5e3b4c33ef28c80d775ba2fef70ddf6b510e4e8da5114c9ca939bf9df
6
+ metadata.gz: 3bf6e7d37215465702e47e9346b9c1c063d2316be70ee7adf6e0af1adfd28ed13b7236eea465ddf8b07dc503e64db366c6773abc34504b70f7c48d91e93fce9f
7
+ data.tar.gz: e1c43c2594f07ae5dac8fbbe5b6b357260697441ba4c965ae372c5646067e1af8b7597c308483ed2b96cdaf850e3a58c16c7e18ba7924aa68c211249aa953ee1
data/lib/jsoncache.rb CHANGED
@@ -1,17 +1,39 @@
1
1
  require 'json'
2
2
 
3
3
  # JSONCache is a simple interface to cache JSON based API calls
4
+ #
5
+ # Author:: Derek Stride (mailto:djgstride@gmail.com)
6
+ # License:: MIT
7
+ #
8
+ # This module provides an easy to use class method +cache+ which caches json
9
+ # in a key-value fashion to the filesystem in /tmp/jsoncache.
4
10
  module JSONCache
5
11
  extend self
6
12
 
7
13
  # Retrieves cached data for the specified key and caches the data provided
8
- # if the cache isn't valid.
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.
14
+ # if the cache isn't valid. Specify a code block after the method call that
15
+ # returns a hash and it will be cached.
16
+ #
17
+ # ==== Parameters
18
+ #
19
+ # +key+:: +String+ The key in which to check for cached data.
20
+ # +options+:: +Hash+ A hash of the parameters to use when caching.
21
+ #
22
+ # ==== Options
23
+ #
11
24
  # Accepted options parameters
12
- # => [Boolean] symbolize
13
- # => [String] cache_directory
14
- # => [Fixnum] delta
25
+ #
26
+ # * +:symbolize+:: +Boolean+ Symbolize keys while parsing JSON.
27
+ # * +:cache_directory+:: +String+ The folder name in /tmp to use as the cache.
28
+ # * +:delta+:: +Fixnum+ The validity time of the cache in seconds.
29
+ #
30
+ # ==== Examples
31
+ #
32
+ # def get_response(uri)
33
+ # JSONCache.cache(uri_to_key(uri), delta: 120) do
34
+ # query_some_json_api(uri)
35
+ # end
36
+ # end
15
37
  def cache(key, options = {})
16
38
  return retrieve_cache(key, options) if cached?(key, options)
17
39
  result = yield
@@ -26,8 +48,11 @@ module JSONCache
26
48
  ########################################################################
27
49
 
28
50
  # Determine whether a file is cached and healthy
29
- # @param [String] key The key in which to check for cached data.
30
- # @param [Hash] options A hash of the parameters to use when caching.
51
+ #
52
+ # ==== Parameters
53
+ #
54
+ # +key+:: +String+ The key in which to check for cached data.
55
+ # +options+:: +Hash+ A hash of the parameters to use when caching.
31
56
  def cached?(key, options = {})
32
57
  timestamp = timestamp_from_key(key, options[:cache_directory])
33
58
  delta = options[:delta] || 0
@@ -41,9 +66,12 @@ module JSONCache
41
66
  end
42
67
 
43
68
  # Cache the result from the uri
44
- # @param [String] key The key in which to check for cached data.
45
- # @param [Hash] data The response to cache.
46
- # @param [Hash] options A hash of the parameters to use when caching.
69
+ #
70
+ # ==== Parameters
71
+ #
72
+ # +key+:: +String+ The key in which to check for cached data.
73
+ # +data+:: +Hash+ The data to cache.
74
+ # +options+:: +Hash+ A hash of the parameters to use when caching.
47
75
  def cache_file(key, data, options = {})
48
76
  return unless data.respond_to?(:to_h)
49
77
 
@@ -58,8 +86,11 @@ module JSONCache
58
86
  end
59
87
 
60
88
  # Retrieves a cached value from a key
61
- # @param [String] key The key in which to check for cached data.
62
- # @param [Hash] options A hash of the parameters to use when caching.
89
+ #
90
+ # ==== Parameters
91
+ #
92
+ # +key+:: +String+ The key in which to check for cached data.
93
+ # +options+:: +Hash+ A hash of the parameters to use when caching.
63
94
  def retrieve_cache(key, options = {})
64
95
  directory = options[:cache_directory]
65
96
  filename = filename_from_key(key, directory)
@@ -75,7 +106,10 @@ module JSONCache
75
106
  ########################################################################
76
107
 
77
108
  # Create, if necessary, and return a cache directory
78
- # @param [String] directory The name of the cache directory.
109
+ #
110
+ # ==== Parameters
111
+ #
112
+ # +directory+:: +String+ The name of the cache directory.
79
113
  def cache_dir(directory)
80
114
  directory ||= 'jsoncache'
81
115
  cache_path = File.join('/tmp', directory)
@@ -84,8 +118,11 @@ module JSONCache
84
118
  end
85
119
 
86
120
  # Gets an existing file from a uri if it exists
87
- # @param [String] key The key in which to check for cached data.
88
- # @param [String] directory The name of the cache directory.
121
+ #
122
+ # ==== Parameters
123
+ #
124
+ # +key+:: +String+ The key in which to check for cached data.
125
+ # +directory+:: +String+ The name of the cache directory.
89
126
  def filename_from_key(key, directory)
90
127
  directory ||= 'jsoncache'
91
128
  Dir.foreach(cache_dir(directory)) do |filename|
@@ -95,8 +132,11 @@ module JSONCache
95
132
  end
96
133
 
97
134
  # Extracts a timestamp from an existing file
98
- # @param [String] key The key in which to check for cached data.
99
- # @param [String] directory The name of the cache directory.
135
+ #
136
+ # ==== Parameters
137
+ #
138
+ # +key+:: +String+ The key in which to check for cached data.
139
+ # +directory+:: +String+ The name of the cache directory.
100
140
  def timestamp_from_key(key, directory)
101
141
  path = filename_from_key(key, directory)
102
142
  return 0 if path.nil?
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.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Stride