Class: GetStreamRuby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/getstream_ruby/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, api_key: nil, api_secret: nil, **options) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/getstream_ruby/client.rb', line 30

def initialize(config = nil, api_key: nil, api_secret: nil, **options)
  @configuration = config || GetStreamRuby.configuration

  # Create new configuration with overrides if any parameters provided
  if api_key || api_secret || !options.empty?
    @configuration = Configuration.with_overrides(
      api_key: api_key,
      api_secret: api_secret,
      **options,
    )
  end

  @configuration.validate!
  @connection = build_connection
  @configuration.log_pool_config_to(@configuration.logger)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



28
29
30
# File 'lib/getstream_ruby/client.rb', line 28

def configuration
  @configuration
end

Instance Method Details

#chatGetStream::Generated::ChatClient

Returns The chat API client.

Returns:



69
70
71
# File 'lib/getstream_ruby/client.rb', line 69

def chat
  @chat ||= GetStream::Generated::ChatClient.new(self)
end

#commonGetStream::Generated::CommonClient

Returns The common API client.

Returns:



54
55
56
# File 'lib/getstream_ruby/client.rb', line 54

def common
  @common ||= GetStream::Generated::CommonClient.new(self)
end

#feed(feed_group_id, feed_id) ⇒ GetStream::Generated::Feed

Create an individual feed instance

Parameters:

  • feed_group_id (String)

    The feed group ID

  • feed_id (String)

    The feed ID

Returns:



82
83
84
# File 'lib/getstream_ruby/client.rb', line 82

def feed(feed_group_id, feed_id)
  GetStream::Generated::Feed.new(self, feed_group_id, feed_id)
end

#feed_resourceObject



47
48
49
# File 'lib/getstream_ruby/client.rb', line 47

def feed_resource
  @feed_resource ||= Resources::Feed.new(self)
end

#feedsGetStream::Generated::FeedsClient

Returns The feeds API client.

Returns:



59
60
61
# File 'lib/getstream_ruby/client.rb', line 59

def feeds
  @feeds ||= GetStream::Generated::FeedsClient.new(self)
end

#make_request(method, path, query_params: nil, body: nil, request_timeout: nil) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/getstream_ruby/client.rb', line 182

def make_request(method, path, query_params: nil, body: nil, request_timeout: nil)
  # Handle query parameters
  if query_params && !query_params.empty?
    query_string = query_params.map { |k, v| "#{k}=#{v}" }.join('&')
    path = "#{path}?#{query_string}"
  end

  # Make the request
  request(method, path, body, request_timeout: request_timeout)
end

#moderationGetStream::Generated::ModerationClient

Returns The moderation API client.

Returns:



64
65
66
# File 'lib/getstream_ruby/client.rb', line 64

def moderation
  @moderation ||= GetStream::Generated::ModerationClient.new(self)
end

#parse_sns(notification_body) ⇒ Object

Decode + parse a Stream-delivered SNS notification body.

Accepts either the raw SNS HTTP envelope JSON or the pre-extracted Message string. Convenience wrapper around StreamChat::Webhook.parse_sns. No signature is required; SNS deliveries are authenticated via AWS IAM.



128
129
130
# File 'lib/getstream_ruby/client.rb', line 128

def parse_sns(notification_body)
  StreamChat::Webhook.parse_sns(notification_body)
end

#parse_sqs(message_body) ⇒ Object

Decode + parse a Stream-delivered SQS message body.

Convenience wrapper around StreamChat::Webhook.parse_sqs. No signature is required; SQS deliveries are authenticated via AWS IAM.



119
120
121
# File 'lib/getstream_ruby/client.rb', line 119

def parse_sqs(message_body)
  StreamChat::Webhook.parse_sqs(message_body)
end

#post(path, body = {}) ⇒ GetStreamRuby::StreamResponse

Returns The API response.

Parameters:

  • path (String)

    The API path

  • body (Hash) (defaults to: {})

    The request body

Returns:



135
136
137
# File 'lib/getstream_ruby/client.rb', line 135

def post(path, body = {})
  request(:post, path, body)
end

#verify_and_parse_webhook(body, signature) ⇒ Object

Verify and parse a webhook payload in one call, using this client’s API secret (CHA-2961).

Handles gzip-compressed bodies transparently. Raises StreamChat::Webhook::InvalidWebhookError on signature mismatch or parse failures; distinguish failure modes via the message substring.

Parameters:

  • body (String)

    raw request body (possibly gzip-compressed)

  • signature (String)

    X-Signature header value

Returns:

  • (Object)

    the typed event class instance or StreamChat::Webhook::UnknownEvent

Raises:



111
112
113
# File 'lib/getstream_ruby/client.rb', line 111

def verify_and_parse_webhook(body, signature)
  StreamChat::Webhook.verify_and_parse_webhook(body, signature, @configuration.api_secret)
end

#verify_signature(body, signature) ⇒ Boolean

Verify a webhook signature using this client’s API secret (CHA-2961).

Convenience wrapper around StreamChat::Webhook.verify_signature that supplies the secret automatically. The module-level method is still available for callers that need to verify with an arbitrary secret.

Parameters:

  • body (String)

    The raw request body (already-decompressed)

  • signature (String)

    The signature from the X-Signature header

Returns:

  • (Boolean)

    true if the signature is valid, false otherwise



95
96
97
# File 'lib/getstream_ruby/client.rb', line 95

def verify_signature(body, signature)
  StreamChat::Webhook.verify_signature(body, signature, @configuration.api_secret)
end

#videoGetStream::Generated::VideoClient

Returns The video API client.

Returns:



74
75
76
# File 'lib/getstream_ruby/client.rb', line 74

def video
  @video ||= GetStream::Generated::VideoClient.new(self)
end

#wait_for_task(task_id, poll_interval: 1, timeout: 60) ⇒ Object

Polls the task-status endpoint until the task reaches a terminal state.

Behaviour:

- status="completed": returns the task `result` payload.
- status="failed":    raises `TaskError` populated from the task's
                      `ErrorResult` (`type`, `description`, `stacktrace`,
                      `version`).
- timeout elapsed:    raises `TransportError` with `error_type:
                      "timeout"`.

Parameters:

  • task_id (String)
  • poll_interval (Numeric) (defaults to: 1)

    seconds between polls (default 1)

  • timeout (Numeric) (defaults to: 60)

    max seconds to wait (default 60)

Returns:

  • (Object)

    the task ‘result` payload on success

Raises:

  • (TaskError)

    when the task reports ‘status=“failed”`

  • (TransportError)

    when the timeout elapses (‘error_type=“timeout”`)



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/getstream_ruby/client.rb', line 155

def wait_for_task(task_id, poll_interval: 1, timeout: 60)
  start_time = monotonic_now

  loop do

    response = common.get_task(task_id)
    status = response.status

    case status
    when 'completed'
      return response.result
    when 'failed'
      raise ErrorMapping.build_task_error(task_id, response.error)
    end

    if monotonic_now - start_time >= timeout
      raise TransportError.new(
        "wait_for_task timed out after #{timeout}s for task_id=#{task_id}",
        error_type: 'timeout',
      )
    end

    sleep(poll_interval)

  end
end