Class: Code::Object::Http
Constant Summary
collapse
- SIG =
[
String,
{
headers: Dictionary.maybe,
query: Dictionary.maybe,
body: String.maybe,
username: String.maybe,
password: String.maybe,
data: Dictionary.maybe
}
].freeze
- STATUS_CODES =
{
continue: 100,
switching_protocols: 101,
processing: 102,
early_hints: 103,
ok: 200,
created: 201,
accepted: 202,
non_authoritative_information: 203,
no_content: 204,
reset_content: 205,
partial_content: 206,
multi_status: 207,
already_reported: 208,
im_used: 226,
multiple_choices: 300,
moved_permanently: 301,
found: 302,
see_other: 303,
not_modified: 304,
use_proxy: 305,
reserved: 306,
temporary_redirect: 307,
permanent_redirect: 308,
bad_request: 400,
unauthorized: 401,
payment_required: 402,
forbidden: 403,
not_found: 404,
method_not_allowed: 405,
not_acceptable: 406,
proxy_authentication_required: 407,
request_timeout: 408,
conflict: 409,
gone: 410,
length_required: 411,
precondition_failed: 412,
request_entity_too_large: 413,
request_uri_too_long: 414,
unsupported_media_type: 415,
requested_range_not_satisfiable: 416,
expectation_failed: 417,
misdirected_request: 421,
unprocessable_entity: 422,
locked: 423,
failed_dependency: 424,
too_early: 425,
upgrade_required: 426,
precondition_required: 428,
too_many_requests: 429,
request_header_fields_too_large: 431,
unavailable_for_legal_reasons: 451,
internal_server_error: 500,
not_implemented: 501,
bad_gateway: 502,
service_unavailable: 503,
gateway_timeout: 504,
http_version_not_supported: 505,
variant_also_negotiates: 506,
insufficient_storage: 507,
loop_detected: 508,
bandwidth_limit_exceeded: 509,
not_extended: 510,
network_authentication_required: 511
}.freeze
Constants inherited
from Code::Object
NUMBER_CLASSES
Instance Attribute Summary
Attributes inherited from Code::Object
#raw
Class Method Summary
collapse
#<=>, #==, #as_json, as_json, #call, code_and_operator, #code_and_operator, #code_as_json, code_as_json, code_different, #code_different, code_equal_equal, #code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, #code_self, code_self, #code_to_json, code_to_json, falsy?, #falsy?, #hash, #initialize, inspect, maybe, #multi_fetch, multi_fetch, nothing?, #nothing?, repeat, sig, #sig, #succ, #to_code, to_json, #to_json, to_s, truthy?, #truthy?, |
Constructor Details
This class inherits a constructor from Code::Object
Class Method Details
.call(**args) ⇒ Object
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
|
# File 'lib/code/object/http.rb', line 84
def self.call(**args)
code_operator = args.fetch(:operator, nil).to_code
code_arguments = args.fetch(:arguments, []).to_code
case code_operator.to_s
when "get"
sig(args) { SIG }
code_get(*code_arguments.raw)
when "head"
sig(args) { SIG }
code_head(*code_arguments.raw)
when "post"
sig(args) { SIG }
code_post(*code_arguments.raw)
when "put"
sig(args) { SIG }
code_put(*code_arguments.raw)
when "delete"
sig(args) { SIG }
code_delete(*code_arguments.raw)
when "connect"
sig(args) { SIG }
code_connect(*code_arguments.raw)
when "options"
sig(args) { SIG }
code_options(*code_arguments.raw)
when "trace"
sig(args) { SIG }
code_trace(*code_arguments.raw)
when "patch"
sig(args) { SIG }
code_patch(*code_arguments.raw)
when "fetch"
sig(args) { [String] + SIG }
code_fetch(*code_arguments.raw)
else
super
end
end
|
.code_connect ⇒ Object
144
145
146
|
# File 'lib/code/object/http.rb', line 144
def self.code_connect(...)
code_fetch("connect", ...)
end
|
.code_delete ⇒ Object
140
141
142
|
# File 'lib/code/object/http.rb', line 140
def self.code_delete(...)
code_fetch("delete", ...)
end
|
.code_fetch(*arguments) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/code/object/http.rb', line 160
def self.code_fetch(*arguments)
verb = arguments.first.to_code.to_s.downcase
url = arguments.second.to_code.to_s
options = arguments.third.to_code
options = Dictionary.new if options.nothing?
username = options.code_get("username").to_s
password = options.code_get("password").to_s
body = options.code_get("body").to_s
= options.code_get("headers").raw || {}
data = options.code_get("data").raw || {}
query = options.code_get("query").raw || {}
query = query.to_a.flatten.map(&:to_s).each_slice(2).to_h.to_query
url = "#{url}?#{query}" if query.present?
uri = ::URI.parse(url)
http = ::Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"
if username.present? || password.present?
[
"Authorization"
] = "Basic #{::Base64.strict_encode64("#{username}:#{password}")}"
end
request_class =
case verb
when "get"
::Net::HTTP::Get
when "head"
::Net::HTTP::Head
when "post"
::Net::HTTP::Post
when "put"
::Net::HTTP::Put
when "delete"
::Net::HTTP::Delete
when "connect"
::Net::HTTP::Get
when "options"
::Net::HTTP::Options
when "trace"
::Net::HTTP::Trace
when "patch"
::Net::HTTP::Patch
else
::Net::HTTP::Get
end
request = request_class.new(uri)
.each { |key, value| request[key.to_s] = value.to_s }
request.body = body if body.present?
request.set_form_data(**data.as_json) if data.present?
response = http.request(request)
code = response.code.to_i
status = STATUS_CODES.key(code) || :ok
Dictionary.new(code: code, status: status, body: response.body)
end
|
124
125
126
|
# File 'lib/code/object/http.rb', line 124
def self.code_get(...)
code_fetch("get", ...)
end
|
.code_head ⇒ Object
128
129
130
|
# File 'lib/code/object/http.rb', line 128
def self.code_head(...)
code_fetch("head", ...)
end
|
.code_options ⇒ Object
148
149
150
|
# File 'lib/code/object/http.rb', line 148
def self.code_options(...)
code_fetch("options", ...)
end
|
.code_patch ⇒ Object
156
157
158
|
# File 'lib/code/object/http.rb', line 156
def self.code_patch(...)
code_fetch("patch", ...)
end
|
.code_post ⇒ Object
132
133
134
|
# File 'lib/code/object/http.rb', line 132
def self.code_post(...)
code_fetch("post", ...)
end
|
136
137
138
|
# File 'lib/code/object/http.rb', line 136
def self.code_put(...)
code_fetch("put", ...)
end
|
.code_trace ⇒ Object
152
153
154
|
# File 'lib/code/object/http.rb', line 152
def self.code_trace(...)
code_fetch("trace", ...)
end
|