Class: Brutalismbot::Slack::Auth

Inherits:
Base
  • Object
show all
Defined in:
lib/brutalismbot/slack/auth.rb,
lib/brutalismbot/slack/stub.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Parser

#parse

Constructor Details

This class inherits a constructor from Brutalismbot::Base

Class Method Details

.stub(bot_id: nil, channel_id: nil, team_id: nil, user_id: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/brutalismbot/slack/stub.rb', line 44

def stub(bot_id:nil, channel_id:nil, team_id:nil, user_id:nil)
  bot_id     ||= "B#{SecureRandom.alphanumeric(8).upcase}"
  channel_id ||= "C#{SecureRandom.alphanumeric(8).upcase}"
  team_id    ||= "T#{SecureRandom.alphanumeric(8).upcase}"
  user_id    ||= "U#{SecureRandom.alphanumeric(8).upcase}"
  new(
    ok:           true,
    access_token: "<token>",
    scope:        "identify,incoming-webhook",
    user_id:      user_id,
    team_name:    "My Team",
    team_id:      team_id,
    incoming_webhook: {
      channel:           "#brutalism",
      channel_id:        channel_id,
      configuration_url: "https://my-team.slack.com/services/#{bot_id}",
      url:               "https://hooks.slack.com/services/#{team_id}/#{bot_id}/1234567890abcdef12345678",
    },
    scopes: [
      "identify",
      "incoming-webhook",
    ],
  )
end

Instance Method Details

#channel_idObject



11
12
13
# File 'lib/brutalismbot/slack/auth.rb', line 11

def channel_id
  @item.dig("incoming_webhook", "channel_id")
end

#pathObject



23
24
25
# File 'lib/brutalismbot/slack/auth.rb', line 23

def path
  File.join("team=#{team_id}", "channel=#{channel_id}", "oauth.json")
end

#push(post, dryrun: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/brutalismbot/slack/auth.rb', line 27

def push(post, dryrun:nil)
  body = post.to_slack.to_json
  uri  = URI.parse(webhook_url)
  Brutalismbot.logger.info("POST #{"DRYRUN " if dryrun}#{uri}")
  unless dryrun
    ssl = uri.scheme == "https"
    req = Net::HTTP::Post.new(uri, "content-type" => "application/json")
    req.body = body
    Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http|
      http.request(req)
    end
  else
    Net::HTTPOK.new("1.1", "204", "ok")
  end
end

#team_idObject



15
16
17
# File 'lib/brutalismbot/slack/auth.rb', line 15

def team_id
  @item.dig("team_id")
end

#webhook_urlObject



19
20
21
# File 'lib/brutalismbot/slack/auth.rb', line 19

def webhook_url
  @item.dig("incoming_webhook", "url")
end