class Brutalismbot::Posts::Client
Public Class Methods
new(bucket:nil, prefix:nil, client:nil)
click to toggle source
Calls superclass method
# 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
stub(items = nil)
click to toggle source
# File lib/brutalismbot/posts/stub.rb, line 31 def stub(items = nil) new(prefix: "data/test/posts/").stub!(items) end
Public Instance Methods
get(**options)
click to toggle source
Calls superclass method
# File lib/brutalismbot/posts/client.rb, line 22 def get(**options) super {|object| Reddit::Post.parse(object.body.read) } end
key_for(post)
click to toggle source
# File lib/brutalismbot/posts/client.rb, line 17 def key_for(post) path = post.created_utc.strftime("year=%Y/month=%Y-%m/day=%Y-%m-%d/%s.json") File.join(@prefix, path) end
last()
click to toggle source
# File lib/brutalismbot/posts/client.rb, line 26 def last Reddit::Post.parse(max_key.get.body.read) end
list(**options)
click to toggle source
Calls superclass method
# File lib/brutalismbot/posts/client.rb, line 30 def list(**options) super do |object| Brutalismbot.logger.info("GET s3://#{@bucket}/#{object.key}") Reddit::Post.parse(object.get.body.read) end end
max_key()
click to toggle source
# File lib/brutalismbot/posts/client.rb, line 37 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_time()
click to toggle source
# File lib/brutalismbot/posts/client.rb, line 52 def max_time max_key.key[/(\d+).json\z/, -1].to_i rescue NoMethodError end
push(post, dryrun:nil)
click to toggle source
# File lib/brutalismbot/posts/client.rb, line 57 def push(post, dryrun:nil) key = key_for(post) Brutalismbot.logger.info("PUT #{"DRYRUN " if dryrun}s3://#{@bucket}/#{key}") @client.put_object(bucket: @bucket, key: key, body: post.to_json) unless dryrun { bucket: @bucket, key: key, url: post.permalink, } end
stub!(items = nil)
click to toggle source
# 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