webcache 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -2
- data/lib/webcache/cache_operations.rb +17 -13
- data/lib/webcache/response.rb +4 -5
- data/lib/webcache/version.rb +2 -2
- data/lib/webcache.rb +0 -2
- metadata +6 -6
- data/lib/webcache/polyfills.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25153a99b8c66cf0ced7058588431c1609861557c8b6078b35c95aa2ee3910bd
|
4
|
+
data.tar.gz: ad2729b1be7a35c41cbe4e5353587b8bf6633b557694b36ac32da2d1de1e481b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 170441dab3662f413d130de99f79d9a778a1feaf85913aaccb0f117275b65540dfaee631c643f11846bae8d776ce8ecf1419863555c9da93075b56aa4224f93b
|
7
|
+
data.tar.gz: a1b2f766439bbf2a2afe5d6818f5a174c7172fa912cba66d4e3114d2f9f0cf6f6782ea95f624343f64274698478366e67d97c05ef9e5b9956cb1c6df50fb4723
|
data/README.md
CHANGED
@@ -59,12 +59,13 @@ puts response.base_uri # => "http://example.com/"
|
|
59
59
|
```
|
60
60
|
|
61
61
|
By default, the cached objects are stored in the `./cache` directory, and
|
62
|
-
expire after 60 minutes. The cache directory will be created as needed
|
62
|
+
expire after 60 minutes. The cache directory will be created as needed, and
|
63
|
+
the permissions of the cached files can be specified if needed.
|
63
64
|
|
64
65
|
You can change these settings on initialization:
|
65
66
|
|
66
67
|
```ruby
|
67
|
-
cache = WebCache.new dir: 'tmp/my_cache', life: '3d'
|
68
|
+
cache = WebCache.new dir: 'tmp/my_cache', life: '3d', permissions: 0o640
|
68
69
|
response = cache.get 'http://example.com'
|
69
70
|
```
|
70
71
|
|
@@ -74,6 +75,7 @@ Or later:
|
|
74
75
|
cache = WebCache.new
|
75
76
|
cache.dir = 'tmp/my_cache'
|
76
77
|
cache.life = '4h'
|
78
|
+
cache.permissions = 0o640
|
77
79
|
response = cache.get 'http://example.com'
|
78
80
|
```
|
79
81
|
|
@@ -4,21 +4,23 @@ require 'http'
|
|
4
4
|
|
5
5
|
class WebCache
|
6
6
|
module CacheOperations
|
7
|
+
attr_accessor :permissions
|
7
8
|
attr_reader :last_error, :user, :pass, :auth
|
8
9
|
attr_writer :dir
|
9
10
|
|
10
|
-
def initialize(dir: 'cache', life: '1h', auth: nil)
|
11
|
+
def initialize(dir: 'cache', life: '1h', auth: nil, permissions: nil)
|
11
12
|
@dir = dir
|
12
13
|
@life = life_to_seconds life
|
13
14
|
@enabled = true
|
14
15
|
@auth = convert_auth auth
|
16
|
+
@permissions = permissions
|
15
17
|
end
|
16
18
|
|
17
19
|
def get(url, force: false)
|
18
20
|
return http_get url unless enabled?
|
19
21
|
|
20
22
|
path = get_path url
|
21
|
-
clear url if force
|
23
|
+
clear url if force || stale?(path)
|
22
24
|
|
23
25
|
get! path, url
|
24
26
|
end
|
@@ -69,6 +71,7 @@ class WebCache
|
|
69
71
|
|
70
72
|
def get!(path, url)
|
71
73
|
return load_file_content path if File.exist? path
|
74
|
+
|
72
75
|
response = http_get url
|
73
76
|
save_file_content path, response unless !response || response.error
|
74
77
|
response
|
@@ -84,8 +87,8 @@ class WebCache
|
|
84
87
|
|
85
88
|
def save_file_content(path, response)
|
86
89
|
FileUtils.mkdir_p dir
|
87
|
-
File.open path, 'wb' do |
|
88
|
-
|
90
|
+
File.open path, 'wb', permissions do |file|
|
91
|
+
file.write Marshal.dump(response)
|
89
92
|
end
|
90
93
|
end
|
91
94
|
|
@@ -111,31 +114,32 @@ class WebCache
|
|
111
114
|
end
|
112
115
|
|
113
116
|
def stale?(path)
|
114
|
-
life
|
117
|
+
life.positive? and File.exist?(path) and Time.new - File.mtime(path) >= life
|
115
118
|
end
|
116
119
|
|
117
120
|
def life_to_seconds(arg)
|
118
121
|
arg = arg.to_s
|
119
122
|
|
120
123
|
case arg[-1]
|
121
|
-
when 's'
|
122
|
-
when 'm'
|
123
|
-
when 'h'
|
124
|
-
when 'd'
|
125
|
-
else;
|
124
|
+
when 's' then arg[0..].to_i
|
125
|
+
when 'm' then arg[0..].to_i * 60
|
126
|
+
when 'h' then arg[0..].to_i * 60 * 60
|
127
|
+
when 'd' then arg[0..].to_i * 60 * 60 * 24
|
128
|
+
else; arg.to_i
|
126
129
|
end
|
127
130
|
end
|
128
131
|
|
129
132
|
def convert_auth(opts)
|
130
|
-
@user
|
133
|
+
@user = nil
|
134
|
+
@pass = nil
|
135
|
+
@auth = nil
|
131
136
|
|
132
|
-
if opts.respond_to?(:has_key?)
|
137
|
+
if opts.respond_to?(:has_key?) && opts.has_key?(:user) && opts.has_key?(:pass)
|
133
138
|
@user = opts[:user]
|
134
139
|
@pass = opts[:pass]
|
135
140
|
else
|
136
141
|
@auth = opts
|
137
142
|
end
|
138
143
|
end
|
139
|
-
|
140
144
|
end
|
141
145
|
end
|
data/lib/webcache/response.rb
CHANGED
@@ -2,11 +2,10 @@ class WebCache
|
|
2
2
|
class Response
|
3
3
|
attr_accessor :error, :base_uri, :content, :code
|
4
4
|
|
5
|
-
def initialize(opts={})
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
init_with_hash opts
|
5
|
+
def initialize(opts = {})
|
6
|
+
case opts
|
7
|
+
when HTTP::Response then init_with_http_response opts
|
8
|
+
when Hash then init_with_hash opts
|
10
9
|
end
|
11
10
|
end
|
12
11
|
|
data/lib/webcache/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
class WebCache
|
2
|
-
VERSION =
|
3
|
-
end
|
2
|
+
VERSION = '0.9.0'
|
3
|
+
end
|
data/lib/webcache.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webcache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -33,14 +33,14 @@ files:
|
|
33
33
|
- README.md
|
34
34
|
- lib/webcache.rb
|
35
35
|
- lib/webcache/cache_operations.rb
|
36
|
-
- lib/webcache/polyfills.rb
|
37
36
|
- lib/webcache/response.rb
|
38
37
|
- lib/webcache/version.rb
|
39
38
|
- lib/webcache/web_cache.rb
|
40
39
|
homepage: https://github.com/DannyBen/webcache
|
41
40
|
licenses:
|
42
41
|
- MIT
|
43
|
-
metadata:
|
42
|
+
metadata:
|
43
|
+
rubygems_mfa_required: 'true'
|
44
44
|
post_install_message:
|
45
45
|
rdoc_options: []
|
46
46
|
require_paths:
|
@@ -49,14 +49,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
49
|
requirements:
|
50
50
|
- - ">="
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version:
|
52
|
+
version: '3.0'
|
53
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
54
|
requirements:
|
55
55
|
- - ">="
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: '0'
|
58
58
|
requirements: []
|
59
|
-
rubygems_version: 3.
|
59
|
+
rubygems_version: 3.3.26
|
60
60
|
signing_key:
|
61
61
|
specification_version: 4
|
62
62
|
summary: Hassle-free caching for HTTP download
|