Class: Hasta::S3URI

Inherits:
Object
  • Object
show all
Defined in:
lib/hasta/s3_uri.rb

Overview

Represents a URI to a file or directory on S3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, path) ⇒ S3URI

Returns a new instance of S3URI.



17
18
19
20
# File 'lib/hasta/s3_uri.rb', line 17

def initialize(bucket, path)
  @bucket = bucket
  @path = path
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



6
7
8
# File 'lib/hasta/s3_uri.rb', line 6

def bucket
  @bucket
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/hasta/s3_uri.rb', line 6

def path
  @path
end

Class Method Details

.parse(uri) ⇒ Object



8
9
10
11
12
13
14
15
# 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

Instance Method Details

#==(other) ⇒ Object



70
71
72
# 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) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
# 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

#basenameObject



30
31
32
33
34
35
36
# File 'lib/hasta/s3_uri.rb', line 30

def basename
  if path
    path.split('/').last
  else
    ''
  end
end

#depthObject



38
39
40
41
42
43
44
45
46
47
# 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?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/hasta/s3_uri.rb', line 22

def directory?
  path.nil? || path.end_with?('/')
end

#file?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/hasta/s3_uri.rb', line 26

def file?
  !directory?
end

#parentObject



56
57
58
59
60
61
62
63
# 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) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
# 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_sObject



74
75
76
# File 'lib/hasta/s3_uri.rb', line 74

def to_s
  ["s3:/", bucket, path].compact.join('/')
end