webcache 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +58 -21
- data/lib/webcache.rb +0 -1
- data/lib/webcache/response.rb +31 -21
- data/lib/webcache/version.rb +1 -1
- data/lib/webcache/web_cache.rb +118 -91
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5cc81de551ef9ce35dfde41ded5128bd8723df5ffa44bc86995e0b786e4b20f8
|
4
|
+
data.tar.gz: 8f8f5a4be13c7e10badcdf64b9ae3b4c519a219a4aa824e103a5b3cf10d4cfd0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5a845a6320ef748177ae9f59592a884b74615c7278e8d70b6a1a312707753a6401fd2029a19670bc288244ede849ddae3b9ede760c6f628001b27d056aff0cc
|
7
|
+
data.tar.gz: d40ef7841aafb3a43cc546c239d00ed641cb5395d27b7bb4fb6295015ba2e50a802aeaf303d0cc28980ca4cf2dbb7e81375b96ce191f637dbe2a18e5c4c3b9f6
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
WebCache
|
2
2
|
==================================================
|
3
3
|
|
4
|
-
[![Gem](https://
|
5
|
-
[![
|
6
|
-
[![
|
7
|
-
[![
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/webcache.svg)](https://badge.fury.io/rb/webcache)
|
5
|
+
[![Build Status](https://travis-ci.com/DannyBen/webcache.svg?branch=master)](https://travis-ci.com/DannyBen/webcache)
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/022f555211d47d655988/maintainability)](https://codeclimate.com/github/DannyBen/webcache/maintainability)
|
7
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/022f555211d47d655988/test_coverage)](https://codeclimate.com/github/DannyBen/webcache/test_coverage)
|
8
8
|
|
9
9
|
---
|
10
10
|
|
@@ -47,7 +47,7 @@ expire after 60 minutes. The cache directory will be created as needed.
|
|
47
47
|
You can change these settings on initialization:
|
48
48
|
|
49
49
|
```ruby
|
50
|
-
cache = WebCache.new 'tmp/my_cache',
|
50
|
+
cache = WebCache.new dir: 'tmp/my_cache', life: '3d'
|
51
51
|
response = cache.get 'http://example.com'
|
52
52
|
```
|
53
53
|
|
@@ -56,11 +56,21 @@ Or later:
|
|
56
56
|
```ruby
|
57
57
|
cache = WebCache.new
|
58
58
|
cache.dir = 'tmp/my_cache'
|
59
|
-
cache.life =
|
59
|
+
cache.life = '4h'
|
60
60
|
response = cache.get 'http://example.com'
|
61
61
|
```
|
62
62
|
|
63
|
-
|
63
|
+
The `life` property accepts any of these formats:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
cache.life = 10 # 10 seconds
|
67
|
+
cache.life = '20s' # 20 seconds
|
68
|
+
cache.life = '10m' # 10 minutes
|
69
|
+
cache.life = '10h' # 10 hours
|
70
|
+
cache.life = '10d' # 10 days
|
71
|
+
```
|
72
|
+
|
73
|
+
Use the `cached?` method to check if a URL is cached:
|
64
74
|
|
65
75
|
```ruby
|
66
76
|
cache = WebCache.new
|
@@ -72,7 +82,7 @@ cache.cached? 'http://example.com'
|
|
72
82
|
# => true
|
73
83
|
```
|
74
84
|
|
75
|
-
|
85
|
+
Use `enable` and `disable` to toggle caching on and off:
|
76
86
|
|
77
87
|
```ruby
|
78
88
|
cache = WebCache.new
|
@@ -90,6 +100,32 @@ cache.cached? 'http://example.com'
|
|
90
100
|
# => true
|
91
101
|
```
|
92
102
|
|
103
|
+
Use `clear url` to remove a cached object if it exists:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
cache = WebCache.new
|
107
|
+
response = cache.get 'http://example.com'
|
108
|
+
cache.cached? 'http://example.com'
|
109
|
+
# => true
|
110
|
+
|
111
|
+
cache.clear 'http://example.com'
|
112
|
+
cache.cached? 'http://example.com'
|
113
|
+
# => false
|
114
|
+
```
|
115
|
+
|
116
|
+
Use `flush` to delete the entire cache directory:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
cache = WebCache.new
|
120
|
+
cache.flush
|
121
|
+
```
|
122
|
+
|
123
|
+
Use `force: true` to force download even if the object is cached:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
cache = WebCache.new
|
127
|
+
response = cache.get 'http://example.com', force: true
|
128
|
+
```
|
93
129
|
|
94
130
|
Basic Authentication and Additional Options
|
95
131
|
--------------------------------------------------
|
@@ -110,29 +146,30 @@ Response Object
|
|
110
146
|
|
111
147
|
The response object holds these properties:
|
112
148
|
|
113
|
-
|
149
|
+
### `response.content`
|
150
|
+
|
114
151
|
Contains the HTML content. In case of an error, this will include the
|
115
152
|
error message. The `#to_s` method of the response object also returns
|
116
153
|
the same content.
|
117
154
|
|
118
|
-
**`response.error`**:
|
119
|
-
In case of an error, this contains the error message, `nil` otherwose.
|
120
155
|
|
121
|
-
|
156
|
+
### `response.error`
|
157
|
+
|
158
|
+
In case of an error, this contains the error message, `nil` otherwise.
|
159
|
+
|
160
|
+
|
161
|
+
### `response.base_uri`
|
162
|
+
|
122
163
|
Contains the actual address of the page. This is useful when the request
|
123
164
|
is redirected. For example, `http://example.com` will set the
|
124
165
|
`base_uri` to `http://example.com/` (note the trailing slash).
|
125
166
|
|
126
167
|
|
127
|
-
|
128
|
-
cache = WebCache.new
|
129
|
-
response = cache.get 'http://example.com/not_found'
|
130
|
-
puts response
|
131
|
-
# => '404 Not Found'
|
168
|
+
---
|
132
169
|
|
133
|
-
|
134
|
-
|
135
|
-
```
|
170
|
+
For a similar gem that provides general purpose caching, see the
|
171
|
+
[Lightly gem][2]
|
136
172
|
|
137
173
|
|
138
|
-
[1]: http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI/OpenRead.html#method-i-open
|
174
|
+
[1]: http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI/OpenRead.html#method-i-open
|
175
|
+
[2]: https://github.com/DannyBen/lightly
|
data/lib/webcache.rb
CHANGED
data/lib/webcache/response.rb
CHANGED
@@ -1,21 +1,31 @@
|
|
1
|
-
class WebCache
|
2
|
-
class Response
|
3
|
-
attr_accessor :error, :base_uri, :content
|
4
|
-
|
5
|
-
def initialize(opts={})
|
6
|
-
if opts.respond_to?(:read) && opts.respond_to?(:base_uri)
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
1
|
+
class WebCache
|
2
|
+
class Response
|
3
|
+
attr_accessor :error, :base_uri, :content
|
4
|
+
|
5
|
+
def initialize(opts={})
|
6
|
+
if opts.respond_to?(:read) && opts.respond_to?(:base_uri)
|
7
|
+
init_with_uri opts
|
8
|
+
elsif opts.is_a? Hash
|
9
|
+
init_with_hash opts
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
content
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def init_with_uri(opts)
|
20
|
+
@content = opts.read
|
21
|
+
@base_uri = opts.base_uri
|
22
|
+
@error = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def init_with_hash(opts)
|
26
|
+
@error = opts[:error]
|
27
|
+
@base_uri = opts[:base_uri]
|
28
|
+
@content = opts[:content]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/webcache/version.rb
CHANGED
data/lib/webcache/web_cache.rb
CHANGED
@@ -1,91 +1,118 @@
|
|
1
|
-
require 'digest/md5'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'open-uri'
|
4
|
-
require 'open_uri_redirections'
|
5
|
-
|
6
|
-
class WebCache
|
7
|
-
attr_reader :last_error
|
8
|
-
attr_accessor :dir
|
9
|
-
|
10
|
-
def initialize(dir
|
11
|
-
@dir = dir
|
12
|
-
@life = life
|
13
|
-
@enabled = true
|
14
|
-
end
|
15
|
-
|
16
|
-
def get(url)
|
17
|
-
return http_get url unless enabled?
|
18
|
-
|
19
|
-
path = get_path url
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
path
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
def
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
def
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
def
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'open_uri_redirections'
|
5
|
+
|
6
|
+
class WebCache
|
7
|
+
attr_reader :last_error, :life
|
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
|
106
|
+
|
107
|
+
# Use a less strict URL retrieval:
|
108
|
+
# 1. Allow http to/from https redirects (through the use of the
|
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
|
+
}
|
117
|
+
end
|
118
|
+
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.4.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: 2018-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: open_uri_redirections
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.10'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.10'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: runfile-tasks
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,28 +72,28 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0.
|
75
|
+
version: '0.15'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0.
|
82
|
+
version: '0.15'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: byebug
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '10.0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '10.0'
|
97
97
|
description: Easy to use file cache for web downloads
|
98
98
|
email: db@dannyben.com
|
99
99
|
executables: []
|
@@ -117,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
117
|
requirements:
|
118
118
|
- - ">="
|
119
119
|
- !ruby/object:Gem::Version
|
120
|
-
version: 2.
|
120
|
+
version: 2.4.0
|
121
121
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
122
|
requirements:
|
123
123
|
- - ">="
|
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
125
|
version: '0'
|
126
126
|
requirements: []
|
127
127
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
128
|
+
rubygems_version: 2.7.6
|
129
129
|
signing_key:
|
130
130
|
specification_version: 4
|
131
131
|
summary: Hassle-free caching for HTTP download
|