Module: Restfulie::Client::HTTP::ResponseCacheCheck
- Included in:
- Net::HTTPResponse
- Defined in:
- lib/restfulie/client/cache/http_ext.rb
Instance Method Summary collapse
- #cache_max_age ⇒ Object
- #has_expired_cache? ⇒ Boolean
- #header_value_from(header, expression) ⇒ Object
-
#may_cache? ⇒ Boolean
checks if the header’s max-age is available and no no-store if available.
-
#may_cache_field?(field) ⇒ Boolean
Returns whether this cache control field allows caching.
-
#value_for(value, expression) ⇒ Object
extracts the header value for an specific expression, which can be located at the start or in the middle of the expression.
-
#vary_headers_for(request) ⇒ Object
extracts all header values related to the Vary header from this response, in order to implement Vary support from the HTTP Specification.
Instance Method Details
#cache_max_age ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/restfulie/client/cache/http_ext.rb', line 45 def cache_max_age val = header_value_from('cache-control', /^\s*max-age=(\d+)/) if val val.to_i else 0 end end |
#has_expired_cache? ⇒ Boolean
60 61 62 63 64 |
# File 'lib/restfulie/client/cache/http_ext.rb', line 60 def has_expired_cache? return true if headers['date'].nil? max_time = Time.rfc2822(headers['date'][0]) + cache_max_age.seconds Time.now > max_time end |
#header_value_from(header, expression) ⇒ Object
54 55 56 57 58 |
# File 'lib/restfulie/client/cache/http_ext.rb', line 54 def header_value_from(header, expression) h = value_for(headers[header], expression) return nil if h.nil? h.match(expression)[1] end |
#may_cache? ⇒ Boolean
checks if the header’s max-age is available and no no-store if available.
67 68 69 |
# File 'lib/restfulie/client/cache/http_ext.rb', line 67 def may_cache? may_cache_method? && may_cache_field?(headers['cache-control']) end |
#may_cache_field?(field) ⇒ Boolean
Returns whether this cache control field allows caching
may_cache_field([‘max-age=2000’, ‘no-store’]) == false may_cache_field(‘max-age=2000,no-store’) == false may_cache_field(‘max-age=2000’) == true
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/restfulie/client/cache/http_ext.rb', line 76 def may_cache_field?(field) return false if field.nil? if field.kind_of? Array field.each do |f| return false if !may_cache_field?(f) end return true end max_age_header = value_for(field, /^max-age=(\d+)/) return false if max_age_header.nil? return !value_for(field, /^no-store/) end |
#value_for(value, expression) ⇒ Object
extracts the header value for an specific expression, which can be located at the start or in the middle of the expression
94 95 96 97 |
# File 'lib/restfulie/client/cache/http_ext.rb', line 94 def value_for(value, expression) value = value[0] if value.kind_of? Array value.split(",").find { |obj| obj.strip =~ expression } end |
#vary_headers_for(request) ⇒ Object
extracts all header values related to the Vary header from this response, in order to implement Vary support from the HTTP Specification
example if the response Vary header is ‘Accept’,‘Accept-Language’, we have vary_headers_for(‘Date’ =>‘…’, ‘Accept-Language’=>‘de’) == [‘application/xml’, ‘de’] vary_headers_for(=> ‘…’, ‘Accept-Language’=>‘de’) == [nil, ‘de’]
106 107 108 109 110 111 |
# File 'lib/restfulie/client/cache/http_ext.rb', line 106 def vary_headers_for(request) return nil if headers['vary'].nil? headers['vary'].split(',').map do |key| request[key.strip] end end |