Module: Net::HTTP::NotModifiedCache

Defined in:
lib/net-http-not_modified_cache.rb

Defined Under Namespace

Classes: Entry

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.rootObject



82
83
84
# File 'lib/net-http-not_modified_cache.rb', line 82

def root
  @root ||= '/tmp/net-http-not_modified_cache'
end

.storeObject



86
87
88
# File 'lib/net-http-not_modified_cache.rb', line 86

def store
  @store ||= ActiveSupport::Cache.lookup_store(:file_store, root, :compress => true)
end

Class Method Details

.disable!Object



63
64
65
# File 'lib/net-http-not_modified_cache.rb', line 63

def disable!
  @enabled = false
end

.enable!Object



67
68
69
# File 'lib/net-http-not_modified_cache.rb', line 67

def enable!
  @enabled = true
end

.enabled?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/net-http-not_modified_cache.rb', line 71

def enabled?
  @enabled
end

.included(base) ⇒ Object



75
76
77
78
79
80
# File 'lib/net-http-not_modified_cache.rb', line 75

def included(base)
  base.class_eval do
    alias_method :request_without_not_modified_cache, :request
    alias_method :request, :request_with_not_modified_cache
  end
end

.versionObject



90
91
92
# File 'lib/net-http-not_modified_cache.rb', line 90

def version
  @version ||= '0.0.0'
end

.while_disabled(&block) ⇒ Object



94
95
96
# File 'lib/net-http-not_modified_cache.rb', line 94

def while_disabled(&block)
  while_enabled_is(false, &block)
end

.while_enabled(&block) ⇒ Object



98
99
100
# File 'lib/net-http-not_modified_cache.rb', line 98

def while_enabled(&block)
  while_enabled_is(true, &block)
end

.while_enabled_is(boolean) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/net-http-not_modified_cache.rb', line 102

def while_enabled_is(boolean)
  old_enabled = @enabled
  @enabled = boolean
  yield
ensure
  @enabled = old_enabled
end

.with_store(store) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/net-http-not_modified_cache.rb', line 110

def with_store(store)
  old_store = self.store
  self.store = store
  yield
ensure
  self.store = old_store
end

Instance Method Details

#cache_entry(response) ⇒ Object



9
10
11
12
# File 'lib/net-http-not_modified_cache.rb', line 9

def cache_entry(response)
  last_modified_at = Time.parse(response['last-modified'] || response['date']) rescue Time.now
  Entry.new(response.body, response['etag'], last_modified_at)
end

#cache_key(request) ⇒ Object



14
15
16
# File 'lib/net-http-not_modified_cache.rb', line 14

def cache_key(request)
  [address, request.path].join
end

#cache_request(request) ⇒ Object



18
19
20
# File 'lib/net-http-not_modified_cache.rb', line 18

def cache_request(request)
  cache_request!(request) if cacheable_request?(request)
end

#cache_request!(request) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/net-http-not_modified_cache.rb', line 22

def cache_request!(request)
  key = cache_key(request)
  unless request['if-modified-since'] || request['if-none-match']
    if entry = NotModifiedCache.store.read(key)
      request['if-modified-since'] = entry.last_modified_at.httpdate
      request['if-none-match'] = entry.etag
    end
  end
  key
end

#cache_response(response, key) ⇒ Object



37
38
39
# File 'lib/net-http-not_modified_cache.rb', line 37

def cache_response(response, key)
  cache_response!(response, key) if cacheable_response?(response)
end

#cache_response!(response, key) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/net-http-not_modified_cache.rb', line 41

def cache_response!(response, key)
  if response.code == '200'
    NotModifiedCache.store.write(key, cache_entry(response))
  elsif response.code == '304' && entry = NotModifiedCache.store.read(key)
    response.instance_variable_set('@body', entry.body)
  end
end

#cacheable_request?(request) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/net-http-not_modified_cache.rb', line 33

def cacheable_request?(request)
  NotModifiedCache.enabled? && request.is_a?(Get)
end

#cacheable_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/net-http-not_modified_cache.rb', line 49

def cacheable_response?(response)
  NotModifiedCache.enabled? && %w(200 304).include?(response.code)
end

#request_with_not_modified_cache(request, body = nil, &block) ⇒ Object



53
54
55
56
57
58
# File 'lib/net-http-not_modified_cache.rb', line 53

def request_with_not_modified_cache(request, body = nil, &block)
  key = cache_request(request)
  response = request_without_not_modified_cache(request, body, &block)
  cache_response(response, key) if key
  response
end