Class: SlackBot::ApiResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_bot/api_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ApiResponse

Returns a new instance of ApiResponse.



6
7
8
9
# File 'lib/slack_bot/api_client.rb', line 6

def initialize(&block)
  @response = block.call
  SlackBot::DevConsole.log_output "#{self.class.name}: #{response.body}"
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/slack_bot/api_client.rb', line 5

def response
  @response
end

Instance Method Details

#authentication_error?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/slack_bot/api_client.rb', line 31

def authentication_error?
  slack_error? && %w[invalid_auth account_inactive].include?(error)
end

#dataObject



35
36
37
38
39
40
# File 'lib/slack_bot/api_client.rb', line 35

def data
  JSON.parse(response.body)
rescue JSON::ParserError => e
  SlackBot::Logger.error("Failed to parse Slack API response: #{e.message}")
  {"ok" => false, "error" => "invalid_json_response"}
end

#errorObject



15
16
17
# File 'lib/slack_bot/api_client.rb', line 15

def error
  data["error"]
end

#ok?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/slack_bot/api_client.rb', line 11

def ok?
  response.status == 200 && data["ok"]
end

#rate_limited?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/slack_bot/api_client.rb', line 19

def rate_limited?
  response.status == 429 || (data["ok"] == false && data["error"] == "rate_limited")
end

#retry_afterObject



23
24
25
# File 'lib/slack_bot/api_client.rb', line 23

def retry_after
  response.headers["Retry-After"]&.to_i || 60
end

#slack_error?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/slack_bot/api_client.rb', line 27

def slack_error?
  !ok? && error.present?
end