-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse.rb
More file actions
169 lines (147 loc) · 4.58 KB
/
response.rb
File metadata and controls
169 lines (147 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# frozen_string_literal: true
unless defined?(:Wreq)
module Wreq
# HTTP response object containing status, headers, and body.
#
# This class wraps a native Rust implementation providing efficient
# access to HTTP response data including status codes, headers, body
# content, and streaming capabilities.
#
# @example Basic response handling
# response = client.get("https://api.example.com")
# puts response.status.as_int # => 200
# puts response.text
#
# @example JSON response
# response = client.get("https://api.example.com/data")
# data = response.json
#
# @example Streaming response
# response = client.get("https://example.com/large-file")
# response.stream.each do |chunk|
# # Process chunk
# end
class Response
# Get the HTTP status code as an integer.
#
# @return [Integer] Status code (e.g., 200, 404, 500)
# @example
# response.code # => 200
def code; end
# Get the HTTP status code object.
#
# @return [Wreq::StatusCode] Status code wrapper with helper methods
# @example
# status = response.status
# status.success? # => true
def status; end
# Get the HTTP protocol version used.
#
# @return [Wreq::Version] HTTP version (HTTP/1.1, HTTP/2, etc.)
# @example
# response.version # => Wreq::Version::HTTP_11
def version; end
# Get the final URL after redirects.
#
# @return [String] The final URL
# @example
# response.url # => "https://example.com/final-page"
def url; end
# Get the content length if known.
#
# @return [Integer, nil] Content length in bytes, or nil if unknown
# @example
# response.content_length # => 1024
def content_length; end
# Get the local socket address.
#
# @return [String, nil] Local address (e.g., "127.0.0.1:54321"), or nil
# @example
# response.local_addr # => "192.168.1.100:54321"
def local_addr; end
# Get the remote socket address.
#
# @return [String, nil] Remote address (e.g., "93.184.216.34:443"), or nil
# @example
# response.remote_addr # => "93.184.216.34:443"
def remote_addr; end
# Get the response body as text.
#
# @return [String] Response body decoded as UTF-8 text
# @example
# html = response.text
# puts html
def text; end
# Parse the response body as JSON.
#
# @return [Object] Parsed JSON (Hash, Array, String, Integer, Float, Boolean, nil)
# @raise [Wreq::DecodingError] if body is not valid JSON
# @example
# data = response.json
# puts data["key"]
def json; end
# Get a streaming iterator for the response body.
#
# Allows processing large responses in chunks without loading
# the entire body into memory.
#
# @return [Wreq::Streamer] Chunk iterator
# @example
# streamer = response.stream
# streamer.each do |chunk|
# process_chunk(chunk)
# end
def stream; end
# Close the response and free associated resources.
#
# @return [void]
# @example
# response.close
def close; end
end
end
end
# ======================== Ruby API Extensions ========================
module Wreq
class Response
# Returns a compact string representation of the response.
#
# Format: #<Wreq::Response STATUS content-type="..." body=SIZE>
#
# @return [String] Compact formatted response information
# @example
# puts response.to_s
# # => #<Wreq::Response 200 content-type="application/json" body=456B>
def to_s
parts = ["#<Wreq::Response"]
# Status code
parts << code.to_s
# Content-Type header if present
if headers.respond_to?(:get)
content_type = headers.get("content-type")
parts << "content-type=#{content_type.inspect}" if content_type
end
# Body size
if content_length
parts << "body=#{format_bytes(content_length)}"
end
parts.join(" ") + ">"
end
private
def format_bytes(bytes)
return "0B" if bytes.zero?
units = ["B", "KB", "MB", "GB"]
size = bytes.to_f
unit_index = 0
while size >= 1024 && unit_index < units.length - 1
size /= 1024.0
unit_index += 1
end
if unit_index == 0
"#{size.to_i}#{units[unit_index]}"
else
"#{size.round(1)}#{units[unit_index]}"
end
end
end
end