Module: RightScale::CloudApi::CacheValidator::ClassMethods

Defined in:
lib/base/routines/cache_validator.rb

Constant Summary collapse

CACHE_PATTERN_KEYS =
[ :verb, :verb!, :path, :path!, :request, :request!, :code, :code!, :response, :response!, :key, :if, :sign ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



64
65
66
67
68
# File 'lib/base/routines/cache_validator.rb', line 64

def self.extended(base)
  unless base.respond_to?(:options) && base.options.is_a?(Hash)
    raise Error::new("CacheValidator routine assumes class being extended responds to :options and returns a hash") 
  end
end

Instance Method Details

#cache_pattern(cache_pattern) ⇒ Object

Adds new cache patters. Patterns are analyzed in order of their definnition. If one pattern hits the rest are not analyzed.

@example:

cache_pattern :verb  => /get|post/,
              :path  => /Action=Describe/,
              :if    => Proc::new{ |o| (o[:params].keys - %w{Action Version AWSAccessKeyId})._blank? },
              :key   => Proc::new{ |o| o[:params]['Action'] },
              :sign  => Proc::new{ |o| o[:response].body.to_s.sub(%r{<requestId>.+?</requestId>}i,'') }

Parameters:

  • cache_pattern (Hash)

    A hash of pattern keys.

Options Hash (cache_pattern):

  • :key (Proc)

    A method that calculates a kache key name.

  • :sign (Proc)

    A method that modifies the response before calculating md5.

See Also:



87
88
89
90
91
92
93
# File 'lib/base/routines/cache_validator.rb', line 87

def cache_pattern(cache_pattern)
  fail Error::new("Pattern should be a Hash and should not be blank") if !cache_pattern.is_a?(Hash) || cache_pattern._blank?
  fail Error::new("Key field not found in cache pattern definition #{cache_pattern.inspect}") unless cache_pattern.keys.include?(:key)
  unsupported_keys = cache_pattern.keys - CACHE_PATTERN_KEYS
  fail Error::new("Unsupported keys #{unsupported_keys.inspect} in cache pattern definition #{cache_pattern.inspect}") unless unsupported_keys._blank?
  (options[:cache_patterns] ||= []) << cache_pattern
end