open-uri-cached 0.0.1 → 0.0.3

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 (2) hide show
  1. data/lib/open-uri/cached.rb +62 -10
  2. metadata +14 -5
@@ -5,14 +5,8 @@ module OpenURI
5
5
  class << self
6
6
  alias original_open_uri open_uri #:nodoc:
7
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)
8
+ response = Cache.get(uri.to_s) ||
9
+ Cache.set(uri.to_s, original_open_uri(uri, *rest))
16
10
 
17
11
  if block_given?
18
12
  begin
@@ -30,16 +24,74 @@ module OpenURI
30
24
  @cache_path = '/tmp/open-uri'
31
25
 
32
26
  class << self
27
+ ##
28
+ # Retrieve file content and meta data from cache
29
+ # @param [String] key
30
+ # @return [StringIO]
33
31
  def get(key)
34
32
  filename = filename_from_url(key)
35
33
  # TODO: head request to determine last_modified vs file modtime
36
- File.exists?(filename) ? File.read(filename) : nil
34
+
35
+ # Read metadata, if it exists
36
+ meta = YAML::load(File.read("#{filename}.meta")) if File.exists?("#{filename}.meta")
37
+
38
+ f = File.exists?(filename) ? StringIO.new(File.read(filename)) : nil
39
+
40
+ # Add meta accessors
41
+ if meta && f
42
+ f.instance_variable_set(:"@meta", meta)
43
+
44
+ def f.meta
45
+ @meta
46
+ end
47
+ def f.base_uri
48
+ @meta[:base_uri]
49
+ end
50
+ def f.content_type
51
+ @meta[:content_type]
52
+ end
53
+ def f.charset
54
+ @meta[:charset]
55
+ end
56
+ def f.content_encoding
57
+ @meta[:content_encoding]
58
+ end
59
+ def f.last_modified
60
+ @meta[:last_modified]
61
+ end
62
+ def f.status
63
+ @meta[:status]
64
+ end
65
+ end
66
+
67
+ f
37
68
  end
38
69
 
70
+ # Cache file content and metadata
71
+ # @param [String] key
72
+ # URL of content to be cached
73
+ # @param [StringIO] value
74
+ # value to be cached, typically StringIO returned from `original_open_uri`
75
+ # @return [StringIO]
76
+ # Returns value
39
77
  def set(key, value)
40
78
  filename = filename_from_url(key)
41
79
  mkpath(filename)
42
- File.open(filename, 'w'){|f| f.write value }
80
+
81
+ # Save metadata in a parallel file
82
+ if value.respond_to?(:meta)
83
+ filename_meta = "#{filename}.meta"
84
+ meta = value.meta
85
+ meta[:status] = value.status if value.respond_to?(:status)
86
+ meta[:content_type] = value.content_type if value.respond_to?(:content_type)
87
+ meta[:base_uri] = value.base_uri if value.respond_to?(:base_uri)
88
+ File.open(filename_meta, 'w') {|f| YAML::dump(meta, f)}
89
+ end
90
+
91
+ # Save file contents
92
+ File.open(filename, 'w'){|f| f.write value.read }
93
+ value.rewind
94
+ value
43
95
  end
44
96
 
45
97
  protected
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open-uri-cached
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 3
9
+ version: 0.0.3
5
10
  platform: ruby
6
11
  authors:
7
12
  - Danial Pearce
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-27 00:00:00 +11:00
17
+ date: 2011-03-22 00:00:00 +11:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -35,21 +40,25 @@ rdoc_options: []
35
40
  require_paths:
36
41
  - lib
37
42
  required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
38
44
  requirements:
39
45
  - - ">="
40
46
  - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
41
49
  version: "0"
42
- version:
43
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
44
52
  requirements:
45
53
  - - ">="
46
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
47
57
  version: "0"
48
- version:
49
58
  requirements: []
50
59
 
51
60
  rubyforge_project:
52
- rubygems_version: 1.3.5
61
+ rubygems_version: 1.3.7
53
62
  signing_key:
54
63
  specification_version: 3
55
64
  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.