Class: Brutalismbot::Slack::Client
- Inherits:
-
Brutalismbot::S3::Client
- Object
- Brutalismbot::S3::Client
- Brutalismbot::Slack::Client
- Defined in:
- lib/brutalismbot/slack/stub.rb,
lib/brutalismbot/slack/client.rb
Instance Attribute Summary
Attributes inherited from Brutalismbot::S3::Client
Class Method Summary collapse
Instance Method Summary collapse
- #get(**options) ⇒ Object
-
#initialize(bucket: nil, prefix: nil, client: nil) ⇒ Client
constructor
A new instance of Client.
- #install(auth, dryrun: nil) ⇒ Object
- #key_for(auth) ⇒ Object
- #list(**options) ⇒ Object
- #push(body:, webhook_url:, dryrun: nil) ⇒ Object
- #stub!(items = nil) ⇒ Object
- #uninstall(auth, dryrun: nil) ⇒ Object
Methods inherited from Brutalismbot::S3::Client
Constructor Details
#initialize(bucket: nil, prefix: nil, client: nil) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 |
# File 'lib/brutalismbot/slack/client.rb', line 13 def initialize(bucket:nil, prefix:nil, client:nil) bucket ||= ENV["SLACK_S3_BUCKET"] || "brutalismbot" prefix ||= ENV["SLACK_S3_PREFIX"] || "data/v1/auths/" super end |
Class Method Details
.stub(items = nil) ⇒ Object
36 37 38 |
# File 'lib/brutalismbot/slack/stub.rb', line 36 def stub(items = nil) new(prefix: "data/test/auths/").stub!(items) end |
Instance Method Details
#get(**options) ⇒ Object
29 30 31 |
# File 'lib/brutalismbot/slack/client.rb', line 29 def get(**) super {|object| Auth.parse(object.body.read) } end |
#install(auth, dryrun: nil) ⇒ Object
19 20 21 22 23 |
# File 'lib/brutalismbot/slack/client.rb', line 19 def install(auth, dryrun:nil) key = key_for(auth) Brutalismbot.logger.info("PUT #{"DRYRUN " if dryrun}s3://#{@bucket}/#{key}") @client.put_object(bucket: @bucket, key: key, body: auth.to_json) unless dryrun end |
#key_for(auth) ⇒ Object
25 26 27 |
# File 'lib/brutalismbot/slack/client.rb', line 25 def key_for(auth) File.join(@prefix, auth.path) end |
#list(**options) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/brutalismbot/slack/client.rb', line 33 def list(**) super(**) do |object| Brutalismbot.logger.info("GET s3://#{@bucket}/#{object.key}") Auth.parse(object.get.body.read) end end |
#push(body:, webhook_url:, dryrun: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/brutalismbot/slack/client.rb', line 40 def push(body:, webhook_url:, dryrun:nil) Brutalismbot.logger.info("POST #{"DRYRUN " if dryrun}#{webhook_url}") unless dryrun uri = URI.parse(webhook_url) ssl = uri.scheme == "https" req = Net::HTTP::Post.new(uri, "content-type" => "application/json") req.body = body.to_json Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) {|http| http.request(req) } else Net::HTTPOK.new("1.1", "204", "ok") end end |
#stub!(items = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/brutalismbot/slack/stub.rb', line 11 def stub!(items = nil) items ||= [Auth.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 @client.stub_responses :delete_object, -> (context) do {version_id: context.params[:key]} end @stubbed = true self end |
#uninstall(auth, dryrun: nil) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/brutalismbot/slack/client.rb', line 53 def uninstall(auth, dryrun:nil) prefix = File.join(@prefix, "team=#{auth.team_id}/") Brutalismbot.logger.info("LIST s3://#{@bucket}/#{prefix}*") bucket.objects(prefix: prefix).map do |object| Brutalismbot.logger.info("DELETE #{"DRYRUN " if dryrun}s3://#{@bucket}/#{object.key}") object.delete unless dryrun end end |