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



52
53
54
# File 'lib/hasta/s3_uri.rb', line 52

def ==(other)
  self.class === other && (self.bucket == other.bucket && self.path == other.path)
end

#append(append_path) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
# File 'lib/hasta/s3_uri.rb', line 47

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

#directory?Boolean

Returns:

  • (Boolean)


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

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

#file?Boolean

Returns:

  • (Boolean)


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

def file?
  !directory?
end

#parentObject



38
39
40
41
42
43
44
45
# File 'lib/hasta/s3_uri.rb', line 38

def parent
  if path.nil?
    nil
  else
    elements = path.split('/')
    self.class.new(bucket, "#{elements.take(elements.length - 1).join('/')}/")
  end
end

#to_sObject



56
57
58
# File 'lib/hasta/s3_uri.rb', line 56

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