Class: Brutalismbot::Posts::Client

Inherits:
S3::Client show all
Defined in:
lib/brutalismbot/posts/stub.rb,
lib/brutalismbot/posts/client.rb

Instance Attribute Summary

Attributes inherited from S3::Client

#client, #prefix

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from S3::Client

#bucket, #keys

Constructor Details

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

Returns a new instance of Client.



11
12
13
14
15
# File 'lib/brutalismbot/posts/client.rb', line 11

def initialize(bucket:nil, prefix:nil, client:nil)
  bucket ||= ENV["POSTS_S3_BUCKET"] || "brutalismbot"
  prefix ||= ENV["POSTS_S3_PREFIX"] || "data/v1/posts/"
  super
end

Class Method Details

.stub(items = nil) ⇒ Object



31
32
33
# File 'lib/brutalismbot/posts/stub.rb', line 31

def stub(items = nil)
  new(prefix: "data/test/posts/").stub!(items)
end

Instance Method Details

#get(**options) ⇒ Object



21
22
23
# File 'lib/brutalismbot/posts/client.rb', line 21

def get(**options)
  super {|object| Reddit::Post.parse(object.body.read) }
end

#key_for(post) ⇒ Object



17
18
19
# File 'lib/brutalismbot/posts/client.rb', line 17

def key_for(post)
  File.join(@prefix, post.path)
end

#lastObject



25
26
27
# File 'lib/brutalismbot/posts/client.rb', line 25

def last
  Reddit::Post.parse(max_key.get.body.read)
end

#list(**options) ⇒ Object



29
30
31
32
33
34
# File 'lib/brutalismbot/posts/client.rb', line 29

def list(**options)
  super(**options) do |object|
    Brutalismbot.logger.info("GET s3://#{@bucket}/#{object.key}")
    Reddit::Post.parse(object.get.body.read)
  end
end

#max_keyObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brutalismbot/posts/client.rb', line 36

def max_key
  # Dig for max key
  prefix = Time.now.utc.strftime("#{@prefix}year=%Y/month=%Y-%m/day=%Y-%m-%d/")
  Brutalismbot.logger.info("GET s3://#{@bucket}/#{prefix}*")

  # Go up a level in prefix if no keys found
  until (keys = bucket.objects(prefix: prefix)).any? || prefix == @prefix
    prefix = prefix.split(/[^\/]+\/\z/).first
    Brutalismbot.logger.info("GET s3://#{@bucket}/#{prefix}*")
  end

  # Return max by key
  keys.max{|a,b| a.key <=> b.key }
end

#max_timeObject



51
52
53
54
# File 'lib/brutalismbot/posts/client.rb', line 51

def max_time
  max_key.key[/(\d+).json\z/, -1].to_i
rescue NoMethodError
end

#push(post, dryrun: nil) ⇒ Object



56
57
58
59
60
61
# File 'lib/brutalismbot/posts/client.rb', line 56

def push(post, dryrun:nil)
  options = post.to_s3(bucket: @bucket, prefix: @prefix)
  Brutalismbot.logger.info("PUT #{"DRYRUN " if dryrun}s3://#{options[:bucket]}/#{options[:key]}")
  @client.put_object(**options) unless dryrun
  options.slice(:bucket, :key)
end

#stub!(items = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/brutalismbot/posts/stub.rb', line 10

def stub!(items = nil)
  items ||= [Reddit::Post.stub]
  items   = items.map{|x| [key_for(x), x.to_h] }.to_h

  @client = Aws::S3::Client.new(stub_responses: true)

  @client.stub_responses :list_objects_v2, -> (context) do
    keys = items.keys.select{|x| x.start_with? context.params[:prefix] }
    {contents: keys.map{|x| {key:x} }}
  end

  @client.stub_responses :get_object, -> (context) do
    {body: StringIO.new(items.fetch(context.params[:key]).to_json)}
  end

  @stubbed = true

  self
end