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
#bucket, #client, #prefix
Class Method Summary
collapse
Instance Method Summary
collapse
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
30
31
32
|
# File 'lib/brutalismbot/posts/stub.rb', line 30
def stub(items = nil)
new(prefix: "data/test/posts/").stub!(items)
end
|
Instance Method Details
#get(key) ⇒ Object
21
22
23
|
# File 'lib/brutalismbot/posts/client.rb', line 21
def get(key)
super {|object| Reddit::Post.parse(object.get.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
|
#last ⇒ Object
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.name}/#{object.key}")
Reddit::Post.parse(object.get.body.read)
end
end
|
#max_key ⇒ Object
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
prefix = Time.now.utc.strftime("#{@prefix}year=%Y/month=%Y-%m/day=%Y-%m-%d/")
Brutalismbot.logger.info("GET s3://#{@bucket.name}/#{prefix}*")
until (keys = @bucket.objects(prefix: prefix)).any? || prefix == @prefix
prefix = prefix.split(/[^\/]+\/\z/).first
Brutalismbot.logger.info("GET s3://#{@bucket.name}/#{prefix}*")
end
keys.max{|a,b| a.key <=> b.key }
end
|
#max_time ⇒ Object
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.name, prefix: @prefix)
Brutalismbot.logger.info("PUT #{"DRYRUN " if dryrun}s3://#{options[:bucket]}/#{options[:key]}")
@bucket.put_object(**options.slice(:key, :body, :metadata)) unless dryrun
options.slice(:bucket, :key, :metadata)
end
|
#stub!(items = nil) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/brutalismbot/posts/stub.rb', line 8
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)
@bucket = Aws::S3::Bucket.new(name: @bucket.name, client: @client)
@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
|