jsoncache 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/jsoncache.rb +75 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 884fbc12e55809b7a28d9040a5e55446f4e30d08
|
4
|
+
data.tar.gz: f44a53b6f0bad030520990390d4ff4d5558112c7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 630c6d62fcaea703832f6de4b2c6a1e5628b99988f006fc223d35ee9b31ef7c1f9c203b140ebe2eb11639c860921a859bba1dce24fde7fa91e37fa62c841416a
|
7
|
+
data.tar.gz: 29da69e11bab483818baccf20171f0e77915cdb8b17b14fff90274d00d2d8066f3004d85214b6da06fc37a6f1b4d7f04c4a9dc3d431511f0fc609cf67f090413
|
data/lib/jsoncache.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
# JSONCache is a simple interface to cache JSON based API calls
|
4
|
+
module JSONCache
|
5
|
+
attr_accessor :cache_directory
|
6
|
+
|
7
|
+
# 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)
|
13
|
+
if timestamp.zero?
|
14
|
+
false
|
15
|
+
elsif !delta.zero?
|
16
|
+
(Time.now.to_i - timestamp) < delta
|
17
|
+
else
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# 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)
|
26
|
+
cache_path = cache_dir
|
27
|
+
existing_file = filename_from_uri(uri)
|
28
|
+
last_path = "#{cache_path}/#{existing_file}"
|
29
|
+
File.delete(last_path) if existing_file && File.exist?(last_path)
|
30
|
+
File.write(
|
31
|
+
"#{cache_path}/#{uri_to_file_path_root(uri)}#{Time.now.to_i}.json",
|
32
|
+
JSON.generate(response))
|
33
|
+
end
|
34
|
+
|
35
|
+
def retrieve_cache(uri, params = {})
|
36
|
+
JSON.parse(
|
37
|
+
File.read("#{cache_dir}/#{filename_from_uri(uri)}"),
|
38
|
+
params)
|
39
|
+
end
|
40
|
+
|
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
|
+
# 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)
|
59
|
+
Dir.foreach(cache_dir) do |filename|
|
60
|
+
next unless filename.include?(root_path)
|
61
|
+
return filename
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Extracts a timestamp from an existing file
|
66
|
+
def timestamp_from_uri(uri)
|
67
|
+
path = filename_from_uri(uri)
|
68
|
+
return 0 if path.nil?
|
69
|
+
last_pattern = uri_to_file_path_root(uri)
|
70
|
+
path.slice(/#{last_pattern}.*/)
|
71
|
+
.gsub(/^#{last_pattern}/, '')
|
72
|
+
.chomp('.json')
|
73
|
+
.to_i
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsoncache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derek Stride
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple JSON cache for caching data intended for usewith HTTP APIs.
|
14
|
+
email: djgstride@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/jsoncache.rb
|
20
|
+
homepage: http://rubygems.org/gems/jsoncache
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.5.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Simple JSON Caching
|
44
|
+
test_files: []
|