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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9edc3145e92b9f4a08aff555ba0b420d9346386a
4
- data.tar.gz: 9f654e4874b7e55ee1ea0a0311c55db83872cc60
2
+ SHA256:
3
+ metadata.gz: 5cc81de551ef9ce35dfde41ded5128bd8723df5ffa44bc86995e0b786e4b20f8
4
+ data.tar.gz: 8f8f5a4be13c7e10badcdf64b9ae3b4c519a219a4aa824e103a5b3cf10d4cfd0
5
5
  SHA512:
6
- metadata.gz: 0271f6a7a1e75b84754d8a7e0877243cc407f7374ce7ece6a634ae5d56e1b6ee965b695262109051d1aa72c6bf7ee24dc396049a0cec3600d88b88122487fdc9
7
- data.tar.gz: 4a19051e54fd769e19c5dafac9956a9166def2b2fddc4b0d1fb3d7307e9d6ddb8a0d62f082dc01ed08dd135d4f1e68a1b02a93611897387593314b169c2ddbd9
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://img.shields.io/gem/v/webcache.svg?style=flat-square)](https://rubygems.org/gems/webcache)
5
- [![Travis](https://img.shields.io/travis/DannyBen/webcache.svg?style=flat-square)](https://travis-ci.org/DannyBen/webcache)
6
- [![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/webcache.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/webcache)
7
- [![Gemnasium](https://img.shields.io/gemnasium/DannyBen/webcache.svg?style=flat-square)](https://gemnasium.com/DannyBen/webcache)
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', 7200
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 = 7200 # seconds
59
+ cache.life = '4h'
60
60
  response = cache.get 'http://example.com'
61
61
  ```
62
62
 
63
- To check if a URL is cached, use the `cached?` method:
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
- You can enable/disable the cache at any time:
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
- **`response.content`**:
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
- **`response.base_uri`**:
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
- ```ruby
128
- cache = WebCache.new
129
- response = cache.get 'http://example.com/not_found'
130
- puts response
131
- # => '404 Not Found'
168
+ ---
132
169
 
133
- puts response.error
134
- # => '404 Not Found'
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
@@ -1,5 +1,4 @@
1
1
  require 'openssl'
2
2
 
3
- require 'webcache/version'
4
3
  require 'webcache/web_cache'
5
4
  require 'webcache/response'
@@ -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
- self.content = opts.read
8
- self.base_uri = opts.base_uri
9
- self.error = nil
10
- elsif opts.is_a? Hash
11
- self.error = opts[:error] if opts[:error]
12
- self.base_uri = opts[:base_uri] if opts[:base_uri]
13
- self.content = opts[:content] if opts[:content]
14
- end
15
- end
16
-
17
- def to_s
18
- content
19
- end
20
- end
21
- end
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
@@ -1,3 +1,3 @@
1
1
  class WebCache
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -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, :life
9
-
10
- def initialize(dir='cache', life=3600)
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
- FileUtils.rm path if old? path
21
-
22
- return load_file_content(path) if File.exist? path
23
-
24
- response = http_get(url)
25
- save_file_content(path, response) unless !response || response.error
26
-
27
- response
28
- end
29
-
30
- def cached?(url)
31
- path = get_path url
32
- File.exist?(path) and !old?(path)
33
- end
34
-
35
- def enabled?
36
- @enabled
37
- end
38
-
39
- def enable
40
- @enabled = true
41
- end
42
-
43
- def disable
44
- @enabled = false
45
- end
46
-
47
- def options
48
- @options ||= default_open_uri_options
49
- end
50
-
51
- private
52
-
53
- def get_path(url)
54
- File.join dir, Digest::MD5.hexdigest(url)
55
- end
56
-
57
- def load_file_content(path)
58
- Marshal.load File.binread(path)
59
- end
60
-
61
- def save_file_content(path, response)
62
- FileUtils.mkdir_p dir
63
- File.open path, 'wb' do |f|
64
- f.write Marshal.dump response
65
- end
66
- end
67
-
68
- def http_get(url)
69
- begin
70
- Response.new open(url, options)
71
- rescue => e
72
- Response.new error: e.message, base_uri: url, content: e.message
73
- end
74
- end
75
-
76
- def old?(path)
77
- life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
78
- end
79
-
80
- # Use a less strict URL retrieval:
81
- # 1. Allow http to/from https redirects (through the use of the
82
- # open_uri_redirections gem)
83
- # 2. Disable SSL verification, otherwise, some https sites that show
84
- # properly in the browser, will return an error.
85
- def default_open_uri_options
86
- {
87
- allow_redirections: :all,
88
- ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
89
- }
90
- end
91
- end
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.3.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: 2017-01-29 00:00:00.000000000 Z
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.7'
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.7'
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.11'
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.11'
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: '9.0'
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: '9.0'
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.0.0
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.6.6
128
+ rubygems_version: 2.7.6
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: Hassle-free caching for HTTP download