Class: FakeS3::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_s3/object.rb

Defined Under Namespace

Classes: FakeObjectOutput

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, key, body) ⇒ Object

Returns a new instance of Object.



7
8
9
10
11
# File 'lib/fake_s3/object.rb', line 7

def initialize(bucket, key, body)
  @bucket, @key, @body = bucket, key, body
  @last_modified = Time.now
  @content_length = 1
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



4
5
6
# File 'lib/fake_s3/object.rb', line 4

def bucket
  @bucket
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/fake_s3/object.rb', line 3

def key
  @key
end

#last_modifiedObject

Returns the value of attribute last_modified.



5
6
7
# File 'lib/fake_s3/object.rb', line 5

def last_modified
  @last_modified
end

Instance Method Details

#content_lengthObject



17
18
19
# File 'lib/fake_s3/object.rb', line 17

def content_length
  @body.length # this should be in bytes, but this is roughly the same, good enough for tests
end

#download_file(path) ⇒ Object



32
33
34
35
36
# File 'lib/fake_s3/object.rb', line 32

def download_file(path)
  File.open(path, 'wb') do |f|
    f << @body
  end
end

#exists?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/fake_s3/object.rb', line 13

def exists?
  !!@body
end

#getObject



28
29
30
# File 'lib/fake_s3/object.rb', line 28

def get
  FakeObjectOutput.new(@body)
end

#move_to(location) ⇒ Object



42
43
44
# File 'lib/fake_s3/object.rb', line 42

def move_to(location)
  location.bucket.move_to(self, location)
end

#public_urlObject



46
47
48
# File 'lib/fake_s3/object.rb', line 46

def public_url
  "https://test.tc.com.s3.amazonaws.com/#{@key}"
end

#put(options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/fake_s3/object.rb', line 21

def put(options = {})
  if options[:body]
    @body = options[:body]
  end
  self
end

#upload_file(source, options = {}) ⇒ Object



38
39
40
# File 'lib/fake_s3/object.rb', line 38

def upload_file(source, options = {})
  @body = source.respond_to?(:read) ? source.read : File.read(source)
end