webcache 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.
- checksums.yaml +4 -4
- data/lib/webcache/cache_operations.rb +95 -93
- data/lib/webcache/response.rb +1 -1
- data/lib/webcache/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 451f1821f816e79684155b09d796979d3de00cef291b53820aa60900e58e7c01
|
4
|
+
data.tar.gz: 17b8951b923e7da3b27c5d9c4f168feb3ba9c662d8a5bcfab2f847f3c317a012
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ab3b897c607b12e869610004a2e2f8fa905b93120052d9479d426d6c1bf969da59e248387ed9aac252a4498ef64793701d63be223af15314872a8e3bd099fd8
|
7
|
+
data.tar.gz: d7bdbb693e586e26e3b8b92f75d8d7ddcf93c0ce26ad7d8d5fbf4c0a4dd267e71165a8130b247a0650484b71eeb14447f493f143080348afa874d2e609ab92be
|
@@ -3,124 +3,126 @@ require 'fileutils'
|
|
3
3
|
require 'open-uri'
|
4
4
|
require 'open_uri_redirections'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
class WebCache
|
7
|
+
module CacheOperations
|
8
|
+
attr_reader :last_error
|
9
|
+
attr_writer :dir
|
10
|
+
|
11
|
+
def initialize(dir: 'cache', life: '1h')
|
12
|
+
@dir = dir
|
13
|
+
@life = life_to_seconds life
|
14
|
+
@enabled = true
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
def get(url, force: false)
|
18
|
+
return http_get url unless enabled?
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
path = get_path url
|
21
|
+
clear url if force or stale? path
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
get! path, url
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
def life
|
27
|
+
@life ||= 3600
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def life=(new_life)
|
31
|
+
@life = life_to_seconds new_life
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
def dir
|
35
|
+
@dir ||= 'cache'
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
def cached?(url)
|
39
|
+
path = get_path url
|
40
|
+
File.exist?(path) and !stale?(path)
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
def enabled?
|
44
|
+
@enabled ||= (@enabled.nil? ? true : @enabled)
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
def enable
|
48
|
+
@enabled = true
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
def disable
|
52
|
+
@enabled = false
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
def clear(url)
|
56
|
+
path = get_path url
|
57
|
+
FileUtils.rm path if File.exist? path
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
def flush
|
61
|
+
FileUtils.rm_rf dir if Dir.exist? dir
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
def options
|
65
|
+
@options ||= default_open_uri_options
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
+
private
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
70
|
+
def get!(path, url)
|
71
|
+
return load_file_content path if File.exist? path
|
72
|
+
response = http_get url
|
73
|
+
save_file_content path, response unless !response || response.error
|
74
|
+
response
|
75
|
+
end
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
77
|
+
def get_path(url)
|
78
|
+
File.join dir, Digest::MD5.hexdigest(url)
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
81
|
+
def load_file_content(path)
|
82
|
+
Marshal.load File.binread(path)
|
83
|
+
end
|
83
84
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
85
|
+
def save_file_content(path, response)
|
86
|
+
FileUtils.mkdir_p dir
|
87
|
+
File.open path, 'wb' do |f|
|
88
|
+
f.write Marshal.dump response
|
89
|
+
end
|
88
90
|
end
|
89
|
-
end
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
def http_get(url)
|
93
|
+
begin
|
94
|
+
Response.new open(url, options)
|
95
|
+
rescue => e
|
96
|
+
Response.new error: e.message, base_uri: url, content: e.message
|
97
|
+
end
|
96
98
|
end
|
97
|
-
end
|
98
99
|
|
99
|
-
|
100
|
-
|
101
|
-
|
100
|
+
def stale?(path)
|
101
|
+
life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
|
102
|
+
end
|
102
103
|
|
103
|
-
|
104
|
-
|
104
|
+
def life_to_seconds(arg)
|
105
|
+
arg = arg.to_s
|
105
106
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
107
|
+
case arg[-1]
|
108
|
+
when 's'; arg[0..-1].to_i
|
109
|
+
when 'm'; arg[0..-1].to_i * 60
|
110
|
+
when 'h'; arg[0..-1].to_i * 60 * 60
|
111
|
+
when 'd'; arg[0..-1].to_i * 60 * 60 * 24
|
112
|
+
else; arg.to_i
|
113
|
+
end
|
112
114
|
end
|
113
|
-
end
|
114
115
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
116
|
+
# Use a less strict URL retrieval:
|
117
|
+
# 1. Allow http to/from https redirects (through the use of the
|
118
|
+
# open_uri_redirections gem)
|
119
|
+
# 2. Disable SSL verification, otherwise, some https sites that show
|
120
|
+
# properly in the browser, will return an error.
|
121
|
+
def default_open_uri_options
|
122
|
+
{
|
123
|
+
allow_redirections: :all,
|
124
|
+
ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
|
125
|
+
}
|
126
|
+
end
|
125
127
|
end
|
126
128
|
end
|
data/lib/webcache/response.rb
CHANGED
data/lib/webcache/version.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.5.
|
4
|
+
version: 0.5.1
|
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-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: open_uri_redirections
|