webcache 0.4.0 → 0.5.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 +22 -1
- data/lib/webcache.rb +2 -1
- data/lib/webcache/cache_operations.rb +126 -0
- data/lib/webcache/response.rb +1 -1
- data/lib/webcache/version.rb +1 -1
- data/lib/webcache/web_cache.rb +3 -114
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a57464d31079c5df7d8e63d027b769b9f54a06f5c0e3d66afbe8b80a1ce184e
|
4
|
+
data.tar.gz: bc33a0a17e492d88639ed595c4e1459bfa27b9ceba68b6b1edb92d216f53357a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4bb3e90d6a456249061cfd399757bffa412e6edc8127a9ca5087c7b25725910a7a028fae43ebf7a9d2e3482df2cee144aae97da0bb8d3d37331341b7c210faf
|
7
|
+
data.tar.gz: 58283031d3550aa0f8bc4ca8073a7c104dc33b901e054165fcc570368da0158197addf8f4b1ebd378d7898b860c4d1d5169e81f515e1e0d2a9c9ef91354e014e
|
data/README.md
CHANGED
@@ -28,7 +28,28 @@ gem 'webcache'
|
|
28
28
|
Usage
|
29
29
|
--------------------------------------------------
|
30
30
|
|
31
|
-
|
31
|
+
WebCache can be used both as an instance, and as a static class.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'webcache'
|
35
|
+
|
36
|
+
# Instance
|
37
|
+
cache = WebCache.new life: '3h'
|
38
|
+
response = cache.get 'http://example.com'
|
39
|
+
|
40
|
+
# Static
|
41
|
+
WebCache.life = '3h'
|
42
|
+
WebCache.get 'http://example.com'
|
43
|
+
```
|
44
|
+
|
45
|
+
The design intention is to provide both a globally available singleton
|
46
|
+
`WebCache` object, as well as multiple caching instances, with different
|
47
|
+
settings - depending on the use case.
|
48
|
+
|
49
|
+
Note that the examples in this README are all using the instance syntax, but
|
50
|
+
all methods are also available statically.
|
51
|
+
|
52
|
+
This is the basic usage pattern:
|
32
53
|
|
33
54
|
```ruby
|
34
55
|
require 'webcache'
|
data/lib/webcache.rb
CHANGED
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'open_uri_redirections'
|
5
|
+
|
6
|
+
module CacheOperations
|
7
|
+
attr_reader :last_error
|
8
|
+
attr_writer :dir
|
9
|
+
|
10
|
+
def initialize(dir: 'cache', life: '1h')
|
11
|
+
@dir = dir
|
12
|
+
@life = life_to_seconds life
|
13
|
+
@enabled = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(url, force: false)
|
17
|
+
return http_get url unless enabled?
|
18
|
+
|
19
|
+
path = get_path url
|
20
|
+
clear url if force or stale? path
|
21
|
+
|
22
|
+
get! path, url
|
23
|
+
end
|
24
|
+
|
25
|
+
def life=(new_life)
|
26
|
+
@life = life_to_seconds new_life
|
27
|
+
end
|
28
|
+
|
29
|
+
def dir
|
30
|
+
@dir ||= 'cache'
|
31
|
+
end
|
32
|
+
|
33
|
+
def life
|
34
|
+
@life ||= 3600
|
35
|
+
end
|
36
|
+
|
37
|
+
def cached?(url)
|
38
|
+
path = get_path url
|
39
|
+
File.exist?(path) and !stale?(path)
|
40
|
+
end
|
41
|
+
|
42
|
+
def enabled?
|
43
|
+
@enabled ||= (@enabled.nil? ? true : @enabled)
|
44
|
+
end
|
45
|
+
|
46
|
+
def enable
|
47
|
+
@enabled = true
|
48
|
+
end
|
49
|
+
|
50
|
+
def disable
|
51
|
+
@enabled = false
|
52
|
+
end
|
53
|
+
|
54
|
+
def clear(url)
|
55
|
+
path = get_path url
|
56
|
+
FileUtils.rm path if File.exist? path
|
57
|
+
end
|
58
|
+
|
59
|
+
def flush
|
60
|
+
FileUtils.rm_rf dir if Dir.exist? dir
|
61
|
+
end
|
62
|
+
|
63
|
+
def options
|
64
|
+
@options ||= default_open_uri_options
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def get!(path, url)
|
70
|
+
return load_file_content path if File.exist? path
|
71
|
+
response = http_get url
|
72
|
+
save_file_content path, response unless !response || response.error
|
73
|
+
response
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_path(url)
|
77
|
+
File.join dir, Digest::MD5.hexdigest(url)
|
78
|
+
end
|
79
|
+
|
80
|
+
def load_file_content(path)
|
81
|
+
Marshal.load File.binread(path)
|
82
|
+
end
|
83
|
+
|
84
|
+
def save_file_content(path, response)
|
85
|
+
FileUtils.mkdir_p dir
|
86
|
+
File.open path, 'wb' do |f|
|
87
|
+
f.write Marshal.dump response
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def http_get(url)
|
92
|
+
begin
|
93
|
+
Response.new open(url, options)
|
94
|
+
rescue => e
|
95
|
+
Response.new error: e.message, base_uri: url, content: e.message
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def stale?(path)
|
100
|
+
life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
|
101
|
+
end
|
102
|
+
|
103
|
+
def life_to_seconds(arg)
|
104
|
+
arg = arg.to_s
|
105
|
+
|
106
|
+
case arg[-1]
|
107
|
+
when 's'; arg[0..-1].to_i
|
108
|
+
when 'm'; arg[0..-1].to_i * 60
|
109
|
+
when 'h'; arg[0..-1].to_i * 60 * 60
|
110
|
+
when 'd'; arg[0..-1].to_i * 60 * 60 * 24
|
111
|
+
else; arg.to_i
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Use a less strict URL retrieval:
|
116
|
+
# 1. Allow http to/from https redirects (through the use of the
|
117
|
+
# open_uri_redirections gem)
|
118
|
+
# 2. Disable SSL verification, otherwise, some https sites that show
|
119
|
+
# properly in the browser, will return an error.
|
120
|
+
def default_open_uri_options
|
121
|
+
{
|
122
|
+
allow_redirections: :all,
|
123
|
+
ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
|
124
|
+
}
|
125
|
+
end
|
126
|
+
end
|
data/lib/webcache/response.rb
CHANGED
data/lib/webcache/version.rb
CHANGED
data/lib/webcache/web_cache.rb
CHANGED
@@ -1,118 +1,7 @@
|
|
1
|
-
require 'digest/md5'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'open-uri'
|
4
|
-
require 'open_uri_redirections'
|
5
|
-
|
6
1
|
class WebCache
|
7
|
-
|
8
|
-
attr_accessor :dir
|
9
|
-
|
10
|
-
def initialize(dir: 'cache', life: '1h')
|
11
|
-
@dir = dir
|
12
|
-
@life = life_to_seconds life
|
13
|
-
@enabled = true
|
14
|
-
end
|
15
|
-
|
16
|
-
def get(url, force: false)
|
17
|
-
return http_get url unless enabled?
|
18
|
-
|
19
|
-
path = get_path url
|
20
|
-
clear url if force or stale? path
|
21
|
-
|
22
|
-
get! path, url
|
23
|
-
end
|
24
|
-
|
25
|
-
def life=(new_life)
|
26
|
-
@life = life_to_seconds new_life
|
27
|
-
end
|
28
|
-
|
29
|
-
def cached?(url)
|
30
|
-
path = get_path url
|
31
|
-
File.exist?(path) and !stale?(path)
|
32
|
-
end
|
33
|
-
|
34
|
-
def enabled?
|
35
|
-
@enabled
|
36
|
-
end
|
37
|
-
|
38
|
-
def enable
|
39
|
-
@enabled = true
|
40
|
-
end
|
41
|
-
|
42
|
-
def disable
|
43
|
-
@enabled = false
|
44
|
-
end
|
45
|
-
|
46
|
-
def clear(url)
|
47
|
-
path = get_path url
|
48
|
-
FileUtils.rm path if File.exist? path
|
49
|
-
end
|
50
|
-
|
51
|
-
def flush
|
52
|
-
FileUtils.rm_rf dir if Dir.exist? dir
|
53
|
-
end
|
54
|
-
|
55
|
-
def options
|
56
|
-
@options ||= default_open_uri_options
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def get!(path, url)
|
62
|
-
return load_file_content path if File.exist? path
|
63
|
-
response = http_get url
|
64
|
-
save_file_content path, response unless !response || response.error
|
65
|
-
response
|
66
|
-
end
|
67
|
-
|
68
|
-
def get_path(url)
|
69
|
-
File.join dir, Digest::MD5.hexdigest(url)
|
70
|
-
end
|
71
|
-
|
72
|
-
def load_file_content(path)
|
73
|
-
Marshal.load File.binread(path)
|
74
|
-
end
|
75
|
-
|
76
|
-
def save_file_content(path, response)
|
77
|
-
FileUtils.mkdir_p dir
|
78
|
-
File.open path, 'wb' do |f|
|
79
|
-
f.write Marshal.dump response
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def http_get(url)
|
84
|
-
begin
|
85
|
-
Response.new open(url, options)
|
86
|
-
rescue => e
|
87
|
-
Response.new error: e.message, base_uri: url, content: e.message
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def stale?(path)
|
92
|
-
life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
|
93
|
-
end
|
94
|
-
|
95
|
-
def life_to_seconds(arg)
|
96
|
-
arg = arg.to_s
|
97
|
-
|
98
|
-
case arg[-1]
|
99
|
-
when 's'; arg[0..-1].to_i
|
100
|
-
when 'm'; arg[0..-1].to_i * 60
|
101
|
-
when 'h'; arg[0..-1].to_i * 60 * 60
|
102
|
-
when 'd'; arg[0..-1].to_i * 60 * 60 * 24
|
103
|
-
else; arg.to_i
|
104
|
-
end
|
105
|
-
end
|
2
|
+
include CacheOperations
|
106
3
|
|
107
|
-
|
108
|
-
|
109
|
-
# open_uri_redirections gem)
|
110
|
-
# 2. Disable SSL verification, otherwise, some https sites that show
|
111
|
-
# properly in the browser, will return an error.
|
112
|
-
def default_open_uri_options
|
113
|
-
{
|
114
|
-
allow_redirections: :all,
|
115
|
-
ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
|
116
|
-
}
|
4
|
+
class << self
|
5
|
+
include CacheOperations
|
117
6
|
end
|
118
7
|
end
|
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.5.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: 2018-09-
|
11
|
+
date: 2018-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: open_uri_redirections
|
@@ -102,6 +102,7 @@ extra_rdoc_files: []
|
|
102
102
|
files:
|
103
103
|
- README.md
|
104
104
|
- lib/webcache.rb
|
105
|
+
- lib/webcache/cache_operations.rb
|
105
106
|
- lib/webcache/response.rb
|
106
107
|
- lib/webcache/version.rb
|
107
108
|
- lib/webcache/web_cache.rb
|