class Hasta::S3URI

Represents a URI to a file or directory on S3

Attributes

bucket[R]
path[R]

Public Class Methods

new(bucket, path) click to toggle source
# File lib/hasta/s3_uri.rb, line 17
def initialize(bucket, path)
  @bucket = bucket
  @path = path
end
parse(uri) click to toggle source
# File lib/hasta/s3_uri.rb, line 8
def self.parse(uri)
  if match = /\As3n?:\/\/([^\/]+?)(\/.*)?\z/.match(uri)
    canonical_path = match[2] && match[2][1..-1]
    new(match[1], canonical_path)
  else
    raise ArgumentError, "Invalid S3 URI: #{uri}"
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/hasta/s3_uri.rb, line 70
def ==(other)
  self.class === other && (self.bucket == other.bucket && self.path == other.path)
end
append(append_path) click to toggle source
# File lib/hasta/s3_uri.rb, line 65
def append(append_path)
  raise ArgumentError, "Cannot append to a file path: #{self}" if file?
  self.class.new(bucket, File.join(path, append_path))
end
basename() click to toggle source
# File lib/hasta/s3_uri.rb, line 30
def basename
  if path
    path.split('/').last
  else
    ''
  end
end
depth() click to toggle source
# File lib/hasta/s3_uri.rb, line 38
def depth
  slashes = (path && path.chars.count { |ch| ch == '/' }) || 0
  if path.nil?
    1
  elsif directory?
    1 + slashes
  else
    2 + slashes
  end
end
directory?() click to toggle source
# File lib/hasta/s3_uri.rb, line 22
def directory?
  path.nil? || path.end_with?('/')
end
file?() click to toggle source
# File lib/hasta/s3_uri.rb, line 26
def file?
  !directory?
end
parent() click to toggle source
# File lib/hasta/s3_uri.rb, line 56
def parent
  if path.nil?
    nil
  else
    elements = path.split('/')
    self.class.new(bucket, "#{elements.take(elements.length - 1).join('/')}/")
  end
end
start_with?(s3_uri) click to toggle source
# File lib/hasta/s3_uri.rb, line 49
def start_with?(s3_uri)
  return true if self == s3_uri
  return false if s3_uri.file?

  (bucket == s3_uri.bucket) && (s3_uri.path.nil? || path.start_with?(s3_uri.path))
end
to_s() click to toggle source
# File lib/hasta/s3_uri.rb, line 74
def to_s
  ["s3:/", bucket, path].compact.join('/')
end