brutalismbot 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5f0cc2d8f65ce4d6e52cd68fba606fd17c9bb7467b7897d33f24687a298b50a
4
- data.tar.gz: a0759a82b139ae7c700eede3a0360d85c521c8d79cd3d97370b8b6597a6b3d02
3
+ metadata.gz: 2b1a53227321c1d498e19fadd19c6672765235e10de63d5182028ef88e97f6ea
4
+ data.tar.gz: 7412d9894367b09119948295333c7e5d40444b1cdca4e686a045de33bcabd20a
5
5
  SHA512:
6
- metadata.gz: 1441ac4a1edd40e508838855702555a0b7e45c8281dc46b3890217b9228154bb5c254781c93bf2e7df8e95a4a9920a816be96bd078454d7157c61d5a7774b110
7
- data.tar.gz: 6d12e3c06bcc3fdaaa5e958002f4c4b2ef82bc8c0f33bd4a85f882003ad35d83b474015138e2370a8d2b54e86caa71f8c4c40a3eb915a37dcbc7077b30ea113b
6
+ metadata.gz: 52ee57689c45dd034e4660ebe2923edd424ee3bf47181d3e858ad023d12ca7937fef43a0f2acb2eeedf2b7bc350675b945c1ed5da24abadd440531091aa6ecd1
7
+ data.tar.gz: 1df7a1413841387e689078238a2ee786ea06a682b39a80865d856f408f91aa2419647def2ba33910f32e37dd91b589690867dc386234631103b3005768bcea2b
@@ -18,8 +18,8 @@ module Brutalismbot
18
18
  File.join(@prefix, post.path)
19
19
  end
20
20
 
21
- def get(key)
22
- super {|object| Reddit::Post.parse(object.get.body.read) }
21
+ def get(**options)
22
+ super {|object| Reddit::Post.parse(object.body.read) }
23
23
  end
24
24
 
25
25
  def last
@@ -28,7 +28,7 @@ module Brutalismbot
28
28
 
29
29
  def list(**options)
30
30
  super(**options) do |object|
31
- Brutalismbot.logger.info("GET s3://#{@bucket.name}/#{object.key}")
31
+ Brutalismbot.logger.info("GET s3://#{@bucket}/#{object.key}")
32
32
  Reddit::Post.parse(object.get.body.read)
33
33
  end
34
34
  end
@@ -36,12 +36,12 @@ module Brutalismbot
36
36
  def max_key
37
37
  # Dig for max key
38
38
  prefix = Time.now.utc.strftime("#{@prefix}year=%Y/month=%Y-%m/day=%Y-%m-%d/")
39
- Brutalismbot.logger.info("GET s3://#{@bucket.name}/#{prefix}*")
39
+ Brutalismbot.logger.info("GET s3://#{@bucket}/#{prefix}*")
40
40
 
41
41
  # Go up a level in prefix if no keys found
42
- until (keys = @bucket.objects(prefix: prefix)).any? || prefix == @prefix
42
+ until (keys = bucket.objects(prefix: prefix)).any? || prefix == @prefix
43
43
  prefix = prefix.split(/[^\/]+\/\z/).first
44
- Brutalismbot.logger.info("GET s3://#{@bucket.name}/#{prefix}*")
44
+ Brutalismbot.logger.info("GET s3://#{@bucket}/#{prefix}*")
45
45
  end
46
46
 
47
47
  # Return max by key
@@ -54,9 +54,9 @@ module Brutalismbot
54
54
  end
55
55
 
56
56
  def push(post, dryrun:nil)
57
- options = post.to_s3(bucket: @bucket.name, prefix: @prefix)
57
+ options = post.to_s3(bucket: @bucket, prefix: @prefix)
58
58
  Brutalismbot.logger.info("PUT #{"DRYRUN " if dryrun}s3://#{options[:bucket]}/#{options[:key]}")
59
- @bucket.put_object(**options.slice(:key, :body, :metadata)) unless dryrun
59
+ @client.put_object(**options) unless dryrun
60
60
  options.slice(:bucket, :key, :metadata)
61
61
  end
62
62
  end
@@ -10,7 +10,6 @@ module Brutalismbot
10
10
  items = items.map{|x| [key_for(x), x.to_h] }.to_h
11
11
 
12
12
  @client = Aws::S3::Client.new(stub_responses: true)
13
- @bucket = Aws::S3::Bucket.new(name: @bucket.name, client: @client)
14
13
 
15
14
  @client.stub_responses :list_objects_v2, -> (context) do
16
15
  keys = items.keys.select{|x| x.start_with? context.params[:prefix] }
@@ -3,27 +3,33 @@ require "aws-sdk-s3"
3
3
  module Brutalismbot
4
4
  module S3
5
5
  class Client
6
- attr_reader :bucket, :prefix, :client
6
+ attr_reader :prefix, :client
7
7
 
8
8
  def initialize(bucket:nil, prefix:nil, client:nil)
9
- bucket ||= ENV["S3_BUCKET"] || "brutalismbot"
10
- prefix ||= ENV["S3_PREFIX"] || "data/v1/"
11
- client ||= Aws::S3::Client.new
12
- @bucket = Aws::S3::Bucket.new(name: bucket, client: client)
13
- @client = client
14
- @prefix = prefix
9
+ @client = client || Aws::S3::Client.new
10
+ @bucket = bucket || ENV["S3_BUCKET"] || "brutalismbot"
11
+ @prefix = prefix || ENV["S3_PREFIX"] || "data/v1/"
15
12
  end
16
13
 
