open-uri-cached 0.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of open-uri-cached might be problematic. Click here for more details.

Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.markdown +14 -0
  3. data/lib/open-uri/cached.rb +63 -0
  4. metadata +57 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Danial Pearce
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ # OpenURI with caching
2
+
3
+ Carelessly make OpenURI requests without getting hate mail.
4
+
5
+ ## Usage
6
+
7
+ Require the library
8
+
9
+ require 'open-uri/cached'
10
+ open('http://www.someone-that-hates-being-scraped.com').read
11
+
12
+ ## Configuring
13
+
14
+ OpenURI::Cache.cache_path = '/tmp/open-uri'
@@ -0,0 +1,63 @@
1
+ require 'open-uri'
2
+ require 'digest/sha1'
3
+
4
+ module OpenURI
5
+ class << self
6
+ alias original_open_uri open_uri #:nodoc:
7
+ def open_uri(uri, *rest, &block)
8
+ response = Cache.get(uri.to_s)
9
+
10
+ unless response
11
+ response = original_open_uri(uri, *rest).read
12
+ Cache.set(uri.to_s, response)
13
+ end
14
+
15
+ response = StringIO.new(response)
16
+
17
+ if block_given?
18
+ begin
19
+ yield response
20
+ ensure
21
+ response.close
22
+ end
23
+ else
24
+ response
25
+ end
26
+ end
27
+ end
28
+
29
+ class Cache
30
+ @cache_path = '/tmp/open-uri'
31
+
32
+ class << self
33
+ def get(key)
34
+ filename = filename_from_url(key)
35
+ # TODO: head request to determine last_modified vs file modtime
36
+ File.exists?(filename) ? File.read(filename) : nil
37
+ end
38
+
39
+ def set(key, value)
40
+ filename = filename_from_url(key)
41
+ mkpath(filename)
42
+ File.open(filename, 'w'){|f| f.write value }
43
+ end
44
+
45
+ protected
46
+ def filename_from_url(url)
47
+ uri = URI.parse(url) # TODO: rescue here?
48
+ [ @cache_path, uri.host, Digest::SHA1.hexdigest(url) ].join('/')
49
+ end
50
+
51
+ def mkpath(path)
52
+ full = []
53
+ dirs = path.split('/'); dirs.pop
54
+ dirs.each do |dir|
55
+ full.push(dir)
56
+ dir = full.join('/')
57
+ next if dir.to_s == ''
58
+ Dir.mkdir(dir) unless File.exists?(dir)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open-uri-cached
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Danial Pearce
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-27 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: OpenURI with transparent disk caching
17
+ email: rubygems@tigris.id.au
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.markdown
26
+ - LICENSE
27
+ - lib/open-uri/cached.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/tigris/open-uri-cached
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Do a lot of site scraping but take lots of attempts at parsing the content before reaching your end result? This gem is for you. But wait, there's more... Ok, no there isn't.
56
+ test_files: []
57
+