ministryofjustice/Claim-for-Crown-Court-Defence

View on GitHub
lib/caching/response.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
class Caching
  class Response
    attr_accessor :response

    def initialize(response)
      self.response = response
      validate!
    end

    delegate :body, to: :response

    delegate :headers, to: :response

    def ttl
      return 0 if cache_control.match?('no-cache')
      cache_control[/max-age=([0-9]+)/, 1]
    end

    private

    def cache_control
      headers[:cache_control] || ''
    end

    def validate!
      raise ArgumentError, 'response object must implement a `body` method' unless response.respond_to?(:body)
      raise ArgumentError, 'response object must implement a `headers` method' unless response.respond_to?(:headers)
    end
  end
end