Class: Brutalismbot::Slack::Auth
- Defined in:
- lib/brutalismbot/slack/auth.rb,
lib/brutalismbot/slack/stub.rb
Class Method Summary collapse
Instance Method Summary collapse
- #channel_id ⇒ Object
- #inspect ⇒ Object
- #path ⇒ Object
- #push(post, dryrun: nil) ⇒ Object
- #team_id ⇒ Object
- #to_s3(bucket: nil, prefix: nil) ⇒ Object
- #webhook_url ⇒ Object
Methods inherited from Base
Methods included from Parser
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_id ⇒ Object
11 12 13 |
# File 'lib/brutalismbot/slack/auth.rb', line 11 def channel_id @item.dig("incoming_webhook", "channel_id") end |
#inspect ⇒ Object
15 16 17 |
# File 'lib/brutalismbot/slack/auth.rb', line 15 def inspect "#<#{self.class} #{team_id}/#{channel_id}>" end |
#path ⇒ Object
19 20 21 |
# File 'lib/brutalismbot/slack/auth.rb', line 19 def path File.join("team=#{team_id}", "channel=#{channel_id}", "oauth.json") end |
#push(post, dryrun: nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/brutalismbot/slack/auth.rb', line 23 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_id ⇒ Object
39 40 41 |
# File 'lib/brutalismbot/slack/auth.rb', line 39 def team_id @item.dig("team_id") end |
#to_s3(bucket: nil, prefix: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/brutalismbot/slack/auth.rb', line 43 def to_s3(bucket:nil, prefix:nil) bucket ||= ENV["SLACK_S3_BUCKET"] || "brutalismbot" prefix ||= ENV["SLACK_S3_PREFIX"] || "data/v1/auths/" { bucket: bucket, key: File.join(*[prefix, path].compact), body: to_json, } end |
#webhook_url ⇒ Object
53 54 55 |
# File 'lib/brutalismbot/slack/auth.rb', line 53 def webhook_url @item.dig("incoming_webhook", "url") end |