Class: Brutalismbot::S3::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brutalismbot/s3/client.rb

Direct Known Subclasses

Posts::Client, Brutalismbot::Slack::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket: nil, prefix: nil, client: nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
# File 'lib/brutalismbot/s3/client.rb', line 8

def initialize(bucket:nil, prefix:nil, client:nil)
  @client = client || Aws::S3::Client.new
  @bucket = bucket || ENV["S3_BUCKET"] || "brutalismbot"
  @prefix = prefix || ENV["S3_PREFIX"] || "data/v1/"
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/brutalismbot/s3/client.rb', line 6

def client
  @client
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/brutalismbot/s3/client.rb', line 6

def prefix
  @prefix
end

Instance Method Details

#bucket(**options) ⇒ Object



14
15
16
17
18
# File 'lib/brutalismbot/s3/client.rb', line 14

def bucket(**options)
  options[:name] ||= @bucket
  options[:client] ||= @client
  Aws::S3::Bucket.new(**options)
end

#get(key:, bucket: nil, **options, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/brutalismbot/s3/client.rb', line 20

def get(key:, bucket:nil, **options, &block)
  bucket ||= @bucket
  Brutalismbot.logger.info("GET s3://#{@bucket}/#{key}")
  object = @client.get_object(bucket: bucket, key: key, **options)
  block_given? ? yield(object) : object
end

#keys(bucket: nil, prefix: nil, **options) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/brutalismbot/s3/client.rb', line 27

def keys(bucket:nil, prefix:nil, **options)
  bucket ||= @bucket
  prefix ||= @prefix
  Brutalismbot.logger.info("LIST s3://#{@bucket}/#{prefix}*")
  result = self.bucket(name: bucket).objects(prefix: prefix, **options)
  Prefix.new(result)
end

#list(bucket: nil, prefix: nil, **options, &block) ⇒ Object



35
36
37
38
# File 'lib/brutalismbot/s3/client.rb', line 35

def list(bucket:nil, prefix:nil, **options, &block)
  result = keys(bucket: bucket, prefix: prefix, **options)
  Prefix.new(result, &block)
end