spikard 0.2.1 → 0.3.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.
@@ -1,109 +1,173 @@
1
- # frozen_string_literal: true
2
-
3
- module Spikard
4
- # Response object returned from route handlers.
5
- # Mirrors the Python/Node response helpers so the native layer
6
- # can extract status, headers, and JSON-serialisable content.
7
- class Response
8
- attr_accessor :content
9
- attr_reader :status_code, :headers
10
-
11
- def initialize(content: nil, body: nil, status_code: 200, headers: nil, content_type: nil)
12
- @content = content.nil? ? body : content
13
- self.status_code = status_code
14
- self.headers = headers
15
- set_header('content-type', content_type) if content_type
16
- end
17
-
18
- def status
19
- @status_code
20
- end
21
-
22
- def status_code=(value)
23
- @status_code = Integer(value)
24
- rescue ArgumentError, TypeError
25
- raise ArgumentError, 'status_code must be an integer'
26
- end
27
-
28
- def headers=(value)
29
- @headers = normalize_headers(value)
30
- end
31
-
32
- def set_header(name, value)
33
- @headers[name.to_s] = value.to_s
34
- end
35
-
36
- def set_cookie(name, value, **options)
37
- raise ArgumentError, 'cookie name required' if name.nil? || name.empty?
38
-
39
- header_value = ["#{name}=#{value}", *cookie_parts(options)].join('; ')
40
- set_header('set-cookie', header_value)
41
- end
42
-
43
- private
44
-
45
- def cookie_parts(options)
46
- [
47
- options[:max_age] && "Max-Age=#{Integer(options[:max_age])}",
48
- options[:domain] && "Domain=#{options[:domain]}",
49
- "Path=#{options.fetch(:path, '/') || '/'}",
50
- options[:secure] ? 'Secure' : nil,
51
- options[:httponly] ? 'HttpOnly' : nil,
52
- options[:samesite] && "SameSite=#{options[:samesite]}"
53
- ].compact
54
- end
55
-
56
- def normalize_headers(value)
57
- case value
58
- when nil
59
- {}
60
- when Hash
61
- value.each_with_object({}) do |(key, val), acc|
62
- acc[key.to_s] = val.to_s
63
- end
64
- else
65
- raise ArgumentError, 'headers must be a Hash'
66
- end
67
- end
68
- end
69
-
70
- module Testing
71
- # Lightweight wrapper around native response hashes.
72
- class Response
73
- attr_reader :status_code, :headers, :body
74
-
75
- def initialize(payload)
76
- @status_code = payload[:status_code]
77
- @headers = payload[:headers] || {}
78
- @body = payload[:body]
79
- @body_text = payload[:body_text]
80
- end
81
-
82
- def status
83
- @status_code
84
- end
85
-
86
- def body_bytes
87
- @body || ''.b
88
- end
89
-
90
- def body_text
91
- @body_text || @body&.dup&.force_encoding(Encoding::UTF_8)
92
- end
93
-
94
- def text
95
- body_text
96
- end
97
-
98
- def json
99
- return nil if @body.nil? || @body.empty?
100
-
101
- JSON.parse(@body)
102
- end
103
-
104
- def bytes
105
- body_bytes.bytes
106
- end
107
- end
108
- end
109
- end
1
+ # frozen_string_literal: true
2
+
3
+ # ⚠️ GENERATED BY crates/spikard-rb/build.rs — DO NOT EDIT BY HAND
4
+ module Spikard
5
+ class Response # :nodoc: Native-backed HTTP response facade generated from Rust metadata.
6
+ attr_reader :content, :status_code, :headers, :native_response
7
+
8
+ def initialize(content: nil, body: nil, status_code: 200, headers: nil, content_type: nil)
9
+ @content = content.nil? ? body : content
10
+ @status_code = Integer(status_code || 200)
11
+ @headers = normalize_headers(headers)
12
+ set_header('content-type', content_type) if content_type
13
+ rebuild_native!
14
+ end
15
+
16
+ def status
17
+ @status_code
18
+ end
19
+
20
+ def status_code=(value)
21
+ @status_code = Integer(value)
22
+ rebuild_native!
23
+ rescue ArgumentError, TypeError
24
+ raise ArgumentError, 'status_code must be an integer'
25
+ end
26
+
27
+ def headers=(value)
28
+ @headers = normalize_headers(value)
29
+ rebuild_native!
30
+ end
31
+
32
+ def content=(value)
33
+ @content = value
34
+ rebuild_native!
35
+ end
36
+
37
+ def set_header(name, value)
38
+ @headers[name.to_s] = value.to_s
39
+ rebuild_native!
40
+ end
41
+
42
+ def set_cookie(name, value, **options)
43
+ raise ArgumentError, 'cookie name required' if name.nil? || name.empty?
44
+
45
+ header_value = ["#{name}=#{value}", *cookie_parts(options)].join('; ')
46
+ set_header('set-cookie', header_value)
47
+ end
48
+
49
+ def to_native_response
50
+ @native_response
51
+ end
52
+
53
+ private
54
+
55
+ def rebuild_native!
56
+ ensure_native!
57
+ @native_response = Spikard::Native.build_response(@content, @status_code, @headers)
58
+ return unless @native_response
59
+
60
+ @status_code = @native_response.status_code
61
+ @headers = @native_response.headers
62
+ end
63
+
64
+ def ensure_native!
65
+ return if defined?(Spikard::Native) && Spikard::Native.respond_to?(:build_response)
66
+
67
+ raise 'Spikard native extension is not loaded'
68
+ end
69
+
70
+ def cookie_parts(options)
71
+ [
72
+ options[:max_age] && "Max-Age=#{Integer(options[:max_age])}",
73
+ options[:domain] && "Domain=#{options[:domain]}",
74
+ "Path=#{options.fetch(:path, '/') || '/'}",
75
+ options[:secure] ? 'Secure' : nil,
76
+ options[:httponly] ? 'HttpOnly' : nil,
77
+ options[:samesite] && "SameSite=#{options[:samesite]}"
78
+ ].compact
79
+ end
80
+
81
+ def normalize_headers(value)
82
+ case value
83
+ when nil
84
+ {}
85
+ when Hash
86
+ value.each_with_object({}) do |(key, val), acc|
87
+ acc[key.to_s.downcase] = val.to_s
88
+ end
89
+ else
90
+ raise ArgumentError, 'headers must be a Hash'
91
+ end
92
+ end
93
+ end
94
+
95
+ class StreamingResponse # :nodoc: Streaming response wrapper backed by the native Rust builder.
96
+ attr_reader :stream, :status_code, :headers, :native_response
97
+
98
+ def initialize(stream, status_code: 200, headers: nil)
99
+ unless stream.respond_to?(:next) || stream.respond_to?(:each)
100
+ raise ArgumentError, 'StreamingResponse requires an object responding to #next or #each'
101
+ end
102
+
103
+ @stream = stream.respond_to?(:to_enum) ? stream.to_enum : stream
104
+ @status_code = Integer(status_code || 200)
105
+ header_hash = headers || {}
106
+ @headers = header_hash.each_with_object({}) do |(key, value), memo|
107
+ memo[String(key)] = String(value)
108
+ end
109
+
110
+ rebuild_native!
111
+ end
112
+
113
+ def to_native_response
114
+ @native_response
115
+ end
116
+
117
+ private
118
+
119
+ def rebuild_native!
120
+ ensure_native!
121
+ @native_response = Spikard::Native.build_streaming_response(@stream, @status_code, @headers)
122
+ return unless @native_response
123
+
124
+ @status_code = @native_response.status_code
125
+ @headers = @native_response.headers
126
+ end
127
+
128
+ def ensure_native!
129
+ return if defined?(Spikard::Native) && Spikard::Native.respond_to?(:build_streaming_response)
130
+
131
+ raise 'Spikard native extension is not loaded'
132
+ end
133
+ end
134
+
135
+ module Testing
136
+ class Response # :nodoc: Lightweight response wrapper used by the test client.
137
+ attr_reader :status_code, :headers, :body
138
+
139
+ def initialize(payload)
140
+ @status_code = payload[:status_code]
141
+ @headers = payload[:headers] || {}
142
+ @body = payload[:body]
143
+ @body_text = payload[:body_text]
144
+ end
145
+
146
+ def status
147
+ @status_code
148
+ end
149
+
150
+ def body_bytes
151
+ @body || ''.b
152
+ end
153
+
154
+ def body_text
155
+ @body_text || @body&.dup&.force_encoding(Encoding::UTF_8)
156
+ end
157
+
158
+ def text
159
+ body_text
160
+ end
161
+
162
+ def json
163
+ return nil if @body.nil? || @body.empty?
164
+
165
+ JSON.parse(@body)
166
+ end
167
+
168
+ def bytes
169
+ body_bytes.bytes
170
+ end
171
+ end
172
+ end
173
+ end