webcache 0.4.0 → 0.8.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: 5cc81de551ef9ce35dfde41ded5128bd8723df5ffa44bc86995e0b786e4b20f8
4
- data.tar.gz: 8f8f5a4be13c7e10badcdf64b9ae3b4c519a219a4aa824e103a5b3cf10d4cfd0
3
+ metadata.gz: e8aa8d4fedb7deea8b440e70002a01a8d5a7bc70fd6f4ea487cbf8db18fdad54
4
+ data.tar.gz: 38a5bcd708f9f09db88b3f4ffa1622ac92e9eeea7be08db57a89d841db446fcf
5
5
  SHA512:
6
- metadata.gz: e5a845a6320ef748177ae9f59592a884b74615c7278e8d70b6a1a312707753a6401fd2029a19670bc288244ede849ddae3b9ede760c6f628001b27d056aff0cc
7
- data.tar.gz: d40ef7841aafb3a43cc546c239d00ed641cb5395d27b7bb4fb6295015ba2e50a802aeaf303d0cc28980ca4cf2dbb7e81375b96ce191f637dbe2a18e5c4c3b9f6
6
+ metadata.gz: b16642f53af90f51903abe71e27b7019a2de8df23e69a3d78b5827a9bb3496c6bc0a9361c197ec2304e4e840d4d26dbad907e6bdadf8c9e501c1a1bd78921e80
7
+ data.tar.gz: c8c6300945a4057a8356073c0ccff91e09c69f8ae57279797bead0bc92578f181f475826061fd43c75d8848d0f4240efb5a0d108f34535a9b75283dd44dbb597
data/README.md CHANGED
@@ -1,10 +1,8 @@
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
- [![Test Coverage](https://api.codeclimate.com/v1/badges/022f555211d47d655988/test_coverage)](https://codeclimate.com/github/DannyBen/webcache/test_coverage)
8
6
 
9
7
  ---
10
8
 
@@ -12,8 +10,7 @@ Hassle-free caching for HTTP download.
12
10
 
13
11
  ---
14
12
 
15
- Install
16
- --------------------------------------------------
13
+ ## Install
17
14
 
18
15
  ```
19
16
  $ gem install webcache
@@ -25,10 +22,30 @@ Or with bundler:
25
22
  gem 'webcache'
26
23
  ```
27
24
 
28
- Usage
29
- --------------------------------------------------
25
+ ## Usage
30
26
 
31
- Load a file from cache, or download if needed:
27
+ WebCache can be used both as an instance, and as a static class.
28
+
29
+ ```ruby
30
+ require 'webcache'
31
+
32
+ # Instance
33
+ cache = WebCache.new life: '3h'
34
+ response = cache.get 'http://example.com'
35
+
36
+ # Static
37
+ WebCache.life = '3h'
38
+ WebCache.get 'http://example.com'
39
+ ```
40
+
41
+ The design intention is to provide both a globally available singleton
42
+ `WebCache` object, as well as multiple caching instances, with different
43
+ settings - depending on the use case.
44
+
45
+ Note that the examples in this README are all using the instance syntax, but
46
+ all methods are also available statically.
47
+
48
+ This is the basic usage pattern:
32
49
 
33
50
  ```ruby
34
51
  require 'webcache'
@@ -127,22 +144,37 @@ cache = WebCache.new
127
144
  response = cache.get 'http://example.com', force: true
128
145
  ```
129
146
 
130
- Basic Authentication and Additional Options
131
- --------------------------------------------------
132
- WebCache uses Ruby's [Open URI][1] to download. If you wish to modify
133
- the options it uses, simply update the `options` hash.
147
+ ## Authentication
134
148
 
135
- For example, to use HTTP basic authentication, use something like this:
149
+ To configure an authentication header, use the `auth` option. Similarly to
150
+ the other options, this can be set directly on the static class, on instance
151
+ initialization, or later on the instance:
136
152
 
137
153
  ```ruby
154
+ cache = WebCache.new auth: '...'
155
+ cache.get 'http://example.com' # authenticated
156
+
138
157
  cache = WebCache.new
139
- cache.options[:http_basic_authentication] = ["user", "pass123!"]
140
- response = cache.get 'http://example.com'
158
+ cache.auth = '...'
159
+ cache.get 'http://example.com' # authenticated
160
+
161
+ WebCache.auth = '...'
162
+ WebCache.get 'http://example.com' # authenticated
141
163
  ```
142
164
 
165
+ For basic authentication, provide a hash:
143
166
 
144
- Response Object
145
- --------------------------------------------------
167
+ ```ruby
168
+ cache = WebCache.new auth: { user: 'user', pass: 's3cr3t' }
169
+ ```
170
+
171
+ For other authentication headers, simply provide the header string:
172
+
173
+ ```ruby
174
+ cache = WebCache.new auth: "Bearer t0k3n"
175
+ ```
176
+
177
+ ## Response Object
146
178
 
147
179
  The response object holds these properties:
148
180
 
@@ -152,11 +184,17 @@ Contains the HTML content. In case of an error, this will include the
152
184
  error message. The `#to_s` method of the response object also returns
153
185
  the same content.
154
186
 
155
-
156
187
  ### `response.error`
157
188
 
158
189
  In case of an error, this contains the error message, `nil` otherwise.
159
190
 
191
+ ### `response.code`
192
+
193
+ Contains the HTTP code, or `nil` if there was a non-HTTP error.
194
+
195
+ ### `response.success?`
196
+
197
+ A convenience method, returns true if `error` is empty.
160
198
 
161
199
  ### `response.base_uri`
162
200
 
@@ -164,12 +202,17 @@ Contains the actual address of the page. This is useful when the request
164
202
  is redirected. For example, `http://example.com` will set the
165
203
  `base_uri` to `http://example.com/` (note the trailing slash).
166
204
 
167
-
168
- ---
205
+ ## Related Projects
169
206
 
170
207
  For a similar gem that provides general purpose caching, see the
171
- [Lightly gem][2]
208
+ [Lightly gem][lightly].
172
209
 
210
+ ## Contributing / Support
211
+
212
+ If you experience any issue, have a question or a suggestion, or if you wish
213
+ to contribute, feel free to [open an issue][issues].
214
+
215
+ ---
173
216
 
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
217
+ [lightly]: https://github.com/DannyBen/lightly
218
+ [issues]: https://github.com/DannyBen/webcache/issues
data/lib/webcache.rb CHANGED
@@ -1,4 +1,12 @@
1
1
  require 'openssl'
2
2
 
3
- require 'webcache/web_cache'
3
+ require 'webcache/polyfills'
4
+ require 'webcache/cache_operations'
4
5
  require 'webcache/response'
6
+ require 'webcache/web_cache'
7
+
8
+ if ENV['BYEBUG']
9
+ require 'byebug'
10
+ require 'lp'
11
+ end
12
+
@@ -0,0 +1,141 @@
1
+ require 'digest/md5'
2
+ require 'fileutils'
3
+ require 'http'
4
+
5
+ class WebCache
6
+ module CacheOperations
7
+ attr_reader :last_error, :user, :pass, :auth
8
+ attr_writer :dir
9
+
10
+ def initialize(dir: 'cache', life: '1h', auth: nil)
11
+ @dir = dir
12
+ @life = life_to_seconds life
13
+ @enabled = true
14
+ @auth = convert_auth auth
15
+ end
16
+
17
+ def get(url, force: false)
18
+ return http_get url unless enabled?
19
+
20
+ path = get_path url
21
+ clear url if force or stale? path
22
+
23
+ get! path, url
24
+ end
25
+
26
+ def life
27
+ @life ||= 3600
28
+ end
29
+
30
+ def life=(new_life)
31
+ @life = life_to_seconds new_life
32
+ end
33
+
34
+ def dir
35
+ @dir ||= 'cache'
36
+ end
37
+
38
+ def cached?(url)
39
+ path = get_path url
40
+ File.exist?(path) and !stale?(path)
41
+ end
42
+
43
+ def enabled?
44
+ @enabled ||= (@enabled.nil? ? true : @enabled)
45
+ end
46
+
47
+ def enable
48
+ @enabled = true
49
+ end
50
+
51
+ def disable
52
+ @enabled = false
53
+ end
54
+
55
+ def clear(url)
56
+ path = get_path url
57
+ FileUtils.rm path if File.exist? path
58
+ end
59
+
60
+ def flush
61
+ FileUtils.rm_rf dir if Dir.exist? dir
62
+ end
63
+
64
+ def auth=(auth)
65
+ convert_auth auth
66
+ end
67
+
68
+ private
69
+
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
76
+
77
+ def get_path(url)
78
+ File.join dir, Digest::MD5.hexdigest(url)
79
+ end
80
+
81
+ def load_file_content(path)
82
+ Marshal.load File.binread(path)
83
+ end
84
+
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
90
+ end
91
+
92
+ def http_get(url)
93
+ Response.new http_response(url)
94
+ rescue => e
95
+ url = URI.parse url
96
+ Response.new error: e.message, base_uri: url, content: e.message
97
+ end
98
+
99
+ def basic_auth?
100
+ !!(user and pass)
101
+ end
102
+
103
+ def http_response(url)
104
+ if basic_auth?
105
+ HTTP.basic_auth(user: user, pass: pass).follow.get url
106
+ elsif auth
107
+ HTTP.auth(auth).follow.get url
108
+ else
109
+ HTTP.follow.get url
110
+ end
111
+ end
112
+
113
+ def stale?(path)
114
+ life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
115
+ end
116
+
117
+ def life_to_seconds(arg)
118
+ arg = arg.to_s
119
+
120
+ 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
126
+ end
127
+ end
128
+
129
+ def convert_auth(opts)
130
+ @user, @pass, @auth = nil, nil, nil
131
+
132
+ if opts.respond_to?(:has_key?) and opts.has_key?(:user) and opts.has_key?(:pass)
133
+ @user = opts[:user]
134
+ @pass = opts[:pass]
135
+ else
136
+ @auth = opts
137
+ end
138
+ end
139
+
140
+ end
141
+ end
@@ -0,0 +1,10 @@
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
@@ -1,10 +1,10 @@
1
1
  class WebCache
2
2
  class Response
3
- attr_accessor :error, :base_uri, :content
3
+ attr_accessor :error, :base_uri, :content, :code
4
4
 
5
5
  def initialize(opts={})
6
- if opts.respond_to?(:read) && opts.respond_to?(:base_uri)
7
- init_with_uri opts
6
+ if opts.is_a? HTTP::Response
7
+ init_with_http_response opts
8
8
  elsif opts.is_a? Hash
9
9
  init_with_hash opts
10
10
  end
@@ -14,18 +14,29 @@ class WebCache
14
14
  content
15
15
  end
16
16
 
17
- private
17
+ def success?
18
+ !error
19
+ end
20
+
21
+ private
18
22
 
19
- def init_with_uri(opts)
20
- @content = opts.read
21
- @base_uri = opts.base_uri
22
- @error = nil
23
+ def init_with_http_response(response)
24
+ @base_uri = response.uri
25
+ @code = response.code
26
+ if response.status.success?
27
+ @content = response.to_s
28
+ @error = nil
29
+ else
30
+ @content = response.status.to_s
31
+ @error = response.status.to_s
32
+ end
23
33
  end
24
34
 
25
35
  def init_with_hash(opts)
26
36
  @error = opts[:error]
27
37
  @base_uri = opts[:base_uri]
28
38
  @content = opts[:content]
39
+ @code = opts[:code]
29
40
  end
30
41
  end
31
42
  end
@@ -1,3 +1,3 @@
1
1
  class WebCache
2
- VERSION = "0.4.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -1,118 +1,7 @@
1
- require 'digest/md5'
2
- require 'fileutils'
3
- require 'open-uri'
4
- require 'open_uri_redirections'
5
-
6
1
  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
2
+ include CacheOperations
106
3
 
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
- }
4
+ class << self
5
+ include CacheOperations
117
6
  end
118
7
  end
metadata CHANGED
@@ -1,99 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webcache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.8.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: 2018-09-05 00:00:00.000000000 Z
11
+ date: 2021-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: open_uri_redirections
14
+ name: http
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.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: '0.2'
27
- - !ruby/object:Gem::Dependency
28
- name: runfile
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '0.10'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0.10'
41
- - !ruby/object:Gem::Dependency
42
- name: runfile-tasks
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0.4'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.4'
55
- - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '3.4'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '3.4'
69
- - !ruby/object:Gem::Dependency
70
- name: simplecov
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '0.15'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '0.15'
83
- - !ruby/object:Gem::Dependency
84
- name: byebug
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '10.0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '10.0'
26
+ version: '5.0'
97
27
  description: Easy to use file cache for web downloads
98
28
  email: db@dannyben.com
99
29
  executables: []
@@ -102,6 +32,8 @@ extra_rdoc_files: []
102
32
  files:
103
33
  - README.md
104
34
  - lib/webcache.rb
35
+ - lib/webcache/cache_operations.rb
36
+ - lib/webcache/polyfills.rb
105
37
  - lib/webcache/response.rb
106
38
  - lib/webcache/version.rb
107
39
  - lib/webcache/web_cache.rb
@@ -117,15 +49,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
49
  requirements:
118
50
  - - ">="
119
51
  - !ruby/object:Gem::Version
120
- version: 2.4.0
52
+ version: 2.5.0
121
53
  required_rubygems_version: !ruby/object:Gem::Requirement
122
54
  requirements:
123
55
  - - ">="
124
56
  - !ruby/object:Gem::Version
125
57
  version: '0'
126
58
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.7.6
59
+ rubygems_version: 3.2.16
129
60
  signing_key:
130
61
  specification_version: 4
131
62
  summary: Hassle-free caching for HTTP download