webcache 0.5.0 → 0.5.1

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: 9a57464d31079c5df7d8e63d027b769b9f54a06f5c0e3d66afbe8b80a1ce184e
4
- data.tar.gz: bc33a0a17e492d88639ed595c4e1459bfa27b9ceba68b6b1edb92d216f53357a
3
+ metadata.gz: 451f1821f816e79684155b09d796979d3de00cef291b53820aa60900e58e7c01
4
+ data.tar.gz: 17b8951b923e7da3b27c5d9c4f168feb3ba9c662d8a5bcfab2f847f3c317a012
5
5
  SHA512:
6
- metadata.gz: a4bb3e90d6a456249061cfd399757bffa412e6edc8127a9ca5087c7b25725910a7a028fae43ebf7a9d2e3482df2cee144aae97da0bb8d3d37331341b7c210faf
7
- data.tar.gz: 58283031d3550aa0f8bc4ca8073a7c104dc33b901e054165fcc570368da0158197addf8f4b1ebd378d7898b860c4d1d5169e81f515e1e0d2a9c9ef91354e014e
6
+ metadata.gz: 3ab3b897c607b12e869610004a2e2f8fa905b93120052d9479d426d6c1bf969da59e248387ed9aac252a4498ef64793701d63be223af15314872a8e3bd099fd8
7
+ data.tar.gz: d7bdbb693e586e26e3b8b92f75d8d7ddcf93c0ce26ad7d8d5fbf4c0a4dd267e71165a8130b247a0650484b71eeb14447f493f143080348afa874d2e609ab92be
@@ -3,124 +3,126 @@ require 'fileutils'
3
3
  require 'open-uri'
4
4
  require 'open_uri_redirections'
5
5
 
6
- module CacheOperations
7
- attr_reader :last_error
8
- attr_writer :dir
9
-
10
- def initialize(dir: 'cache', life: '1h')
11
- @dir = dir
12
- @life = life_to_seconds life
13
- @enabled = true
14
- end
6
+ class WebCache
7
+ module CacheOperations
8
+ attr_reader :last_error
9
+ attr_writer :dir
10
+
11
+ def initialize(dir: 'cache', life: '1h')
12
+ @dir = dir
13
+ @life = life_to_seconds life
14
+ @enabled = true
15
+ end
15
16
 
16
- def get(url, force: false)
17
- return http_get url unless enabled?
17
+ def get(url, force: false)
18
+ return http_get url unless enabled?
18
19
 
19
- path = get_path url
20
- clear url if force or stale? path
20
+ path = get_path url
21
+ clear url if force or stale? path
21
22
 
22
- get! path, url
23
- end
23
+ get! path, url
24
+ end
24
25
 
25
- def life=(new_life)
26
- @life = life_to_seconds new_life
27
- end
26
+ def life
27
+ @life ||= 3600
28
+ end
28
29
 
29
- def dir
30
- @dir ||= 'cache'
31
- end
30
+ def life=(new_life)
31
+ @life = life_to_seconds new_life
32
+ end
32
33
 
33
- def life
34
- @life ||= 3600
35
- end
34
+ def dir
35
+ @dir ||= 'cache'
36
+ end
36
37
 
37
- def cached?(url)
38
- path = get_path url
39
- File.exist?(path) and !stale?(path)
40
- end
38
+ def cached?(url)
39
+ path = get_path url
40
+ File.exist?(path) and !stale?(path)
41
+ end
41
42
 
42
- def enabled?
43
- @enabled ||= (@enabled.nil? ? true : @enabled)
44
- end
43
+ def enabled?
44
+ @enabled ||= (@enabled.nil? ? true : @enabled)
45
+ end
45
46
 
46
- def enable
47
- @enabled = true
48
- end
47
+ def enable
48
+ @enabled = true
49
+ end
49
50
 
