Class: Ask::Slack::ClientProxy

Inherits:
BasicObject
Defined in:
lib/ask/slack/client.rb

Overview

Proxies method calls to a ::Slack::Web::Client, converting auth errors and retrying transient network failures with exponential backoff.

Constant Summary collapse

MAX_RETRIES =
3
RETRYABLE_ERRORS =
[
  ::Faraday::TimeoutError,
  ::Faraday::ConnectionFailed,
  ::Faraday::ServerError,
  ::Errno::ECONNREFUSED,
  ::Errno::ECONNRESET,
  ::Timeout::Error
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, base_delay: nil) ⇒ ClientProxy

Returns a new instance of ClientProxy.



58
59
60
61
# File 'lib/ask/slack/client.rb', line 58

def initialize(client, base_delay: nil)
  @client = client
  @_base_delay = base_delay || 1
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ask/slack/client.rb', line 63

def method_missing(name, ...)
  retry_count = 0
  begin
    @client.public_send(name, ...)
  rescue ::Slack::Web::Api::Errors::NotAuthed,
         ::Slack::Web::Api::Errors::InvalidAuth,
         ::Slack::Web::Api::Errors::AccountInactive,
         ::Slack::Web::Api::Errors::TokenRevoked,
         ::Slack::Web::Api::Errors::TokenExpired
    ::Kernel.raise ::Ask::Auth::InvalidCredential, :slack_token
  rescue *RETRYABLE_ERRORS => e
    retry_count += 1
    if retry_count <= MAX_RETRIES
      ::Kernel.sleep(@_base_delay * (2 ** (retry_count - 1)))
      retry
    end
    ::Kernel.raise ::Ask::Auth::InvalidCredential.new(:slack_token, e.message)
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



56
57
58
# File 'lib/ask/slack/client.rb', line 56

def client
  @client
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/ask/slack/client.rb', line 83

def respond_to_missing?(name, include_private = false)
  @client.respond_to?(name, include_private) || super
end