Class: OpenStax::Aws::S3TextFile

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/aws/s3_text_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name:, bucket_region:, key:) ⇒ S3TextFile

Returns a new instance of S3TextFile.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
# File 'lib/openstax/aws/s3_text_file.rb', line 6

def initialize(bucket_name:, bucket_region:, key:)
  raise ArgumentError, "bucket_name cannot be nil" if bucket_name.nil?
  raise ArgumentError, "bucket_region cannot be nil" if bucket_region.nil?
  raise ArgumentError, "key cannot be nil" if key.nil?

  @bucket_name = bucket_name
  @bucket_region = bucket_region
  @key = key
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



4
5
6
# File 'lib/openstax/aws/s3_text_file.rb', line 4

def bucket_name
  @bucket_name
end

#bucket_regionObject (readonly)

Returns the value of attribute bucket_region.



4
5
6
# File 'lib/openstax/aws/s3_text_file.rb', line 4

def bucket_region
  @bucket_region
end

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/openstax/aws/s3_text_file.rb', line 4

def key
  @key
end

Instance Method Details

#deleteObject



37
38
39
# File 'lib/openstax/aws/s3_text_file.rb', line 37

def delete
  object.delete
end

#getObject



21
22
23
24
# File 'lib/openstax/aws/s3_text_file.rb', line 21

def get
  object.load
  object.get
end

#objectObject



41
42
43
44
45
46
47
# File 'lib/openstax/aws/s3_text_file.rb', line 41

def object
  @object ||= Aws::S3::Object.new(
    bucket_name,
    key,
    client: Aws::S3::Client.new(region: bucket_region)
  )
end

#readObject



16
17
18
19
# File 'lib/openstax/aws/s3_text_file.rb', line 16

def read
  object.load
  object.get.body.read
end

#write(string_contents:, content_type: 'text/plain', cache_control: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/openstax/aws/s3_text_file.rb', line 26

def write(string_contents:, content_type:'text/plain', cache_control: nil)
  args = {
    body: StringIO.new(string_contents)
  }

  args[:content_type] = content_type if !content_type.nil?
  args[:cache_control] = cache_control if !cache_control.nil?

  object.put(**args)
end