50
- def disable
51
- @enabled = false
52
- end
51
+ def disable
52
+ @enabled = false
53
+ end
53
54
 
54
- def clear(url)
55
- path = get_path url
56
- FileUtils.rm path if File.exist? path
57
- end
55
+ def clear(url)
56
+ path = get_path url
57
+ FileUtils.rm path if File.exist? path
58
+ end
58
59
 
59
- def flush
60
- FileUtils.rm_rf dir if Dir.exist? dir
61
- end
60
+ def flush
61
+ FileUtils.rm_rf dir if Dir.exist? dir
62
+ end
62
63
 
63
- def options
64
- @options ||= default_open_uri_options
65
- end
64
+ def options
65
+ @options ||= default_open_uri_options
66
+ end
66
67
 
67
- private
68
+ private
68
69
 
69
- def get!(path, url)
70
- return load_file_content path if File.exist? path
71
- response = http_get url
72
- save_file_content path, response unless !response || response.error
73
- response
74
- end
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
75
76
 
76
- def get_path(url)
77
- File.join dir, Digest::MD5.hexdigest(url)
78
- end
77
+ def get_path(url)
78
+ File.join dir, Digest::MD5.hexdigest(url)
79
+ end
79
80
 
80
- def load_file_content(path)
81
- Marshal.load File.binread(path)
82
- end
81
+ def load_file_content(path)
82
+ Marshal.load File.binread(path)
83
+ end
83
84
 
84
- def save_file_content(path, response)
85
- FileUtils.mkdir_p dir
86
- File.open path, 'wb' do |f|
87
- f.write Marshal.dump response
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
88
90
  end
89
- end
90
91
 
91
- def http_get(url)
92
- begin
93
- Response.new open(url, options)
94
- rescue => e
95
- Response.new error: e.message, base_uri: url, content: e.message
92
+ def http_get(url)
93
+ begin
94
+ Response.new open(url, options)
95
+ rescue => e
96
+ Response.new error: e.message, base_uri: url, content: e.message
97
+ end
96
98
  end
97
- end
98
99
 
99
- def stale?(path)
100
- life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
101
- end
100
+ def stale?(path)
101
+ life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
102
+ end
102
103
 
103
- def life_to_seconds(arg)
104
- arg = arg.to_s
104
+ def life_to_seconds(arg)
105
+ arg = arg.to_s
105
106
 
106
- case arg[-1]
107
- when 's'; arg[0..-1].to_i
108
- when 'm'; arg[0..-1].to_i * 60
109
- when 'h'; arg[0..-1].to_i * 60 * 60
110
- when 'd'; arg[0..-1].to_i * 60 * 60 * 24
111
- else; arg.to_i
107
+ case arg[-1]
108
+ when 's'; arg[0..-1].to_i
109
+ when 'm'; arg[0..-1].to_i * 60
110
+ when 'h'; arg[0..-1].to_i * 60 * 60
111
+ when 'd'; arg[0..-1].to_i * 60 * 60 * 24
112
+ else; arg.to_i
113
+ end
112
114
  end
113
- end
114
115
 
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
- }
116
+ # Use a less strict URL retrieval:
117
+ # 1. Allow http to/from https redirects (through the use of the
118
+ # open_uri_redirections gem)
119
+ # 2. Disable SSL verification, otherwise, some https sites that show
120
+ # properly in the browser, will return an error.
121
+ def default_open_uri_options
122
+ {
123
+ allow_redirections: :all,
124
+ ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
125
+ }
126
+ end
125
127
  end
126
128
  end
@@ -1,4 +1,4 @@
1
- module CacheOperations
1
+ class WebCache
2
2
  class Response
3
3
  attr_accessor :error, :base_uri, :content
4
4
 
@@ -1,3 +1,3 @@
1
1
  class WebCache
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
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.5.0
4
+ version: 0.5.1
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-06 00:00:00.000000000 Z
11
+ date: 2018-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: open_uri_redirections