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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfe5883d3260aa52a00461c30acdeb2cabb0efb4bfb8bac8a522d5f887264603
4
- data.tar.gz: 170c3bc9a8a737208d95e56e676323bb89544e96d86ba3d4bdf358e004a49c0a
3
+ metadata.gz: ba7f34df29a71904f0339c0495ec3e794fb27057203bbb9957cf52944f0d4394
4
+ data.tar.gz: b15806d79f3ba736aaeab5f335c23ae7f24efdea3902cd955362b9fcc6c0e46e
5
5
  SHA512:
6
- metadata.gz: f03d4cdf614eca182e19571224faf62a7adc4a96b43c57c63ea3bdcadcc5cb7d47ed496488eeb7c2d6149984f30bb119adc9b1c817943128e659f1f003c7c31a
7
- data.tar.gz: be5c985699f051544a8451c41abb8da3f5045d4fa953538098a8e8e1483ba5089158ad104362fa6fabe7097a7937a31a05be572d0bfd37b1cf15e112f6fb57eb
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
- Basic Authentication and Additional Options
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
- For example, to use HTTP basic authentication, use something like this:
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.options[:http_basic_authentication] = ["user", "pass123!"]
160
- response = cache.get 'http://example.com'
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
 
@@ -4,3 +4,9 @@ require 'webcache/polyfills'
4
4
  require 'webcache/cache_operations'
5
5
  require 'webcache/response'
6
6
  require 'webcache/web_cache'
7
+
8
+ if ENV['BYEBUG']
9
+ require 'byebug'
10
+ require 'lp'
11
+ end
12
+
@@ -1,17 +1,17 @@
1
1
  require 'digest/md5'
2
2
  require 'fileutils'
3
- require 'open-uri'
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 options
65
- @options ||= default_open_uri_options
64
+ def auth=(auth)
65
+ convert_auth auth
66
66
  end
67
67
 
68
- private
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 open(url, options)
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
- # Use a less strict URL retrieval:
116
- # 1. Allow http to/from https redirects (through the use of the
117
- # open_uri_redirections gem)
118
- # 2. Disable SSL verification, otherwise, some https sites that show
119
- # properly in the browser, will return an error.
120
- def default_open_uri_options
121
- {
122
- allow_redirections: :all,
123
- ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
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
@@ -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.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
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.6.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-28 00:00:00.000000000 Z
11
+ date: 2019-12-29 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: '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: '0.2'
26
+ version: '4.2'
27
27
  description: Easy to use file cache for web downloads
28
28
  email: db@dannyben.com
29
29
  executables: []