xdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +123 -0
  4. data/lib/xdk/client.rb +410 -0
  5. data/lib/xdk/clients/account_activity_client.rb +109 -0
  6. data/lib/xdk/clients/account_client.rb +53 -0
  7. data/lib/xdk/clients/activity_client.rb +119 -0
  8. data/lib/xdk/clients/articles_client.rb +54 -0
  9. data/lib/xdk/clients/bots_client.rb +138 -0
  10. data/lib/xdk/clients/broadcasts_client.rb +854 -0
  11. data/lib/xdk/clients/calls_client.rb +255 -0
  12. data/lib/xdk/clients/chat_client.rb +1017 -0
  13. data/lib/xdk/clients/communities_client.rb +69 -0
  14. data/lib/xdk/clients/community_notes_client.rb +147 -0
  15. data/lib/xdk/clients/compliance_client.rb +157 -0
  16. data/lib/xdk/clients/connections_client.rb +102 -0
  17. data/lib/xdk/clients/direct_messages_client.rb +269 -0
  18. data/lib/xdk/clients/general_client.rb +34 -0
  19. data/lib/xdk/clients/lists_client.rb +245 -0
  20. data/lib/xdk/clients/media_client.rb +400 -0
  21. data/lib/xdk/clients/news_client.rb +67 -0
  22. data/lib/xdk/clients/posts_client.rb +596 -0
  23. data/lib/xdk/clients/spaces_client.rb +222 -0
  24. data/lib/xdk/clients/stream_client.rb +690 -0
  25. data/lib/xdk/clients/trends_client.rb +81 -0
  26. data/lib/xdk/clients/usage_client.rb +57 -0
  27. data/lib/xdk/clients/users_client.rb +1280 -0
  28. data/lib/xdk/clients/webhooks_client.rb +164 -0
  29. data/lib/xdk/errors.rb +69 -0
  30. data/lib/xdk/http_client.rb +126 -0
  31. data/lib/xdk/oauth1.rb +183 -0
  32. data/lib/xdk/oauth2_pkce.rb +156 -0
  33. data/lib/xdk/paginator.rb +41 -0
  34. data/lib/xdk/streaming.rb +294 -0
  35. data/lib/xdk/version.rb +9 -0
  36. data/lib/xdk.rb +62 -0
  37. metadata +84 -0
