class Brutalismbot::Slack::Client

Public Class Methods

new(bucket:nil, prefix:nil, client:nil) click to toggle source
Calls superclass method Brutalismbot::S3::Client::new
# 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
stub(items = nil) click to toggle source
# File lib/brutalismbot/slack/stub.rb, line 36
def stub(items = nil)
  new(prefix: "data/test/auths/").stub!(items)
end

Public Instance Methods

get(**options) click to toggle source
Calls superclass method Brutalismbot::S3::Client#get
# File lib/brutalismbot/slack/client.rb, line 29
def get(**options)
  super {|object| Auth.parse(object.body.read) }
end
install(auth, dryrun:nil) click to toggle source
# 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) click to toggle source
# File lib/brutalismbot/slack/client.rb, line 25
def key_for(auth)
  File.join(@prefix, auth.path)
end
list(**options) click to toggle source
Calls superclass method Brutalismbot::S3::Client#list
# File lib/brutalismbot/slack/client.rb, line 33
def list(**options)
  super do |object|
    Brutalismbot.logger.info("GET s3://#{@bucket}/#{object.key}")
    Auth.parse(object.get.body.read)
  end
end
push(post, webhook_url, dryrun:nil) click to toggle source
# File lib/brutalismbot/slack/client.rb, line 40
def push(post, webhook_url, dryrun:nil)
  blocks = blocks_for(post)

  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 = { text: post.title, blocks: blocks }.to_json
    Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http|
      http.request(req)
    end.tap do |res|
      Brutalismbot.logger.error("RESPONSE [#{res.code}] #{res.body}") unless res.kind_of?(Net::HTTPSuccess)
    end
  else
    Net::HTTPOK.new("1.1", "204", "ok")
  end
end
stub!(items = nil) click to toggle source
# 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) click to toggle source
# File lib/brutalismbot/slack/client.rb, line 59
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

Private Instance Methods

blocks_for(post) click to toggle source
# File lib/brutalismbot/slack/client.rb, line 70
def blocks_for(post)
  post.media_urls.map do |media_url|
    [
      {
        type: "image",
        title: {
          type: "plain_text",
          text: "/r/brutalism",
          emoji: true,
        },
        image_url: media_url,
        alt_text: post.title,
      },
      {
        type: "context",
        elements: [
          {
            type: "mrkdwn",
            text: "<#{post.permalink}|#{post.title}>",
          },
        ],
      },
    ]
  end.flatten
end