Class: SlackBot::ApiResponse
- Inherits:
-
Object
- Object
- SlackBot::ApiResponse
- Defined in:
- lib/slack_bot/api_client.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #authentication_error? ⇒ Boolean
- #data ⇒ Object
- #error ⇒ Object
-
#initialize(&block) ⇒ ApiResponse
constructor
A new instance of ApiResponse.
- #ok? ⇒ Boolean
- #rate_limited? ⇒ Boolean
- #retry_after ⇒ Object
- #slack_error? ⇒ Boolean
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
#response ⇒ Object (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
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 |
#data ⇒ Object
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.}") {"ok" => false, "error" => "invalid_json_response"} end |
#error ⇒ Object
15 16 17 |
# File 'lib/slack_bot/api_client.rb', line 15 def error data["error"] end |
#ok? ⇒ Boolean
11 12 13 |
# File 'lib/slack_bot/api_client.rb', line 11 def ok? response.status == 200 && data["ok"] end |
#rate_limited? ⇒ 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_after ⇒ Object
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
27 28 29 |
# File 'lib/slack_bot/api_client.rb', line 27 def slack_error? !ok? && error.present? end |