webcache 0.8.0 → 0.9.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8aa8d4fedb7deea8b440e70002a01a8d5a7bc70fd6f4ea487cbf8db18fdad54
4
- data.tar.gz: 38a5bcd708f9f09db88b3f4ffa1622ac92e9eeea7be08db57a89d841db446fcf
3
+ metadata.gz: 25153a99b8c66cf0ced7058588431c1609861557c8b6078b35c95aa2ee3910bd
4
+ data.tar.gz: ad2729b1be7a35c41cbe4e5353587b8bf6633b557694b36ac32da2d1de1e481b
5
5
  SHA512:
6
- metadata.gz: b16642f53af90f51903abe71e27b7019a2de8df23e69a3d78b5827a9bb3496c6bc0a9361c197ec2304e4e840d4d26dbad907e6bdadf8c9e501c1a1bd78921e80
7
- data.tar.gz: c8c6300945a4057a8356073c0ccff91e09c69f8ae57279797bead0bc92578f181f475826061fd43c75d8848d0f4240efb5a0d108f34535a9b75283dd44dbb597
6
+ metadata.gz: 170441dab3662f413d130de99f79d9a778a1feaf85913aaccb0f117275b65540dfaee631c643f11846bae8d776ce8ecf1419863555c9da93075b56aa4224f93b
7
+ data.tar.gz: a1b2f766439bbf2a2afe5d6818f5a174c7172fa912cba66d4e3114d2f9f0cf6f6782ea95f624343f64274698478366e67d97c05ef9e5b9956cb1c6df50fb4723
data/README.md CHANGED
@@ -59,12 +59,13 @@ puts response.base_uri # => "http://example.com/"
59
59
  ```
60
60
 
61
61
  By default, the cached objects are stored in the `./cache` directory, and
62
- 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.
63
64
 
64
65
  You can change these settings on initialization:
65
66
 
66
67
  ```ruby
67
- cache = WebCache.new dir: 'tmp/my_cache', life: '3d'
68
+ cache = WebCache.new dir: 'tmp/my_cache', life: '3d', permissions: 0o640
68
69
  response = cache.get 'http://example.com'
69
70
  ```
70
71
 
@@ -74,6 +75,7 @@ Or later:
74
75
  cache = WebCache.new
75
76
  cache.dir = 'tmp/my_cache'
76
77
  cache.life = '4h'
78
+ cache.permissions = 0o640
77
79
  response = cache.get 'http://example.com'
78
80
  ```
79
81
 
@@ -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.8.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.8.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: 2021-05-24 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
@@ -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.5.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.2.16
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