@@ -0,0 +1,41 @@
1
+ # AUTO-GENERATED FILE - DO NOT EDIT
2
+ # This file was automatically generated by the XDK build tool.
3
+ # Any manual changes will be overwritten on the next generation.
4
+ # frozen_string_literal: true
5
+
6
+ module Xdk
7
+ # Cursor-based pagination helper yielding pages lazily via Ruby's Enumerator.
8
+ module Paginator
9
+ module_function
10
+
11
+ # Extract the next pagination token from an X API response Hash.
12
+ def extract_next_token(page)
13
+ return nil unless page.is_a?(Hash)
14
+
15
+ meta = page["meta"]
16
+ return nil unless meta.is_a?(Hash)
17
+
18
+ token = meta["next_token"] || meta["pagination_token"]
19
+ token.nil? || token.to_s.empty? ? nil : token.to_s
20
+ end
21
+
22
+ # Return an Enumerator that fetches pages until next_token is absent or repeats.
23
+ def paginate(initial_token: nil)
24
+ Enumerator.new do |yielder|
25
+ current_token = initial_token
26
+ seen_tokens = {}
27
+
28
+ loop do
29
+ page = yield(current_token)
30
+ yielder << page
31
+
32
+ next_token = extract_next_token(page)
33
+ break if next_token.nil? || seen_tokens[next_token]
34
+
35
+ seen_tokens[next_token] = true
36
+ current_token = next_token
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,294 @@
1
+ # AUTO-GENERATED FILE - DO NOT EDIT
2
+ # This file was automatically generated by the XDK build tool.
3
+ # Any manual changes will be overwritten on the next generation.
4
+ # frozen_string_literal: true
5
+
6
+ require "json"
7
+ require "net/http"
8
+ require "uri"
9
+
10
+ module Xdk
11
+ # Classification of streaming errors for retry decisions.
12
+ module StreamErrorType
13
+ CONNECTION_ERROR = :connection_error
14
+ TIMEOUT = :timeout
15
+ SERVER_ERROR = :server_error
16
+ RATE_LIMITED = :rate_limited
17
+ STREAM_INTERRUPTED = :stream_interrupted
18
+ AUTHENTICATION_ERROR = :authentication_error
19
+ CLIENT_ERROR = :client_error
20
+ FATAL_ERROR = :fatal_error
21
+
22
+ RETRYABLE = [
23
+ CONNECTION_ERROR,
24
+ TIMEOUT,
25
+ SERVER_ERROR,
26
+ RATE_LIMITED,
27
+ STREAM_INTERRUPTED
28
+ ].freeze
29
+ end
30
+
31
+ # Raised for streaming failures with retryability classification.
32
+ class StreamError < Error
33
+ attr_reader :error_type, :original_exception, :status_code, :response_body
34
+
35
+ def initialize(message, error_type:, original_exception: nil, status_code: nil, response_body: nil)
36
+ super(message)
37
+ @error_type = error_type
38
+ @original_exception = original_exception
39
+ @status_code = status_code
40
+ @response_body = response_body
41
+ end
42
+
43
+ def retryable?
44
+ StreamErrorType::RETRYABLE.include?(@error_type)
45
+ end
46
+ end
47
+
48
+ # Raised when no data or keep-alive heartbeat arrives within idle_timeout.
49
+ class StreamIdleTimeoutError < StreamError
50
+ attr_reader :idle_timeout
51
+
52
+ def initialize(idle_timeout, original_exception: nil)
53
+ super(
54
+ "No data or keep-alive received for #{idle_timeout}s",
55
+ error_type: StreamErrorType::TIMEOUT,
56
+ original_exception: original_exception
57
+ )
58
+ @idle_timeout = idle_timeout
59
+ end
60
+ end
61
+
62
+ # Configuration for streaming connections with automatic reconnection and
63
+ # keep-alive / idle-timeout recovery.
64
+ class StreamConfig
65
+ attr_accessor :max_retries, :initial_backoff, :max_backoff, :backoff_multiplier,
66
+ :jitter, :timeout, :idle_timeout, :reconnect_on_close,
67
+ :on_connect, :on_disconnect, :on_reconnect, :on_error, :on_keep_alive
68
+
69
+ def initialize(
70
+ max_retries: 10,
71
+ initial_backoff: 1.0,
72
+ max_backoff: 64.0,
73
+ backoff_multiplier: 2.0,
74
+ jitter: true,
75
+ timeout: nil,
76
+ idle_timeout: nil,
77
+ reconnect_on_close: true,
78
+ on_connect: nil,
79
+ on_disconnect: nil,
80
+ on_reconnect: nil,
81
+ on_error: nil,
82
+ on_keep_alive: nil
83
+ )
84
+ @max_retries = max_retries
85
+ @initial_backoff = initial_backoff.to_f
86
+ @max_backoff = max_backoff.to_f
87
+ @backoff_multiplier = backoff_multiplier.to_f
88
+ @jitter = jitter
89
+ @timeout = timeout
90
+ @idle_timeout = idle_timeout
91
+ @reconnect_on_close = reconnect_on_close
92
+ @on_connect = on_connect
93
+ @on_disconnect = on_disconnect
94
+ @on_reconnect = on_reconnect
95
+ @on_error = on_error
96
+ @on_keep_alive = on_keep_alive
97
+ end
98
+ end
99
+
100
+ # Streaming connection runner with NDJSON parsing, heartbeat tracking, and
101
+ # exponential backoff reconnection.
102
+ module Streaming
103
+ module_function
104
+
105
+ def stream_with_retry(method:, url:, query: nil, headers_factory:, body: nil, config: nil, &block)
106
+ return enum_for(__method__, method: method, url: url, query: query, headers_factory: headers_factory, body: body, config: config) unless block_given?
107
+
108
+ cfg = config || StreamConfig.new
109
+ full_url = append_query(url, query)
110
+ retry_count = 0
111
+
112
+ loop do
113
+ begin
114
+ connected = false
115
+ stream_once(
116
+ method: method,
117
+ url: full_url,
118
+ headers: headers_factory.call,
119
+ body: body,
120
+ config: cfg,
121
+ on_connected: lambda do
122
+ connected = true
123
+ cfg.on_connect&.call
124
+ end,
125
+ on_activity: -> { retry_count = 0 },
126
+ &block
127
+ )
128
+ cfg.on_disconnect&.call(nil) if connected
129
+ break unless cfg.reconnect_on_close && can_retry?(cfg, retry_count)
130
+
131
+ retry_count += 1
132
+ delay = backoff_delay(cfg, retry_count)
133
+ cfg.on_reconnect&.call(retry_count, delay)
134
+ sleep(delay) if delay > 0
135
+ rescue StreamError => e
136
+ cfg.on_disconnect&.call(e)
137
+ cfg.on_error&.call(e)
138
+ raise unless e.retryable? && can_retry?(cfg, retry_count)
139
+
140
+ retry_count += 1
141
+ delay = backoff_delay(cfg, retry_count)
142
+ cfg.on_reconnect&.call(retry_count, delay)
143
+ sleep(delay) if delay > 0
144
+ rescue Net::ReadTimeout => e
145
+ err = cfg.idle_timeout ? StreamIdleTimeoutError.new(cfg.idle_timeout, original_exception: e) : StreamError.new(e.message, error_type: StreamErrorType::TIMEOUT, original_exception: e)
146
+ cfg.on_disconnect&.call(err)
147
+ cfg.on_error&.call(err)
148
+ raise err unless can_retry?(cfg, retry_count)
149
+
150
+ retry_count += 1
151
+ delay = backoff_delay(cfg, retry_count)
152
+ cfg.on_reconnect&.call(retry_count, delay)
153
+ sleep(delay) if delay > 0
154
+ rescue Net::OpenTimeout, Errno::ETIMEDOUT => e
155
+ err = StreamError.new(e.message, error_type: StreamErrorType::TIMEOUT, original_exception: e)
156
+ cfg.on_disconnect&.call(err)
157
+ cfg.on_error&.call(err)
158
+ raise err unless can_retry?(cfg, retry_count)
159
+
160
+ retry_count += 1
161
+ delay = backoff_delay(cfg, retry_count)
162
+ cfg.on_reconnect&.call(retry_count, delay)
163
+ sleep(delay) if delay > 0
164
+ rescue IOError, SystemCallError, OpenSSL::SSL::SSLError, EOFError, Net::ProtocolError => e
165
+ err = StreamError.new(e.message, error_type: StreamErrorType::CONNECTION_ERROR, original_exception: e)
166
+ cfg.on_disconnect&.call(err)
167
+ cfg.on_error&.call(err)
168
+ raise err unless can_retry?(cfg, retry_count)
169
+
170
+ retry_count += 1
171
+ delay = backoff_delay(cfg, retry_count)
172
+ cfg.on_reconnect&.call(retry_count, delay)
173
+ sleep(delay) if delay > 0
174
+ end
175
+ end
176
+ end
177
+
178
+ def stream_once(method:, url:, headers:, body:, config:, on_connected:, on_activity:)
179
+ uri = URI.parse(url)
180
+ http = Net::HTTP.new(uri.host, uri.port)
181
+ http.use_ssl = uri.scheme == "https"
182
+ http.open_timeout = config.timeout if config.timeout
183
+ read_timeout = config.idle_timeout || config.timeout
184
+ http.read_timeout = read_timeout if read_timeout
185
+
186
+ req = build_request(method, uri, headers, body)
187
+ http.start do |conn|
188
+ conn.request(req) do |res|
189
+ status = res.code.to_i
190
+ unless status.between?(200, 299)
191
+ resp_body = res.body.to_s
192
+ raise classify_http_error(status, resp_body)
193
+ end
194
+
195
+ on_connected.call
196
+ buffer = +""
197
+ res.read_body do |chunk|
198
+ next if chunk.nil? || chunk.empty?
199
+
200
+ buffer << chunk.dup.force_encoding(Encoding::UTF_8)
201
+ while (idx = buffer.index("\n"))
202
+ line = buffer.slice!(0..idx).strip
203
+ if line.empty?
204
+ on_activity.call
205
+ config.on_keep_alive&.call
206
+ next
207
+ end
208
+ begin
209
+ parsed = JSON.parse(line)
210
+ rescue JSON::ParserError
211
+ next
212
+ end
213
+ if parsed.is_a?(Hash) && parsed.empty?
214
+ on_activity.call
215
+ config.on_keep_alive&.call
216
+ next
217
+ end
218
+ on_activity.call
219
+ yield(parsed)
220
+ end
221
+ end
222
+
223
+ trailing = buffer.strip
224
+ unless trailing.empty?
225
+ begin
226
+ parsed = JSON.parse(trailing)
227
+ if parsed.is_a?(Hash) && parsed.empty?
228
+ on_activity.call
229
+ config.on_keep_alive&.call
230
+ else
231
+ on_activity.call
232
+ yield(parsed)
233
+ end
234
+ rescue JSON::ParserError
235
+ # Ignore incomplete trailing fragment at EOF
236
+ end
237
+ end
238
+ end
239
+ end
240
+ end
241
+
242
+ def build_request(method, uri, headers, body)
243
+ klass = case method.to_s.upcase
244
+ when "GET" then Net::HTTP::Get
245
+ when "POST" then Net::HTTP::Post
246
+ when "PUT" then Net::HTTP::Put
247
+ when "DELETE" then Net::HTTP::Delete
248
+ when "PATCH" then Net::HTTP::Patch
249
+ else Net::HTTP::Get
250
+ end
251
+ req = klass.new(uri.request_uri)
252
+ req["User-Agent"] = Xdk::USER_AGENT
253
+ req["Accept"] = "application/json"
254
+ headers.each { |k, v| req[k] = v.to_s unless v.nil? }
255
+ req.body = body unless body.nil?
256
+ req
257
+ end
258
+
259
+ def classify_http_error(status, body)
260
+ type = case status
261
+ when 401, 403 then StreamErrorType::AUTHENTICATION_ERROR
262
+ when 429 then StreamErrorType::RATE_LIMITED
263
+ when 500..599 then StreamErrorType::SERVER_ERROR
264
+ when 400..499 then StreamErrorType::CLIENT_ERROR
265
+ else StreamErrorType::FATAL_ERROR
266
+ end
267
+ StreamError.new(
268
+ "Streaming HTTP #{status}: #{body[0, 300]}",
269
+ error_type: type,
270
+ status_code: status,
271
+ response_body: body
272
+ )
273
+ end
274
+
275
+ def can_retry?(config, retry_count)
276
+ config.max_retries < 0 || retry_count < config.max_retries
277
+ end
278
+
279
+ def backoff_delay(config, attempt)
280
+ base = config.initial_backoff * (config.backoff_multiplier**(attempt - 1))
281
+ capped = [base, config.max_backoff].min
282
+ config.jitter ? capped * (0.5 + (rand * 0.5)) : capped
283
+ end
284
+
285
+ def append_query(url, query)
286
+ return url if query.nil? || query.empty?
287
+
288
+ qs = URI.encode_www_form(query)
289
+ return url if qs.empty?
290
+
291
+ url.include?("?") ? "#{url}&#{qs}" : "#{url}?#{qs}"
292
+ end
293
+ end
294
+ end
@@ -0,0 +1,9 @@
1
+ # AUTO-GENERATED FILE - DO NOT EDIT
2
+ # This file was automatically generated by the XDK build tool.
3
+ # Any manual changes will be overwritten on the next generation.
4
+ # frozen_string_literal: true
5
+
6
+ module Xdk
7
+ VERSION = "0.1.0"
8
+ USER_AGENT = "xdk-ruby/0.1.0"
9
+ end
data/lib/xdk.rb ADDED
@@ -0,0 +1,62 @@
1
+ # AUTO-GENERATED FILE - DO NOT EDIT
2
+ # This file was automatically generated by the XDK build tool.
3
+ # Any manual changes will be overwritten on the next generation.
4
+ # frozen_string_literal: true
5
+
6
+ require_relative "xdk/version"
7
+ require_relative "xdk/errors"
8
+ require_relative "xdk/http_client"
9
+ require_relative "xdk/oauth1"
10
+ require_relative "xdk/oauth2_pkce"
11
+ require_relative "xdk/paginator"
12
+ require_relative "xdk/streaming"
13
+
14
+ require_relative "xdk/clients/bots_client"
15
+
16
+ require_relative "xdk/clients/connections_client"
17
+
18
+ require_relative "xdk/clients/usage_client"
19
+
20
+ require_relative "xdk/clients/trends_client"
21
+
22
+ require_relative "xdk/clients/communities_client"
23
+
24
+ require_relative "xdk/clients/spaces_client"
25
+
26
+ require_relative "xdk/clients/calls_client"
27
+
28
+ require_relative "xdk/clients/community_notes_client"
29
+
30
+ require_relative "xdk/clients/webhooks_client"
31
+
32
+ require_relative "xdk/clients/lists_client"
33
+
34
+ require_relative "xdk/clients/broadcasts_client"
35
+
36
+ require_relative "xdk/clients/media_client"
37
+
38
+ require_relative "xdk/clients/account_activity_client"
39
+
40
+ require_relative "xdk/clients/activity_client"
41
+
42
+ require_relative "xdk/clients/users_client"
43
+
44
+ require_relative "xdk/clients/chat_client"
45
+
46
+ require_relative "xdk/clients/account_client"
47
+
48
+ require_relative "xdk/clients/direct_messages_client"
49
+
50
+ require_relative "xdk/clients/general_client"
51
+
52
+ require_relative "xdk/clients/posts_client"
53
+
54
+ require_relative "xdk/clients/stream_client"
55
+
56
+ require_relative "xdk/clients/compliance_client"
57
+
58
+ require_relative "xdk/clients/articles_client"
59
+
60
+ require_relative "xdk/clients/news_client"
61
+
62
+ require_relative "xdk/client"
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - X Developer Platform
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-12-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Official auto-generated Ruby SDK for the X API v2. Supports Bearer Token,
15
+ OAuth 2.0 PKCE, and OAuth 1.0a authentication, automatic cursor pagination,
16
+ robust real-time streaming with keep-alive and idle-timeout recovery, and
17
+ zero runtime gem dependencies.
18
+ email:
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - LICENSE
24
+ - README.md
25
+ - lib/xdk.rb
26
+ - lib/xdk/client.rb
27
+ - lib/xdk/clients/account_activity_client.rb
28
+ - lib/xdk/clients/account_client.rb
29
+ - lib/xdk/clients/activity_client.rb
30
+ - lib/xdk/clients/articles_client.rb
31
+ - lib/xdk/clients/bots_client.rb
32
+ - lib/xdk/clients/broadcasts_client.rb
33
+ - lib/xdk/clients/calls_client.rb
34
+ - lib/xdk/clients/chat_client.rb
35
+ - lib/xdk/clients/communities_client.rb
36
+ - lib/xdk/clients/community_notes_client.rb
37
+ - lib/xdk/clients/compliance_client.rb
38
+ - lib/xdk/clients/connections_client.rb
39
+ - lib/xdk/clients/direct_messages_client.rb
40
+ - lib/xdk/clients/general_client.rb
41
+ - lib/xdk/clients/lists_client.rb
42
+ - lib/xdk/clients/media_client.rb
43
+ - lib/xdk/clients/news_client.rb
44
+ - lib/xdk/clients/posts_client.rb
45
+ - lib/xdk/clients/spaces_client.rb
46
+ - lib/xdk/clients/stream_client.rb
47
+ - lib/xdk/clients/trends_client.rb
48
+ - lib/xdk/clients/usage_client.rb
49
+ - lib/xdk/clients/users_client.rb
50
+ - lib/xdk/clients/webhooks_client.rb
51
+ - lib/xdk/errors.rb
52
+ - lib/xdk/http_client.rb
53
+ - lib/xdk/oauth1.rb
54
+ - lib/xdk/oauth2_pkce.rb
55
+ - lib/xdk/paginator.rb
56
+ - lib/xdk/streaming.rb
57
+ - lib/xdk/version.rb
58
+ homepage: https://github.com/xdevplatform/xdk-ruby
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ source_code_uri: https://github.com/xdevplatform/xdk-ruby
63
+ documentation_uri: https://docs.x.com/xdks/ruby/overview
64
+ bug_tracker_uri: https://github.com/xdevplatform/xdk/issues
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '3.0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.5.22
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Official Ruby SDK for the X API v2
84
+ test_files: []