spikard 0.3.5 → 0.3.6

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 (102) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +659 -659
  4. data/ext/spikard_rb/Cargo.toml +17 -17
  5. data/ext/spikard_rb/extconf.rb +10 -10
  6. data/ext/spikard_rb/src/lib.rs +6 -6
  7. data/lib/spikard/app.rb +386 -386
  8. data/lib/spikard/background.rb +27 -27
  9. data/lib/spikard/config.rb +396 -396
  10. data/lib/spikard/converters.rb +13 -13
  11. data/lib/spikard/handler_wrapper.rb +113 -113
  12. data/lib/spikard/provide.rb +214 -214
  13. data/lib/spikard/response.rb +173 -173
  14. data/lib/spikard/schema.rb +243 -243
  15. data/lib/spikard/sse.rb +111 -111
  16. data/lib/spikard/streaming_response.rb +44 -44
  17. data/lib/spikard/testing.rb +221 -221
  18. data/lib/spikard/upload_file.rb +131 -131
  19. data/lib/spikard/version.rb +5 -5
  20. data/lib/spikard/websocket.rb +59 -59
  21. data/lib/spikard.rb +43 -43
  22. data/sig/spikard.rbs +366 -360
  23. data/vendor/crates/spikard-core/Cargo.toml +40 -40
  24. data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
  25. data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
  26. data/vendor/crates/spikard-core/src/debug.rs +63 -63
  27. data/vendor/crates/spikard-core/src/di/container.rs +726 -726
  28. data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
  29. data/vendor/crates/spikard-core/src/di/error.rs +118 -118
  30. data/vendor/crates/spikard-core/src/di/factory.rs +538 -538
  31. data/vendor/crates/spikard-core/src/di/graph.rs +545 -545
  32. data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
  33. data/vendor/crates/spikard-core/src/di/resolved.rs +411 -411
  34. data/vendor/crates/spikard-core/src/di/value.rs +283 -283
  35. data/vendor/crates/spikard-core/src/errors.rs +39 -39
  36. data/vendor/crates/spikard-core/src/http.rs +153 -153
  37. data/vendor/crates/spikard-core/src/lib.rs +29 -29
  38. data/vendor/crates/spikard-core/src/lifecycle.rs +422 -422
  39. data/vendor/crates/spikard-core/src/parameters.rs +722 -722
  40. data/vendor/crates/spikard-core/src/problem.rs +310 -310
  41. data/vendor/crates/spikard-core/src/request_data.rs +189 -189
  42. data/vendor/crates/spikard-core/src/router.rs +249 -249
  43. data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
  44. data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
  45. data/vendor/crates/spikard-core/src/validation.rs +699 -699
  46. data/vendor/crates/spikard-http/Cargo.toml +68 -68
  47. data/vendor/crates/spikard-http/src/auth.rs +247 -247
  48. data/vendor/crates/spikard-http/src/background.rs +249 -249
  49. data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
  50. data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
  51. data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
  52. data/vendor/crates/spikard-http/src/cors.rs +490 -490
  53. data/vendor/crates/spikard-http/src/debug.rs +63 -63
  54. data/vendor/crates/spikard-http/src/di_handler.rs +423 -423
  55. data/vendor/crates/spikard-http/src/handler_response.rs +190 -190
  56. data/vendor/crates/spikard-http/src/handler_trait.rs +228 -228
  57. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -284
  58. data/vendor/crates/spikard-http/src/lib.rs +529 -529
  59. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -149
  60. data/vendor/crates/spikard-http/src/lifecycle.rs +428 -428
  61. data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -285
  62. data/vendor/crates/spikard-http/src/middleware/multipart.rs +86 -86
  63. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +147 -147
  64. data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -287
  65. data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
  66. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +190 -190
  67. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +308 -308
  68. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +195 -195
  69. data/vendor/crates/spikard-http/src/parameters.rs +1 -1
  70. data/vendor/crates/spikard-http/src/problem.rs +1 -1
  71. data/vendor/crates/spikard-http/src/query_parser.rs +369 -369
  72. data/vendor/crates/spikard-http/src/response.rs +399 -399
  73. data/vendor/crates/spikard-http/src/router.rs +1 -1
  74. data/vendor/crates/spikard-http/src/schema_registry.rs +1 -1
  75. data/vendor/crates/spikard-http/src/server/handler.rs +87 -87
  76. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -98
  77. data/vendor/crates/spikard-http/src/server/mod.rs +805 -805
  78. data/vendor/crates/spikard-http/src/server/request_extraction.rs +119 -119
  79. data/vendor/crates/spikard-http/src/sse.rs +447 -447
  80. data/vendor/crates/spikard-http/src/testing/form.rs +14 -14
  81. data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -60
  82. data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -285
  83. data/vendor/crates/spikard-http/src/testing.rs +377 -377
  84. data/vendor/crates/spikard-http/src/type_hints.rs +1 -1
  85. data/vendor/crates/spikard-http/src/validation.rs +1 -1
  86. data/vendor/crates/spikard-http/src/websocket.rs +324 -324
  87. data/vendor/crates/spikard-rb/Cargo.toml +42 -42
  88. data/vendor/crates/spikard-rb/build.rs +8 -8
  89. data/vendor/crates/spikard-rb/src/background.rs +63 -63
  90. data/vendor/crates/spikard-rb/src/config.rs +294 -294
  91. data/vendor/crates/spikard-rb/src/conversion.rs +453 -453
  92. data/vendor/crates/spikard-rb/src/di.rs +409 -409
  93. data/vendor/crates/spikard-rb/src/handler.rs +625 -625
  94. data/vendor/crates/spikard-rb/src/lib.rs +2771 -2771
  95. data/vendor/crates/spikard-rb/src/lifecycle.rs +274 -274
  96. data/vendor/crates/spikard-rb/src/server.rs +283 -283
  97. data/vendor/crates/spikard-rb/src/sse.rs +231 -231
  98. data/vendor/crates/spikard-rb/src/test_client.rs +404 -404
  99. data/vendor/crates/spikard-rb/src/test_sse.rs +143 -143
  100. data/vendor/crates/spikard-rb/src/test_websocket.rs +221 -221
  101. data/vendor/crates/spikard-rb/src/websocket.rs +233 -233
  102. metadata +1 -1
