webcache 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -6
- data/lib/webcache.rb +6 -0
- data/lib/webcache/cache_operations.rb +32 -18
- data/lib/webcache/response.rb +19 -8
- data/lib/webcache/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba7f34df29a71904f0339c0495ec3e794fb27057203bbb9957cf52944f0d4394
|
4
|
+
data.tar.gz: b15806d79f3ba736aaeab5f335c23ae7f24efdea3902cd955362b9fcc6c0e46e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 664369168097d544eb74acd295ca76cfc64c122b7d12b04e49f93632ca781210d258badcb4fcd05b79533d4b06dfc0d7143659d05e9e19c5f29886f8be4c4644
|
7
|
+
data.tar.gz: 2d957952ebd6771959432bca59880ed06ca50ee64d7b0f64e8446dbf6c7232e9a43dd55c723373181f8c0269548bff731d095efda8dd60fc41a5f281d2459ebe
|
data/README.md
CHANGED
@@ -147,19 +147,38 @@ cache = WebCache.new
|
|
147
147
|
response = cache.get 'http://example.com', force: true
|
148
148
|
```
|
149
149
|
|
150
|
-
|
150
|
+
Authentication
|
151
151
|
--------------------------------------------------
|
152
|
-
WebCache uses Ruby's [Open URI][1] to download. If you wish to modify
|
153
|
-
the options it uses, simply update the `options` hash.
|
154
152
|
|
155
|
-
|
153
|
+
To configure an authentication header, use the `auth` option. Similarly to
|
154
|
+
the other options, this can be set directly on the static class, on instance
|
155
|
+
initialization, or later on the instance:
|
156
156
|
|
157
157
|
```ruby
|
158
|
+
cache = WebCache.new auth: '...'
|
159
|
+
cache.get 'http://example.com' # authenticated
|
160
|
+
|
158
161
|
cache = WebCache.new
|
159
|
-
cache.
|
160
|
-
|
162
|
+
cache.auth = '...'
|
163
|
+
cache.get 'http://example.com' # authenticated
|
164
|
+
|
165
|
+
WebCache.auth = '...'
|
166
|
+
WebCache.get 'http://example.com' # authenticated
|
167
|
+
```
|
168
|
+
|
169
|
+
For basic authentication, provide a hash:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
cache = WebCache.new auth: { user: 'user', pass: 's3cr3t' }
|
161
173
|
```
|
162
174
|
|
175
|
+
For other authentication headers, simply provide the header string:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
cache = WebCache.new auth: "Bearer t0k3n"
|
179
|
+
```
|
180
|
+
|
181
|
+
|
163
182
|
|
164
183
|
Response Object
|
165
184
|
--------------------------------------------------
|
@@ -177,6 +196,13 @@ the same content.
|
|
177
196
|
|
178
197
|
In case of an error, this contains the error message, `nil` otherwise.
|
179
198
|
|
199
|
+
### `response.code`
|
200
|
+
|
201
|
+
Contains the HTTP code, or `nil` if there was a non-HTTP error.
|
202
|
+
|
203
|
+
### `response.success?`
|
204
|
+
|
205
|
+
A convenience method, returns true if `error` is empty.
|
180
206
|
|
181
207
|
### `response.base_uri`
|
182
208
|
|
data/lib/webcache.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'digest/md5'
|
2
2
|
require 'fileutils'
|
3
|
-
require '
|
4
|
-
require 'open_uri_redirections'
|
3
|
+
require 'http'
|
5
4
|
|
6
5
|
class WebCache
|
7
6
|
module CacheOperations
|
8
|
-
attr_reader :last_error
|
7
|
+
attr_reader :last_error, :user, :pass, :auth
|
9
8
|
attr_writer :dir
|
10
9
|
|
11
|
-
def initialize(dir: 'cache', life: '1h')
|
10
|
+
def initialize(dir: 'cache', life: '1h', auth: nil)
|
12
11
|
@dir = dir
|
13
12
|
@life = life_to_seconds life
|
14
13
|
@enabled = true
|
14
|
+
@auth = convert_auth auth
|
15
15
|
end
|
16
16
|
|
17
17
|
def get(url, force: false)
|
@@ -61,11 +61,11 @@ class WebCache
|
|
61
61
|
FileUtils.rm_rf dir if Dir.exist? dir
|
62
62
|
end
|
63
63
|
|
64
|
-
def
|
65
|
-
|
64
|
+
def auth=(auth)
|
65
|
+
convert_auth auth
|
66
66
|
end
|
67
67
|
|
68
|
-
|
68
|
+
private
|
69
69
|
|
70
70
|
def get!(path, url)
|
71
71
|
return load_file_content path if File.exist? path
|
@@ -90,12 +90,26 @@ class WebCache
|
|
90
90
|
end
|
91
91
|
|
92
92
|
def http_get(url)
|
93
|
-
Response.new
|
93
|
+
Response.new http_response(url)
|
94
94
|
rescue => e
|
95
95
|
url = URI.parse url
|
96
96
|
Response.new error: e.message, base_uri: url, content: e.message
|
97
97
|
end
|
98
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
|
+
|
99
113
|
def stale?(path)
|
100
114
|
life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
|
101
115
|
end
|
@@ -112,16 +126,16 @@ class WebCache
|
|
112
126
|
end
|
113
127
|
end
|
114
128
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
}
|
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
|
125
138
|
end
|
139
|
+
|
126
140
|
end
|
127
141
|
end
|
data/lib/webcache/response.rb
CHANGED
@@ -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.
|
7
|
-
|
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
|
-
|
17
|
+
def success?
|
18
|
+
!error
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
18
22
|
|
19
|
-
def
|
20
|
-
@
|
21
|
-
@
|
22
|
-
|
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
|
data/lib/webcache/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webcache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.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-
|
11
|
+
date: 2019-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: http
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
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: '
|
26
|
+
version: '4.2'
|
27
27
|
description: Easy to use file cache for web downloads
|
28
28
|
email: db@dannyben.com
|
29
29
|
executables: []
|