17
- def get(key, &block)
18
- Brutalismbot.logger.info("GET s3://#{@bucket.name}/#{key}")
19
- object = @bucket.object(key)
14
+ def bucket(**options)
15
+ options[:name] ||= @bucket
16
+ options[:client] ||= @client
17
+ Aws::S3::Bucket.new(**options)
18
+ end
19
+
20
+ def get(key:, bucket:nil, **options, &block)
21
+ bucket ||= @bucket
22
+ Brutalismbot.logger.info("GET s3://#{@bucket}/#{key}")
23
+ object = @client.get_object(bucket: bucket, key: key, **options)
20
24
  block_given? ? yield(object) : object
21
25
  end
22
26
 
23
- def list(**options, &block)
24
- options[:prefix] ||= @prefix
25
- Brutalismbot.logger.info("LIST s3://#{@bucket.name}/#{options[:prefix]}*")
26
- Prefix.new(@bucket.objects(options), &block)
27
+ def list(bucket:nil, prefix:nil, **options, &block)
28
+ bucket ||= @bucket
29
+ prefix ||= @prefix
30
+ Brutalismbot.logger.info("LIST s3://#{@bucket}/#{prefix}*")
31
+ result = self.bucket(name: bucket).objects(prefix: prefix, **options)
32
+ Prefix.new(result, &block)
27
33
  end
28
34
  end
29
35
  end
@@ -12,12 +12,8 @@ module Brutalismbot
12
12
  @item.dig("incoming_webhook", "channel_id")
13
13
  end
14
14
 
15
- def team_id
16
- @item.dig("team_id")
17
- end
18
-
19
- def webhook_url
20
- @item.dig("incoming_webhook", "url")
15
+ def inspect
16
+ "#<#{self.class} #{team_id}/#{channel_id}>"
21
17
  end
22
18
 
23
19
  def path
@@ -39,6 +35,14 @@ module Brutalismbot
39
35
  Net::HTTPOK.new("1.1", "204", "ok")
40
36
  end
41
37
  end
38
+
39
+ def team_id
40
+ @item.dig("team_id")
41
+ end
42
+
43
+ def webhook_url
44
+ @item.dig("incoming_webhook", "url")
45
+ end
42
46
  end
43
47
  end
44
48
  end
@@ -16,21 +16,21 @@ module Brutalismbot
16
16
 
17
17
  def install(auth, dryrun:nil)
18
18
  key = key_for(auth)
19
- Brutalismbot.logger.info("PUT #{"DRYRUN " if dryrun}s3://#{@bucket.name}/#{key}")
20
- @bucket.put_object(key: key, body: auth.to_json) unless dryrun
19
+ Brutalismbot.logger.info("PUT #{"DRYRUN " if dryrun}s3://#{@bucket}/#{key}")
20
+ @client.put_object(bucket: @bucket, key: key, body: auth.to_json) unless dryrun
21
21
  end
22
22
 
23
23
  def key_for(auth)
24
24
  File.join(@prefix, auth.path)
25
25
  end
26
26
 
27
- def get(key)
28
- super {|object| Auth.parse(object.get.body.read) }
27
+ def get(**options)
28
+ super {|object| Auth.parse(object.body.read) }
29
29
  end
30
30
 
31
31
  def list(**options)
32
32
  super(**options) do |object|
33
- Brutalismbot.logger.info("GET s3://#{@bucket.name}/#{object.key}")
33
+ Brutalismbot.logger.info("GET s3://#{@bucket}/#{object.key}")
34
34
  Auth.parse(object.get.body.read)
35
35
  end
36
36
  end
@@ -38,16 +38,16 @@ module Brutalismbot
38
38
  def push(post, dryrun:nil)
39
39
  list.map do |auth|
40
40
  key = key_for(auth)
41
- Brutalismbot.logger.info("PUSH #{"DRYRUN " if dryrun}s3://#{@bucket.name}/#{key}")
41
+ Brutalismbot.logger.info("PUSH #{"DRYRUN " if dryrun}s3://#{@bucket}/#{key}")
42
42
  auth.push(post, dryrun: dryrun)
43
43
  end
44
44
  end
45
45
 
46
46
  def uninstall(auth, dryrun:nil)
47
47
  prefix = File.join(@prefix, "team=#{auth.team_id}/")
48
- Brutalismbot.logger.info("LIST s3://#{@bucket.name}/#{prefix}*")
49
- @bucket.objects(prefix: prefix).map do |object|
50
- Brutalismbot.logger.info("DELETE #{"DRYRUN " if dryrun}s3://#{@bucket.name}/#{object.key}")
48
+ Brutalismbot.logger.info("LIST s3://#{@bucket}/#{prefix}*")
49
+ bucket.objects(prefix: prefix).map do |object|
50
+ Brutalismbot.logger.info("DELETE #{"DRYRUN " if dryrun}s3://#{@bucket}/#{object.key}")
51
51
  object.delete unless dryrun
52
52
  end
53
53
  end
@@ -12,7 +12,6 @@ module Brutalismbot
12
12
  items = items.map{|x| [key_for(x), x.to_h] }.to_h
13
13
 
14
14
  @client = Aws::S3::Client.new(stub_responses: true)
15
- @bucket = Aws::S3::Bucket.new(name: @bucket.name, client: @client)
16
15
 
17
16
  @client.stub_responses :list_objects_v2, -> (context) do
18
17
  keys = items.keys.select{|x| x.start_with? context.params[:prefix] }
@@ -1,3 +1,3 @@
1
1
  module Brutalismbot
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brutalismbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Mancevice