webcache 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +86 -0
- data/lib/webcache.rb +2 -0
- data/lib/webcache/version.rb +3 -0
- data/lib/webcache/web_cache.rb +68 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 443cde9b95f84eb6cff86c4fccd0335aa17139c3
|
4
|
+
data.tar.gz: fbb2acb31a86388987d29facad46a5f73440aae5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08febe9b278725d875b25bc68e72a7e48fb5a9b872429f0bf53bdd366c0b50d3451d367f7138028d6a29c5a741211bd6a1446d857f97a184ddc7d5e5eeee864b
|
7
|
+
data.tar.gz: 9957b6418433717b67180b1b2bd8ca5a6da110172ee3000d5639f83f817a80c5815f36d5addf38457038dfc90240b980bf558cf4e0a025635c94c37c4106e625
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
WebCache - Download web pages with a simple cache
|
2
|
+
==================================================
|
3
|
+
|
4
|
+
[![Gem](https://img.shields.io/gem/v/webcache.svg?style=flat-square)](https://rubygems.org/gems/webcache)
|
5
|
+
[![Travis](https://img.shields.io/travis/DannyBen/webcache.svg?style=flat-square)](https://travis-ci.org/DannyBen/webcache)
|
6
|
+
[![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/webcache.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/webcache)
|
7
|
+
[![Gemnasium](https://img.shields.io/gemnasium/DannyBen/webcache.svg?style=flat-square)](https://gemnasium.com/DannyBen/webcache)
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
WebCache provides a hassle-free caching for HTTP download.
|
12
|
+
|
13
|
+
---
|
14
|
+
|
15
|
+
Install
|
16
|
+
--------------------------------------------------
|
17
|
+
|
18
|
+
```
|
19
|
+
$ gem install webcache
|
20
|
+
```
|
21
|
+
|
22
|
+
Or with bundler:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'webcache'
|
26
|
+
```
|
27
|
+
|
28
|
+
Usage
|
29
|
+
--------------------------------------------------
|
30
|
+
|
31
|
+
Use `WebCache.get url` to load from cache, or download:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
cache = WebCache.new
|
35
|
+
content = cache.get 'http://example.com'
|
36
|
+
```
|
37
|
+
|
38
|
+
By default, the cached objects are stored in the `./cache` folder, and
|
39
|
+
expire after 60 minutes. The cache folder will be created as needed.
|
40
|
+
|
41
|
+
You can change these settings on initialization:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
cache = WebCache.new 'tmp/my_cache', 120
|
45
|
+
content = cache.get 'http://example.com'
|
46
|
+
```
|
47
|
+
|
48
|
+
Or later:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
cache = WebCache.new
|
52
|
+
cache.dir = 'tmp/my_cache'
|
53
|
+
cache.life = 120
|
54
|
+
content = cache.get 'http://example.com'
|
55
|
+
```
|
56
|
+
|
57
|
+
To check if a URL is cached, use the 'cached?' method:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
cache = WebCache.new
|
61
|
+
cache.cached? 'http://example.com'
|
62
|
+
# => false
|
63
|
+
|
64
|
+
content = cache.get 'http://example.com'
|
65
|
+
cache.cached? 'http://example.com'
|
66
|
+
# => true
|
67
|
+
```
|
68
|
+
|
69
|
+
You can enable/disable the cache at any time:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
cache = WebCache.new
|
73
|
+
cache.disable
|
74
|
+
content = cache.get 'http://example.com'
|
75
|
+
cache.enabled?
|
76
|
+
# => false
|
77
|
+
|
78
|
+
cache.cached? 'http://example.com'
|
79
|
+
# => false
|
80
|
+
|
81
|
+
cache.enable
|
82
|
+
content = cache.get 'http://example.com'
|
83
|
+
cache.cached? 'http://example.com'
|
84
|
+
# => true
|
85
|
+
```
|
86
|
+
|
data/lib/webcache.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
class WebCache
|
6
|
+
attr_accessor :dir, :life
|
7
|
+
|
8
|
+
def initialize(dir='cache', life=60)
|
9
|
+
@dir = dir
|
10
|
+
@life = life
|
11
|
+
@enabled = true
|
12
|
+
FileUtils.mkdir_p dir
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(url)
|
16
|
+
return http_get url unless enabled?
|
17
|
+
|
18
|
+
path = get_path url
|
19
|
+
FileUtils.rm path if old? path
|
20
|
+
return load_file_content path if File.exist? path
|
21
|
+
|
22
|
+
content = http_get(url)
|
23
|
+
save_file_content(path, content)
|
24
|
+
content
|
25
|
+
end
|
26
|
+
|
27
|
+
def cached?(url)
|
28
|
+
path = get_path url
|
29
|
+
File.exist?(path) and !old?(path)
|
30
|
+
end
|
31
|
+
|
32
|
+
def enabled?
|
33
|
+
@enabled
|
34
|
+
end
|
35
|
+
|
36
|
+
def enable
|
37
|
+
@enabled = true
|
38
|
+
end
|
39
|
+
|
40
|
+
def disable
|
41
|
+
@enabled = false
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def get_path(url)
|
47
|
+
@last_url = url
|
48
|
+
File.join dir, Digest::MD5.hexdigest(url)
|
49
|
+
end
|
50
|
+
|
51
|
+
def load_file_content(path)
|
52
|
+
Marshal.load File.binread(path)
|
53
|
+
end
|
54
|
+
|
55
|
+
def save_file_content(path, content)
|
56
|
+
File.open path, 'wb' do |f|
|
57
|
+
f.write Marshal.dump content
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def http_get(url)
|
62
|
+
open(url).read
|
63
|
+
end
|
64
|
+
|
65
|
+
def old?(path)
|
66
|
+
life > 0 and File.exists?(path) and Time.new - File.mtime(path) >= life
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webcache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Danny Ben Shitrit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: runfile
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: runfile-tasks
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.4'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.11'
|
69
|
+
description: Easy to use file cache for web downloads
|
70
|
+
email: db@dannyben.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- README.md
|
76
|
+
- lib/webcache.rb
|
77
|
+
- lib/webcache/version.rb
|
78
|
+
- lib/webcache/web_cache.rb
|
79
|
+
homepage: https://github.com/DannyBen/webcache
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 2.0.0
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.4.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Download web pages with caching
|
103
|
+
test_files: []
|