class RestFtpDaemon::Location

Attributes

aws_bucket[R]
aws_id[R]
aws_region[R]
aws_secret[R]
dir[R]
name[RW]

Accessors

scheme[R]
tokens[R]
uri[R]
url[R]

Public Class Methods

new(url) click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 26
def initialize url
  # Check parameters
  # unless url.is_a? String
  #   raise RestFtpDaemon::AssertionFailed, "location/init/string: #{url.inspect}"
  # end
  debug nil

  @url = url.clone
  debug :url, @url
  @tokens = []

  # Detect tokens in string
  @tokens = detect_tokens(url)
  debug :tokens, @tokens.inspect

  # First resolve tokens
  resolve_tokens! url

  # Build URI from parameters
  build_uri url

  # Extract dir and name
  build_dir_name

  # Specific initializations
  case @uri
  when URI::S3    then init_aws               # Match AWS URL with BUCKET.s3.amazonaws.com
  end
end

Public Instance Methods

dir_fs() click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 69
def dir_fs
  '/' + @dir
end
generate_temp_name!() click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 90
def generate_temp_name!
  @name = "#{@name}.temp-#{identifier(JOB_TEMPFILE_LEN)}"
end
is?(kind) click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 56
def is? kind
  @uri.is_a? kind
end
local_files() click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 73
def local_files
  Dir.glob("/#{path}").collect do |file|
    next unless File.readable? file
    next unless File.file? file
    # Create a new location object
    self.class.new(file)
  end
end
path() click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 60
def path
  # Don't join if dirname is empty
  return name.to_s if @dir.to_s.empty?
  return File.join(@dir.to_s, name.to_s)
end
path_fs() click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 66
def path_fs
  '/' + path
end
size() click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 82
def size
  return unless uri.is_a? URI::FILE

  local_file_path = path_fs
  return unless File.exist? path_fs
  return File.size path_fs
end

Private Instance Methods

build_dir_name() click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 150
def build_dir_name
  # Store URL parts (remove leading slashes in dir)
  @dir      = extract_dirname(@uri.path).to_s.gsub(/^\//, '')
  @name     = extract_filename(@uri.path)
  # @dir      = File.dirname(@uri.path)
  # @name     = File.basename(@uri.path)

  debug :dir, dir
  debug :name, name
end
build_uri(url) click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 105
def build_uri url
  # Fix scheme if URL as none
  url.gsub! /^\/(.*)/, 'file:/\1'

  # Parse that URL
  @uri = URI.parse url # rescue nil
  raise RestFtpDaemon::LocationParseError, location_path unless @uri

  # Remove unnecessary double slahes
  @uri.path.gsub!(/\/+/, '/')

  # Check we finally have a scheme
  debug :scheme, @uri.scheme 
  debug :path, @uri.path
  debug :host, @uri.host
  debug :to_s, @uri.to_s

  # Raise if still no scheme #FIXME
  raise RestFtpDaemon::SchemeUnsupported, url unless @uri.scheme
  # raise RestFtpDaemon::LocationParseError, base unless @uri
end
debug(var, val = nil) click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 194
def debug var, val = nil
  # Read conf if not already cached
  @debug ||= Conf.at(:debug, :location)

  # Skip if no debug requeste
  return unless @debug

  # Dump line
  if var.nil?
    printf("|%s \n", "-"*100) 
  else
    printf("| %-15s: %s \n", var, val)
  end
end
detect_tokens(item) click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 189
def detect_tokens item
  # item.scan /\[([^\[\]]*)\]/
  item.scan(/\[([^\[\]]*)\]/).map(&:first)
end
extract_dirname(path) click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 183
def extract_dirname path
  # match all the beginning of the string up to the last slash
  m = path.match(/^(.*)\/[^\/]*$/)
  return m[1].to_s unless m.nil?
end
extract_filename(path) click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 177
def extract_filename path
  # match everything that's after a slash at the end of the string
  m = path.match(/\/?([^\/]+)$/)
  return m[1].to_s unless m.nil?
end
init_aws() click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 161
def init_aws
  # Split hostname
  parts       = @uri.host.split('.')

  # Pop parts
  aws_tld     = parts.pop
  aws_domain  = parts.pop
  @aws_region = parts.pop
  aws_tag     = parts.pop
  @aws_bucket = parts.pop

  # Credentials from config
  @aws_id     = Conf.at(:credentials, @uri.host, :id)
  @aws_secret = Conf.at(:credentials, @uri.host, :secret)
end
resolve_tokens!(path) click to toggle source
# File lib/rest-ftp-daemon/location.rb, line 127
def resolve_tokens! path
  # Gther endpoints, and copy path string to alter it later
  endpoints = BmcDaemonLib::Conf[:endpoints]
  vectors = {}
  vectors = endpoints.clone if endpoints.is_a? Hash

  # Stack RANDOM into tokens
  vectors["RANDOM"] = SecureRandom.hex(JOB_RANDOM_LEN)

  # Replace endpoints defined in config
  vectors.each do |from, to|
    next unless to.is_a? String
    next if to.to_s.empty?
    path.gsub! tokenize(from), to
  end

  # Ensure result does not contain tokens after replacement
  detected = detect_tokens(path)
  unless detected.empty?
    raise RestFtpDaemon::JobUnresolvedTokens, 'unresolved tokens: ' + detected.join(' ')
  end
end
tokenize(item) click to toggle source

def scheme? condition

return @uri.scheme == condition

end

# File lib/rest-ftp-daemon/location.rb, line 100
def tokenize item
  return unless item.is_a? String
  "[#{item}]"
end