@@ -1,231 +1,231 @@
1
- //! Ruby SSE producer bindings
2
- //!
3
- //! This module provides the bridge between Ruby blocks/procs and Rust's SSE system.
4
- //! Uses magnus to safely call Ruby code from Rust async tasks.
5
-
6
- use magnus::{RHash, Value, prelude::*, value::Opaque};
7
- use serde_json::Value as JsonValue;
8
- use spikard_http::{SseEvent, SseEventProducer};
9
- use tracing::{debug, error};
10
-
11
- /// Ruby implementation of SseEventProducer
12
- pub struct RubySseEventProducer {
13
- /// Producer name for debugging
14
- name: String,
15
- /// Ruby proc/callable for next_event (Opaque for Send safety)
16
- next_event_proc: Opaque<Value>,
17
- /// Ruby proc/callable for on_connect (Opaque for Send safety)
18
- on_connect_proc: Option<Opaque<Value>>,
19
- /// Ruby proc/callable for on_disconnect (Opaque for Send safety)
20
- on_disconnect_proc: Option<Opaque<Value>>,
21
- }
22
-
23
- impl RubySseEventProducer {
24
- /// Create a new Ruby SSE event producer
25
- #[allow(dead_code)]
26
- pub fn new(
27
- name: String,
28
- next_event_proc: Value,
29
- on_connect_proc: Option<Value>,
30
- on_disconnect_proc: Option<Value>,
31
- ) -> Self {
32
- Self {
33
- name,
34
- next_event_proc: next_event_proc.into(),
35
- on_connect_proc: on_connect_proc.map(|v| v.into()),
36
- on_disconnect_proc: on_disconnect_proc.map(|v| v.into()),
37
- }
38
- }
39
-
40
- /// Convert Ruby value to JSON
41
- fn ruby_to_json(ruby: &magnus::Ruby, value: Value) -> Result<JsonValue, String> {
42
- if value.is_nil() {
43
- return Ok(JsonValue::Null);
44
- }
45
-
46
- let json_module: Value = ruby
47
- .class_object()
48
- .const_get("JSON")
49
- .map_err(|e| format!("JSON module not available: {}", e))?;
50
-
51
- let json_str: String = json_module
52
- .funcall("generate", (value,))
53
- .map_err(|e| format!("Failed to generate JSON: {}", e))?;
54
-
55
- serde_json::from_str(&json_str).map_err(|e| format!("Failed to parse JSON: {}", e))
56
- }
57
- }
58
-
59
- impl SseEventProducer for RubySseEventProducer {
60
- async fn next_event(&self) -> Option<SseEvent> {
61
- debug!("Ruby SSE producer '{}': next_event", self.name);
62
-
63
- match magnus::Ruby::get()
64
- .map_err(|e| format!("Failed to get Ruby: {}", e))
65
- .and_then(|ruby| {
66
- debug!("Ruby SSE producer: acquired Ruby VM");
67
-
68
- let proc_value = ruby.get_inner(self.next_event_proc);
69
- let result: Value = proc_value
70
- .funcall("call", ())
71
- .map_err(|e| format!("Producer '{}' call failed: {}", self.name, e))?;
72
-
73
- debug!("Ruby SSE producer: called next_event proc");
74
-
75
- if result.is_nil() {
76
- debug!("Ruby SSE producer: received nil, ending stream");
77
- return Ok(None);
78
- }
79
-
80
- let result_hash = if let Some(hash) = RHash::from_value(result) {
81
- hash
82
- } else {
83
- let hash_value: Value = result.funcall("to_h", ()).map_err(|e| {
84
- format!(
85
- "next_event must return a Hash/SseEvent convertible via to_h ({}): {}",
86
- unsafe { result.classname() },
87
- e
88
- )
89
- })?;
90
- RHash::from_value(hash_value).ok_or_else(|| {
91
- format!("next_event to_h must return a Hash, got {}", unsafe {
92
- hash_value.classname()
93
- })
94
- })?
95
- };
96
-
97
- let data_value = result_hash
98
- .get(ruby.to_symbol("data"))
99
- .ok_or_else(|| "next_event Hash must have :data key".to_string())?;
100
-
101
- let data_json = Self::ruby_to_json(&ruby, data_value)?;
102
-
103
- let event_type: Option<String> = result_hash
104
- .get(ruby.to_symbol("event_type"))
105
- .and_then(|v| if v.is_nil() { None } else { String::try_convert(v).ok() });
106
-
107
- let id: Option<String> = result_hash
108
- .get(ruby.to_symbol("id"))
109
- .and_then(|v| if v.is_nil() { None } else { String::try_convert(v).ok() });
110
-
111
- let retry: Option<u64> = result_hash
112
- .get(ruby.to_symbol("retry"))
113
- .and_then(|v| if v.is_nil() { None } else { u64::try_convert(v).ok() });
114
-
115
- let mut event = if let Some(et) = event_type {
116
- SseEvent::with_type(et, data_json)
117
- } else {
118
- SseEvent::new(data_json)
119
- };
120
-
121
- if let Some(id_str) = id {
122
- event = event.with_id(id_str);
123
- }
124
-
125
- if let Some(retry_ms) = retry {
126
- event = event.with_retry(retry_ms);
127
- }
128
-
129
- Ok(Some(event))
130
- }) {
131
- Ok(event) => event,
132
- Err(e) => {
133
- error!("Ruby error in next_event: {}", e);
134
- None
135
- }
136
- }
137
- }
138
-
139
- async fn on_connect(&self) {
140
- debug!("Ruby SSE producer '{}': on_connect", self.name);
141
-
142
- if let Some(on_connect_proc) = self.on_connect_proc
143
- && let Err(e) = magnus::Ruby::get()
144
- .map_err(|e| format!("Failed to get Ruby: {}", e))
145
- .and_then(|ruby| {
146
- debug!("Ruby SSE producer: on_connect acquired Ruby VM");
147
- let proc_value = ruby.get_inner(on_connect_proc);
148
- proc_value
149
- .funcall::<_, _, Value>("call", ())
150
- .map_err(|e| format!("on_connect '{}' call failed: {}", self.name, e))?;
151
- debug!("Ruby SSE producer: on_connect completed");
152
- Ok(())
153
- })
154
- {
155
- error!("on_connect error: {}", e);
156
- }
157
- }
158
-
159
- async fn on_disconnect(&self) {
160
- debug!("Ruby SSE producer '{}': on_disconnect", self.name);
161
-
162
- if let Some(on_disconnect_proc) = self.on_disconnect_proc
163
- && let Err(e) = magnus::Ruby::get()
164
- .map_err(|e| format!("Failed to get Ruby: {}", e))
165
- .and_then(|ruby| {
166
- let proc_value = ruby.get_inner(on_disconnect_proc);
167
- proc_value
168
- .funcall::<_, _, Value>("call", ())
169
- .map_err(|e| format!("on_disconnect '{}' call failed: {}", self.name, e))?;
170
- debug!("Ruby SSE producer: on_disconnect completed");
171
- Ok(())
172
- })
173
- {
174
- error!("on_disconnect error: {}", e);
175
- }
176
- }
177
- }
178
-
179
- unsafe impl Send for RubySseEventProducer {}
180
- unsafe impl Sync for RubySseEventProducer {}
181
-
182
- /// Create SseState from Ruby producer object
183
- ///
184
- /// This function is designed to be called from Ruby to register SSE producers.
185
- #[allow(dead_code)]
186
- pub fn create_sse_state(
187
- ruby: &magnus::Ruby,
188
- producer_obj: Value,
189
- ) -> Result<spikard_http::SseState<RubySseEventProducer>, magnus::Error> {
190
- let next_event_proc: Value = producer_obj
191
- .funcall("method", (ruby.to_symbol("next_event"),))
192
- .map_err(|e| {
193
- magnus::Error::new(
194
- ruby.exception_arg_error(),
195
- format!("next_event method not found: {}", e),
196
- )
197
- })?;
198
-
199
- let on_connect_proc = producer_obj
200
- .funcall::<_, _, Value>("method", (ruby.to_symbol("on_connect"),))
201
- .ok();
202
-
203
- let on_disconnect_proc = producer_obj
204
- .funcall::<_, _, Value>("method", (ruby.to_symbol("on_disconnect"),))
205
- .ok();
206
-
207
- let event_schema = producer_obj
208
- .funcall::<_, _, Value>("instance_variable_get", (ruby.to_symbol("@_event_schema"),))
209
- .ok()
210
- .and_then(|v| {
211
- if v.is_nil() {
212
- None
213
- } else {
214
- RubySseEventProducer::ruby_to_json(ruby, v).ok()
215
- }
216
- });
217
-
218
- let ruby_producer = RubySseEventProducer::new(
219
- "SseEventProducer".to_string(),
220
- next_event_proc,
221
- on_connect_proc,
222
- on_disconnect_proc,
223
- );
224
-
225
- if event_schema.is_some() {
226
- spikard_http::SseState::with_schema(ruby_producer, event_schema)
227
- .map_err(|e| magnus::Error::new(ruby.exception_runtime_error(), e))
228
- } else {
229
- Ok(spikard_http::SseState::new(ruby_producer))
230
- }
231
- }
1
+ //! Ruby SSE producer bindings
2
+ //!
3
+ //! This module provides the bridge between Ruby blocks/procs and Rust's SSE system.
4
+ //! Uses magnus to safely call Ruby code from Rust async tasks.
5
+
6
+ use magnus::{RHash, Value, prelude::*, value::Opaque};
7
+ use serde_json::Value as JsonValue;
8
+ use spikard_http::{SseEvent, SseEventProducer};
9
+ use tracing::{debug, error};
10
+
11
+ /// Ruby implementation of SseEventProducer
12
+ pub struct RubySseEventProducer {
13
+ /// Producer name for debugging
14
+ name: String,
15
+ /// Ruby proc/callable for next_event (Opaque for Send safety)
16
+ next_event_proc: Opaque<Value>,
17
+ /// Ruby proc/callable for on_connect (Opaque for Send safety)
18
+ on_connect_proc: Option<Opaque<Value>>,
19
+ /// Ruby proc/callable for on_disconnect (Opaque for Send safety)
20
+ on_disconnect_proc: Option<Opaque<Value>>,
21
+ }
22
+
23
+ impl RubySseEventProducer {
24
+ /// Create a new Ruby SSE event producer
25
+ #[allow(dead_code)]
26
+ pub fn new(
27
+ name: String,
28
+ next_event_proc: Value,
29
+ on_connect_proc: Option<Value>,
30
+ on_disconnect_proc: Option<Value>,
31
+ ) -> Self {
32
+ Self {
33
+ name,
34
+ next_event_proc: next_event_proc.into(),
35
+ on_connect_proc: on_connect_proc.map(|v| v.into()),
36
+ on_disconnect_proc: on_disconnect_proc.map(|v| v.into()),
37
+ }
38
+ }
39
+
40
+ /// Convert Ruby value to JSON
41
+ fn ruby_to_json(ruby: &magnus::Ruby, value: Value) -> Result<JsonValue, String> {
42
+ if value.is_nil() {
43
+ return Ok(JsonValue::Null);
44
+ }
45
+
46
+ let json_module: Value = ruby
47
+ .class_object()
48
+ .const_get("JSON")
49
+ .map_err(|e| format!("JSON module not available: {}", e))?;
50
+
51
+ let json_str: String = json_module
52
+ .funcall("generate", (value,))
53
+ .map_err(|e| format!("Failed to generate JSON: {}", e))?;
54
+
55
+ serde_json::from_str(&json_str).map_err(|e| format!("Failed to parse JSON: {}", e))
56
+ }
57
+ }
58
+
59
+ impl SseEventProducer for RubySseEventProducer {
60
+ async fn next_event(&self) -> Option<SseEvent> {
61
+ debug!("Ruby SSE producer '{}': next_event", self.name);
62
+
63
+ match magnus::Ruby::get()
64
+ .map_err(|e| format!("Failed to get Ruby: {}", e))
65
+ .and_then(|ruby| {
66
+ debug!("Ruby SSE producer: acquired Ruby VM");
67
+
68
+ let proc_value = ruby.get_inner(self.next_event_proc);
69
+ let result: Value = proc_value
70
+ .funcall("call", ())
71
+ .map_err(|e| format!("Producer '{}' call failed: {}", self.name, e))?;
72
+
73
+ debug!("Ruby SSE producer: called next_event proc");
74
+
75
+ if result.is_nil() {
76
+ debug!("Ruby SSE producer: received nil, ending stream");
77
+ return Ok(None);
78
+ }
79
+
80
+ let result_hash = if let Some(hash) = RHash::from_value(result) {
81
+ hash
82
+ } else {
83
+ let hash_value: Value = result.funcall("to_h", ()).map_err(|e| {
84
+ format!(
85
+ "next_event must return a Hash/SseEvent convertible via to_h ({}): {}",
86
+ unsafe { result.classname() },
87
+ e
88
+ )
89
+ })?;
90
+ RHash::from_value(hash_value).ok_or_else(|| {
91
+ format!("next_event to_h must return a Hash, got {}", unsafe {
92
+ hash_value.classname()
93
+ })
94
+ })?
95
+ };
96
+
97
+ let data_value = result_hash
98
+ .get(ruby.to_symbol("data"))
99
+ .ok_or_else(|| "next_event Hash must have :data key".to_string())?;
100
+
101
+ let data_json = Self::ruby_to_json(&ruby, data_value)?;
102
+
103
+ let event_type: Option<String> = result_hash
104
+ .get(ruby.to_symbol("event_type"))
105
+ .and_then(|v| if v.is_nil() { None } else { String::try_convert(v).ok() });
106
+
107
+ let id: Option<String> = result_hash
108
+ .get(ruby.to_symbol("id"))
109
+ .and_then(|v| if v.is_nil() { None } else { String::try_convert(v).ok() });
110
+
111
+ let retry: Option<u64> = result_hash
112
+ .get(ruby.to_symbol("retry"))
113
+ .and_then(|v| if v.is_nil() { None } else { u64::try_convert(v).ok() });
114
+
115
+ let mut event = if let Some(et) = event_type {
116
+ SseEvent::with_type(et, data_json)
117
+ } else {
118
+ SseEvent::new(data_json)
119
+ };
120
+
121
+ if let Some(id_str) = id {
122
+ event = event.with_id(id_str);
123
+ }
124
+
125
+ if let Some(retry_ms) = retry {
126
+ event = event.with_retry(retry_ms);
127
+ }
128
+
129
+ Ok(Some(event))
130
+ }) {
131
+ Ok(event) => event,
132
+ Err(e) => {
133
+ error!("Ruby error in next_event: {}", e);
134
+ None
135
+ }
136
+ }
137
+ }
138
+
139
+ async fn on_connect(&self) {
140
+ debug!("Ruby SSE producer '{}': on_connect", self.name);
141
+
142
+ if let Some(on_connect_proc) = self.on_connect_proc
143
+ && let Err(e) = magnus::Ruby::get()
144
+ .map_err(|e| format!("Failed to get Ruby: {}", e))
145
+ .and_then(|ruby| {
146
+ debug!("Ruby SSE producer: on_connect acquired Ruby VM");
147
+ let proc_value = ruby.get_inner(on_connect_proc);
148
+ proc_value
149
+ .funcall::<_, _, Value>("call", ())
150
+ .map_err(|e| format!("on_connect '{}' call failed: {}", self.name, e))?;
151
+ debug!("Ruby SSE producer: on_connect completed");
152
+ Ok(())
153
+ })
154
+ {
155
+ error!("on_connect error: {}", e);
156
+ }
157
+ }
158
+
159
+ async fn on_disconnect(&self) {
160
+ debug!("Ruby SSE producer '{}': on_disconnect", self.name);
161
+
162
+ if let Some(on_disconnect_proc) = self.on_disconnect_proc
163
+ && let Err(e) = magnus::Ruby::get()
164
+ .map_err(|e| format!("Failed to get Ruby: {}", e))
165
+ .and_then(|ruby| {
166
+ let proc_value = ruby.get_inner(on_disconnect_proc);
167
+ proc_value
168
+ .funcall::<_, _, Value>("call", ())
169
+ .map_err(|e| format!("on_disconnect '{}' call failed: {}", self.name, e))?;
170
+ debug!("Ruby SSE producer: on_disconnect completed");
171
+ Ok(())
172
+ })
173
+ {
174
+ error!("on_disconnect error: {}", e);
175
+ }
176
+ }
177
+ }
178
+
179
+ unsafe impl Send for RubySseEventProducer {}
180
+ unsafe impl Sync for RubySseEventProducer {}
181
+
182
+ /// Create SseState from Ruby producer object
183
+ ///
184
+ /// This function is designed to be called from Ruby to register SSE producers.
185
+ #[allow(dead_code)]
186
+ pub fn create_sse_state(
187
+ ruby: &magnus::Ruby,
188
+ producer_obj: Value,
189
+ ) -> Result<spikard_http::SseState<RubySseEventProducer>, magnus::Error> {
190
+ let next_event_proc: Value = producer_obj
191
+ .funcall("method", (ruby.to_symbol("next_event"),))
192
+ .map_err(|e| {
193
+ magnus::Error::new(
194
+ ruby.exception_arg_error(),
195
+ format!("next_event method not found: {}", e),
196
+ )
197
+ })?;
198
+
199
+ let on_connect_proc = producer_obj
200
+ .funcall::<_, _, Value>("method", (ruby.to_symbol("on_connect"),))
201
+ .ok();
202
+
203
+ let on_disconnect_proc = producer_obj
204
+ .funcall::<_, _, Value>("method", (ruby.to_symbol("on_disconnect"),))
205
+ .ok();
206
+
207
+ let event_schema = producer_obj
208
+ .funcall::<_, _, Value>("instance_variable_get", (ruby.to_symbol("@_event_schema"),))
209
+ .ok()
210
+ .and_then(|v| {
211
+ if v.is_nil() {
212
+ None
213
+ } else {
214
+ RubySseEventProducer::ruby_to_json(ruby, v).ok()
215
+ }
216
+ });
217
+
218
+ let ruby_producer = RubySseEventProducer::new(
219
+ "SseEventProducer".to_string(),
220
+ next_event_proc,
221
+ on_connect_proc,
222
+ on_disconnect_proc,
223
+ );
224
+
225
+ if event_schema.is_some() {
226
+ spikard_http::SseState::with_schema(ruby_producer, event_schema)
227
+ .map_err(|e| magnus::Error::new(ruby.exception_runtime_error(), e))
228
+ } else {
229
+ Ok(spikard_http::SseState::new(ruby_producer))
230
+ }
231
+ }