class RainbowStorage::PathGenerator

Attributes

config[RW]

Public Class Methods

new(config) click to toggle source
# File lib/rainbow_storage/path_generator.rb, line 5
def initialize(config)
  @config = config
end

Public Instance Methods

filename(id, extension) click to toggle source

Add extension to path if present do not add extra dot before extesion

# File lib/rainbow_storage/path_generator.rb, line 11
def filename(id, extension)
  path = filepath(id)
  return path if extension.nil?
  "#{path}.#{extension.gsub(/^\./, '')}"
end
filepath(id) click to toggle source

Convert string like `abcdefghi` to `ab/cd/ef/ghi` to represent location on file system

# File lib/rainbow_storage/path_generator.rb, line 19
def filepath(id)
  tokens = hmac(id).chars.each_slice(2).map(&:join)
  result = []
  nesting_depth.times { result << tokens.shift }
  result << tokens.join
  result.join('/')
end

Private Instance Methods

hmac(id) click to toggle source
# File lib/rainbow_storage/path_generator.rb, line 29
def hmac(id)
  digest = OpenSSL::Digest.new('sha1')
  OpenSSL::HMAC.hexdigest(digest, hmac_key, id.to_s)
end
hmac_key() click to toggle source
# File lib/rainbow_storage/path_generator.rb, line 34
def hmac_key
  config.salt
end
nesting_depth() click to toggle source
# File lib/rainbow_storage/path_generator.rb, line 38
def nesting_depth
  config.nesting
end