webcache 0.1.0 → 0.1.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
  SHA1:
3
- metadata.gz: 443cde9b95f84eb6cff86c4fccd0335aa17139c3
4
- data.tar.gz: fbb2acb31a86388987d29facad46a5f73440aae5
3
+ metadata.gz: 31ef4fc734aa42ccb8ed629de15a5bf7d48fd2ea
4
+ data.tar.gz: 2e44e49b6e8c66a26c83fb2fcb2ca1cadab7a6c1
5
5
  SHA512:
6
- metadata.gz: 08febe9b278725d875b25bc68e72a7e48fb5a9b872429f0bf53bdd366c0b50d3451d367f7138028d6a29c5a741211bd6a1446d857f97a184ddc7d5e5eeee864b
7
- data.tar.gz: 9957b6418433717b67180b1b2bd8ca5a6da110172ee3000d5639f83f817a80c5815f36d5addf38457038dfc90240b980bf558cf4e0a025635c94c37c4106e625
6
+ metadata.gz: 5336d34a8cafb3c8311209b4e50cb7191a8caa3da4fe999a4bf6036a7abed161e5d3b54c3e3cfbe64a7e81efd476a2016ba8a08f8eb0ca19795ef2463e789b69
7
+ data.tar.gz: 4ffdc6145037c4459e49cab3ca28bdc2f2f3915a6f15480e1c6ea44363dde1540043e7655e5653dfb2dba90b789e33bed3e2d28dc5567217db61b1ea889b43a4
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- WebCache - Download web pages with a simple cache
1
+ WebCache
2
2
  ==================================================
3
3
 
4
4
  [![Gem](https://img.shields.io/gem/v/webcache.svg?style=flat-square)](https://rubygems.org/gems/webcache)
@@ -31,6 +31,7 @@ Usage
31
31
  Use `WebCache.get url` to load from cache, or download:
32
32
 
33
33
  ```ruby
34
+ require 'webcache'
34
35
  cache = WebCache.new
35
36
  content = cache.get 'http://example.com'
36
37
  ```
@@ -41,7 +42,7 @@ expire after 60 minutes. The cache folder will be created as needed.
41
42
  You can change these settings on initialization:
42
43
 
43
44
  ```ruby
44
- cache = WebCache.new 'tmp/my_cache', 120
45
+ cache = WebCache.new 'tmp/my_cache', 7200
45
46
  content = cache.get 'http://example.com'
46
47
  ```
47
48
 
@@ -50,11 +51,11 @@ Or later:
50
51
  ```ruby
51
52
  cache = WebCache.new
52
53
  cache.dir = 'tmp/my_cache'
53
- cache.life = 120
54
+ cache.life = 7200 # seconds
54
55
  content = cache.get 'http://example.com'
55
56
  ```
56
57
 
57
- To check if a URL is cached, use the 'cached?' method:
58
+ To check if a URL is cached, use the `cached?` method:
58
59
 
59
60
  ```ruby
60
61
  cache = WebCache.new
@@ -71,10 +72,10 @@ You can enable/disable the cache at any time:
71
72
  ```ruby
72
73
  cache = WebCache.new
73
74
  cache.disable
74
- content = cache.get 'http://example.com'
75
75
  cache.enabled?
76
76
  # => false
77
77
 
78
+ content = cache.get 'http://example.com'
78
79
  cache.cached? 'http://example.com'
79
80
  # => false
80
81
 
@@ -84,3 +85,21 @@ cache.cached? 'http://example.com'
84
85
  # => true
85
86
  ```
86
87
 
88
+ Error Handling
89
+ --------------------------------------------------
90
+
91
+ Whenever a request results in any HTTP error, two things will happen:
92
+
93
+ 1. The return value will be set to the error message
94
+ 2. The `last_error` variable will be set to the error message
95
+
96
+ If `last_error` is anything but `false`, it means failure.
97
+
98
+ ```ruby
99
+ cache = WebCache.new
100
+ puts cache.get 'http://example.com/not_found'
101
+ # => '404 Not Found'
102
+
103
+ puts cache.last_error
104
+ # => '404 Not Found'
105
+ ```
@@ -1,3 +1,3 @@
1
1
  class WebCache
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -3,16 +3,17 @@ require 'fileutils'
3
3
  require 'open-uri'
4
4
 
5
5
  class WebCache
6
+ attr_reader :last_error
6
7
  attr_accessor :dir, :life
7
8
 
8
- def initialize(dir='cache', life=60)
9
+ def initialize(dir='cache', life=3600)
9
10
  @dir = dir
10
11
  @life = life
11
12
  @enabled = true
12
- FileUtils.mkdir_p dir
13
13
  end
14
14
 
15
15
  def get(url)
16
+ @last_error = false
16
17
  return http_get url unless enabled?
17
18
 
18
19
  path = get_path url
@@ -20,7 +21,12 @@ class WebCache
20
21
  return load_file_content path if File.exist? path
21
22
 
22
23
  content = http_get(url)
23
- save_file_content(path, content)
24
+ if @last_error
25
+ content = @last_error
26
+ else
27
+ save_file_content(path, content)
28
+ end
29
+
24
30
  content
25
31
  end
26
32
 
@@ -53,16 +59,21 @@ class WebCache
53
59
  end
54
60
 
55
61
  def save_file_content(path, content)
62
+ FileUtils.mkdir_p dir
56
63
  File.open path, 'wb' do |f|
57
64
  f.write Marshal.dump content
58
65
  end
59
66
  end
60
67
 
61
68
  def http_get(url)
62
- open(url).read
69
+ begin
70
+ open(url).read
71
+ rescue OpenURI::HTTPError => e
72
+ @last_error = e.message
73
+ end
63
74
  end
64
75
 
65
76
  def old?(path)
66
- life > 0 and File.exists?(path) and Time.new - File.mtime(path) >= life
77
+ life > 0 and File.exist?(path) and Time.new - File.mtime(path) >= life
67
78
  end
68
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webcache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
@@ -99,5 +99,5 @@ rubyforge_project:
99
99
  rubygems_version: 2.4.6
100
100
  signing_key:
101
101
  specification_version: 4
102
- summary: Download web pages with caching
102
+ summary: Hassle-free caching for HTTP download
103
103
  test_files: []