webcache 0.1.1 → 0.2.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 -17
- data/lib/webcache.rb +1 -0
- data/lib/webcache/response.rb +21 -0
- data/lib/webcache/version.rb +1 -1
- data/lib/webcache/web_cache.rb +10 -15
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c2a4162f4e389f2e179228e16f143ac6c22f7aa
|
4
|
+
data.tar.gz: e3790f1a5d4e219de4b3499e582e527bf4e03af3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90d54a6b3b0005cc06212337fb71ac681924ca0725dedb2ea2b074f88d0884c29ce97428d187b227be5ee582ff4d2cc1966ab5e9e3eac01826ddd38c8177e36a
|
7
|
+
data.tar.gz: 3788c024218d83ae2041fba24c07b76ef95001d6fca166445caa238c488a2233465d4e972abda51829e4687292287481b2e4d1cfba6997f53cf2af875d0b2f16
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ WebCache
|
|
8
8
|
|
9
9
|
---
|
10
10
|
|
11
|
-
|
11
|
+
Hassle-free caching for HTTP download.
|
12
12
|
|
13
13
|
---
|
14
14
|
|
@@ -28,22 +28,27 @@ gem 'webcache'
|
|
28
28
|
Usage
|
29
29
|
--------------------------------------------------
|
30
30
|
|
31
|
-
|
31
|
+
Load a file from cache, or download if needed:
|
32
32
|
|
33
33
|
```ruby
|
34
34
|
require 'webcache'
|
35
35
|
cache = WebCache.new
|
36
|
-
|
36
|
+
response = cache.get 'http://example.com'
|
37
|
+
puts response # => "<html>...</html>"
|
38
|
+
puts response.content # => same as above
|
39
|
+
puts response.to_s # => same as above
|
40
|
+
puts response.error # => nil
|
41
|
+
puts response.base_uri # => "http://example.com/"
|
37
42
|
```
|
38
43
|
|
39
|
-
By default, the cached objects are stored in the `./cache`
|
40
|
-
expire after 60 minutes. The cache
|
44
|
+
By default, the cached objects are stored in the `./cache` directory, and
|
45
|
+
expire after 60 minutes. The cache directory will be created as needed.
|
41
46
|
|
42
47
|
You can change these settings on initialization:
|
43
48
|
|
44
49
|
```ruby
|
45
50
|
cache = WebCache.new 'tmp/my_cache', 7200
|
46
|
-
|
51
|
+
response = cache.get 'http://example.com'
|
47
52
|
```
|
48
53
|
|
49
54
|
Or later:
|
@@ -52,7 +57,7 @@ Or later:
|
|
52
57
|
cache = WebCache.new
|
53
58
|
cache.dir = 'tmp/my_cache'
|
54
59
|
cache.life = 7200 # seconds
|
55
|
-
|
60
|
+
response = cache.get 'http://example.com'
|
56
61
|
```
|
57
62
|
|
58
63
|
To check if a URL is cached, use the `cached?` method:
|
@@ -62,7 +67,7 @@ cache = WebCache.new
|
|
62
67
|
cache.cached? 'http://example.com'
|
63
68
|
# => false
|
64
69
|
|
65
|
-
|
70
|
+
response = cache.get 'http://example.com'
|
66
71
|
cache.cached? 'http://example.com'
|
67
72
|
# => true
|
68
73
|
```
|
@@ -75,31 +80,41 @@ cache.disable
|
|
75
80
|
cache.enabled?
|
76
81
|
# => false
|
77
82
|
|
78
|
-
|
83
|
+
response = cache.get 'http://example.com'
|
79
84
|
cache.cached? 'http://example.com'
|
80
85
|
# => false
|
81
86
|
|
82
87
|
cache.enable
|
83
|
-
|
88
|
+
response = cache.get 'http://example.com'
|
84
89
|
cache.cached? 'http://example.com'
|
85
90
|
# => true
|
86
91
|
```
|
87
92
|
|
88
|
-
|
93
|
+
Response Object
|
89
94
|
--------------------------------------------------
|
90
95
|
|
91
|
-
|
96
|
+
The response object holds these properties:
|
92
97
|
|
93
|
-
|
94
|
-
|
98
|
+
**`response.content`**:
|
99
|
+
Contains the HTML content. In case of an error, this will include the
|
100
|
+
error message. The `#to_s` method of the response object also returns
|
101
|
+
the same content.
|
102
|
+
|
103
|
+
**`response.error`**:
|
104
|
+
In case of an error, this contains the error message, `nil` otherwose.
|
105
|
+
|
106
|
+
**`response.base_uri`**:
|
107
|
+
Contains the actual address of the page. This is useful when the request
|
108
|
+
is redirected. For example, `http://example.com` will set the
|
109
|
+
`base_uri` to `http://example.com/` (note the trailing slash).
|
95
110
|
|
96
|
-
If `last_error` is anything but `false`, it means failure.
|
97
111
|
|
98
112
|
```ruby
|
99
113
|
cache = WebCache.new
|
100
|
-
|
114
|
+
response = cache.get 'http://example.com/not_found'
|
115
|
+
puts response
|
101
116
|
# => '404 Not Found'
|
102
117
|
|
103
|
-
puts
|
118
|
+
puts response.error
|
104
119
|
# => '404 Not Found'
|
105
120
|
```
|
data/lib/webcache.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
class WebCache
|
2
|
+
class Response
|
3
|
+
attr_accessor :error, :base_uri, :content
|
4
|
+
|
5
|
+
def initialize(opts={})
|
6
|
+
if opts.respond_to?(:read) && opts.respond_to?(:base_uri)
|
7
|
+
self.content = opts.read
|
8
|
+
self.base_uri = opts.base_uri
|
9
|
+
self.error = nil
|
10
|
+
elsif opts.is_a? Hash
|
11
|
+
self.error = opts[:error] if opts[:error]
|
12
|
+
self.base_uri = opts[:base_uri] if opts[:base_uri]
|
13
|
+
self.content = opts[:content] if opts[:content]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
content
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/webcache/version.rb
CHANGED
data/lib/webcache/web_cache.rb
CHANGED
@@ -13,21 +13,17 @@ class WebCache
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def get(url)
|
16
|
-
@last_error = false
|
17
16
|
return http_get url unless enabled?
|
18
17
|
|
19
18
|
path = get_path url
|
20
19
|
FileUtils.rm path if old? path
|
21
|
-
return load_file_content path if File.exist? path
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
save_file_content(path, content)
|
28
|
-
end
|
21
|
+
return load_file_content(path) if File.exist? path
|
22
|
+
|
23
|
+
response = http_get(url)
|
24
|
+
save_file_content(path, response) unless !response || response.error
|
29
25
|
|
30
|
-
|
26
|
+
response
|
31
27
|
end
|
32
28
|
|
33
29
|
def cached?(url)
|
@@ -50,7 +46,6 @@ class WebCache
|
|
50
46
|
private
|
51
47
|
|
52
48
|
def get_path(url)
|
53
|
-
@last_url = url
|
54
49
|
File.join dir, Digest::MD5.hexdigest(url)
|
55
50
|
end
|
56
51
|
|
@@ -58,18 +53,18 @@ class WebCache
|
|
58
53
|
Marshal.load File.binread(path)
|
59
54
|
end
|
60
55
|
|
61
|
-
def save_file_content(path,
|
56
|
+
def save_file_content(path, response)
|
62
57
|
FileUtils.mkdir_p dir
|
63
58
|
File.open path, 'wb' do |f|
|
64
|
-
f.write Marshal.dump
|
59
|
+
f.write Marshal.dump response
|
65
60
|
end
|
66
61
|
end
|
67
62
|
|
68
63
|
def http_get(url)
|
69
64
|
begin
|
70
|
-
open(url)
|
71
|
-
rescue
|
72
|
-
|
65
|
+
Response.new open(url)
|
66
|
+
rescue => e
|
67
|
+
Response.new error: e.message, base_uri: url, content: e.message
|
73
68
|
end
|
74
69
|
end
|
75
70
|
|
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.
|
4
|
+
version: 0.2.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: 2016-
|
11
|
+
date: 2016-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: runfile
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.11'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '9.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '9.0'
|
69
83
|
description: Easy to use file cache for web downloads
|
70
84
|
email: db@dannyben.com
|
71
85
|
executables: []
|
@@ -74,6 +88,7 @@ extra_rdoc_files: []
|
|
74
88
|
files:
|
75
89
|
- README.md
|
76
90
|
- lib/webcache.rb
|
91
|
+
- lib/webcache/response.rb
|
77
92
|
- lib/webcache/version.rb
|
78
93
|
- lib/webcache/web_cache.rb
|
79
94
|
homepage: https://github.com/DannyBen/webcache
|
@@ -96,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
111
|
version: '0'
|
97
112
|
requirements: []
|
98
113
|
rubyforge_project:
|
99
|
-
rubygems_version: 2.
|
114
|
+
rubygems_version: 2.5.1
|
100
115
|
signing_key:
|
101
116
|
specification_version: 4
|
102
117
|
summary: Hassle-free caching for HTTP download
|