webcache 0.7.0 → 0.9.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
2
  SHA256:
3
- metadata.gz: ba7f34df29a71904f0339c0495ec3e794fb27057203bbb9957cf52944f0d4394
4
- data.tar.gz: b15806d79f3ba736aaeab5f335c23ae7f24efdea3902cd955362b9fcc6c0e46e
3
+ metadata.gz: 25153a99b8c66cf0ced7058588431c1609861557c8b6078b35c95aa2ee3910bd
4
+ data.tar.gz: ad2729b1be7a35c41cbe4e5353587b8bf6633b557694b36ac32da2d1de1e481b
5
5
  SHA512:
6
- metadata.gz: 664369168097d544eb74acd295ca76cfc64c122b7d12b04e49f93632ca781210d258badcb4fcd05b79533d4b06dfc0d7143659d05e9e19c5f29886f8be4c4644
7
- data.tar.gz: 2d957952ebd6771959432bca59880ed06ca50ee64d7b0f64e8446dbf6c7232e9a43dd55c723373181f8c0269548bff731d095efda8dd60fc41a5f281d2459ebe
6
+ metadata.gz: 170441dab3662f413d130de99f79d9a778a1feaf85913aaccb0f117275b65540dfaee631c643f11846bae8d776ce8ecf1419863555c9da93075b56aa4224f93b
7
+ data.tar.gz: a1b2f766439bbf2a2afe5d6818f5a174c7172fa912cba66d4e3114d2f9f0cf6f6782ea95f624343f64274698478366e67d97c05ef9e5b9956cb1c6df50fb4723
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
- WebCache
2
- ==================================================
1
+ # WebCache
3
2
 
4
3
  [![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)
4
+ [![Build Status](https://github.com/DannyBen/webcache/workflows/Test/badge.svg)](https://github.com/DannyBen/webcache/actions?query=workflow%3ATest)
6
5
  [![Maintainability](https://api.codeclimate.com/v1/badges/022f555211d47d655988/maintainability)](https://codeclimate.com/github/DannyBen/webcache/maintainability)
7
6
 
8
7
  ---
@@ -11,8 +10,7 @@ Hassle-free caching for HTTP download.
11
10
 
12
11
  ---
13
12
 
14
- Install
15
- --------------------------------------------------
13
+ ## Install
16
14
 
17
15
  ```
18
16
  $ gem install webcache
@@ -24,8 +22,7 @@ Or with bundler:
24
22
  gem 'webcache'
25
23
  ```
26
24
 
27
- Usage
28
- --------------------------------------------------
25
+ ## Usage
29
26
 
30
27
  WebCache can be used both as an instance, and as a static class.
31
28
 
@@ -62,12 +59,13 @@ puts response.base_uri # => "http://example.com/"
62
59
  ```
63
60
 
64
61
  By default, the cached objects are stored in the `./cache` directory, and
65
- 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.
66
64
 
67
65
  You can change these settings on initialization:
68
66
 
69
67
  ```ruby
70
- cache = WebCache.new dir: 'tmp/my_cache', life: '3d'
68
+ cache = WebCache.new dir: 'tmp/my_cache', life: '3d', permissions: 0o640
71
69
  response = cache.get 'http://example.com'
72
70
  ```
73
71
 
@@ -77,6 +75,7 @@ Or later:
77
75
  cache = WebCache.new
78
76
  cache.dir = 'tmp/my_cache'
79
77
  cache.life = '4h'
78
+ cache.permissions = 0o640
80
79
  response = cache.get 'http://example.com'
81
80
  ```
82
81
 
@@ -147,8 +146,7 @@ cache = WebCache.new
147
146
  response = cache.get 'http://example.com', force: true
148
147
  ```
149
148
 
150
- Authentication
151
- --------------------------------------------------
149
+ ## Authentication
152
150
 
153
151
  To configure an authentication header, use the `auth` option. Similarly to
154
152
  the other options, this can be set directly on the static class, on instance
@@ -178,10 +176,7 @@ For other authentication headers, simply provide the header string:
178
176
  cache = WebCache.new auth: "Bearer t0k3n"
179
177
  ```
180
178
 
181
-
182
-
183
- Response Object
184
- --------------------------------------------------
179
+ ## Response Object
185
180
 
186
181
  The response object holds these properties:
187
182
 
@@ -191,7 +186,6 @@ Contains the HTML content. In case of an error, this will include the
191
186
  error message. The `#to_s` method of the response object also returns
192
187
  the same content.
193
188
 
194
-
195
189
  ### `response.error`
196
190
 
197
191
  In case of an error, this contains the error message, `nil` otherwise.
@@ -210,12 +204,17 @@ Contains the actual address of the page. This is useful when the request
210
204
  is redirected. For example, `http://example.com` will set the
211
205
  `base_uri` to `http://example.com/` (note the trailing slash).
212
206
 
213
-
214
- ---
207
+ ## Related Projects
215
208
 
216
209
  For a similar gem that provides general purpose caching, see the
217
- [Lightly gem][2]
210
+ [Lightly gem][lightly].
211
+
212
+ ## Contributing / Support
218
213
 
214
+ If you experience any issue, have a question or a suggestion, or if you wish
215
+ to contribute, feel free to [open an issue][issues].
216
+
217
+ ---
219
218
 
220
- [1]: http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI/OpenRead.html#method-i-open
221
- [2]: https://github.com/DannyBen/lightly
219
+ [lightly]: https://github.com/DannyBen/lightly
220
+ [issues]: https://github.com/DannyBen/webcache/issues
@@ -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 or stale? path
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 |f|
88
- f.write Marshal.dump response
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 > 0 and File.exist?(path) and Time.new - File.mtime(path) >= 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'; arg[0..-1].to_i
122
- when 'm'; arg[0..-1].to_i * 60
123
- when 'h'; arg[0..-1].to_i * 60 * 60
124
- when 'd'; arg[0..-1].to_i * 60 * 60 * 24
125
- else; arg.to_i
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, @pass, @auth = nil, nil, nil
133
+ @user = nil
134
+ @pass = nil
135
+ @auth = nil
131
136
 
132
- if opts.respond_to?(:has_key?) and opts.has_key?(:user) and opts.has_key?(:pass)
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
@@ -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
- if opts.is_a? HTTP::Response
7
- init_with_http_response opts
8
- elsif opts.is_a? Hash
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
 
@@ -1,3 +1,3 @@
1
1
  class WebCache
2
- VERSION = "0.7.0"
3
- end
2
+ VERSION = '0.9.0'
3
+ end
data/lib/webcache.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'openssl'
2
2
 
3
- require 'webcache/polyfills'
4
3
  require 'webcache/cache_operations'
5
4
  require 'webcache/response'
6
5
  require 'webcache/web_cache'
@@ -9,4 +8,3 @@ if ENV['BYEBUG']
9
8
  require 'byebug'
10
9
  require 'lp'
11
10
  end
12
-
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.7.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: 2019-12-29 00:00:00.000000000 Z
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.2'
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '4.2'
26
+ version: '5.0'
27
27
  description: Easy to use file cache for web downloads
28
28
  email: db@dannyben.com
29
29
  executables: []
@@ -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: 2.3.0
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.0.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
@@ -1,10 +0,0 @@
1
- # :nocov:
2
-
3
- # Required for Ruby < 2.4
4
- if !Dir.respond_to? :empty?
5
- class Dir
6
- def self.empty?(path_name)
7
- exist?(path_name) && (entries(path_name) - ['.', '..']).empty?
8
- end
9
- end
10
- end