http.rb 0.13.3 → 0.14.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccdd073930399a89a9a5345bcad82b9fb08c8f4ec0ac43ac498dfeb7b9d16d36
4
- data.tar.gz: a9565770affd06919fdfc4d8a82c1d3c80015c046641db788c1da75c5e8852cd
3
+ metadata.gz: 55e2dfad8365a9b2918a6b6da9d8ab8c6abb469b014a9ad70c44d4787607512a
4
+ data.tar.gz: 891e2fe083b00fac2dbbd1bc0187853318471762136f74ff61ee662468c4acfb
5
5
  SHA512:
6
- metadata.gz: 27b4d41bf3b4e35476c42fe785ea3262b5d5f66c85d373e319d06b2df8e6f50af91b619148f729d2947414d300fad912d9f7f50023203a7e4968f10fbd9561e1
7
- data.tar.gz: 467692c9cf9ca0941db528e5391ce796198c3533bfeb0d19fa7e54d279abfb3c96ad0f34a2fa398dd664d3a2c5aadaca06f5f242fbbaed4daf1d47c8a825debc
6
+ metadata.gz: 6690a4668fb76471816c3929b06fc980a4e29fb4be84493315507b6166bd2203b353b248ad070b05b7cc1059b9a8e335148eab7f39cccd149ba6a53de40436c0
7
+ data.tar.gz: 66474bb9db3def1e771454c4fb832e49bcccfe2732b2f6a7d8e742ea7f7b788fd22441c32a84db5667606c9a4cef7c9b628139ba2817904f559674837a2a7eaf
data/CHANGELOG.txt CHANGED
@@ -1,3 +1,14 @@
1
+ # 20250711
2
+ # 0.14.0: + DELETE
3
+ 1. + HTTP.delete
4
+ 2. + Net::HTTP::Delete#set_headers
5
+ 3. + spec/HTTP/delete_spec.rb
6
+ 4. ~ HTTP.delete: /require/require_relative/
7
+ 5. ~ HTTP.delete: /require/require_relative/
8
+ 6. ~ HTTP::VERSION: /0.13.3/0.14.0/
9
+ 7. ~ CHANGELOG.txt
10
+ 8. ~ http.rb.gemspec: Change date.
11
+
1
12
  # 20250501
2
13
  # 0.13.3: Handle when there's a redirect to a URL with arguments, so as to not add an additional '?' at the end.
3
14
  1. ~ HTTP.get: Check if the args hash is empty.
data/http.rb.gemspec CHANGED
@@ -3,7 +3,7 @@ require_relative './lib/HTTP/VERSION'
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'http.rb'
5
5
  s.version = HTTP::VERSION
6
- s.date = '2025-05-01'
6
+ s.date = '2025-07-11'
7
7
 
8
8
  s.summary = "HTTP made easy."
9
9
  s.description = "HTTP is the simplest HTTP mezzanine library for Ruby. Supply a URI, \
data/lib/HTTP/VERSION.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # HTTP::VERSION
3
3
 
4
4
  module HTTP
5
- VERSION = '0.13.3'
5
+ VERSION = '0.14.0'
6
6
  end
@@ -0,0 +1,54 @@
1
+ # HTTP/delete.rb
2
+ # HTTP.delete
3
+
4
+ require 'net/http'
5
+ require 'openssl'
6
+ require 'uri'
7
+
8
+ require_relative '../Hash/x_www_form_urlencode'
9
+ require_relative '../Net/HTTP/set_options'
10
+ require_relative '../Net/HTTP/Delete/set_headers'
11
+ require_relative '../Net/HTTPResponse/StatusPredicates'
12
+ require_relative '../URI/Generic/use_sslQ'
13
+
14
+ module HTTP
15
+
16
+ def delete(uri, args = {}, headers = {}, options = {}, &block)
17
+ uri = uri.is_a?(URI) ? uri : URI.parse(uri)
18
+ http = Net::HTTP.new(uri.host, uri.port)
19
+ no_redirect = options.delete(:no_redirect)
20
+ options[:use_ssl] ||= uri.use_ssl?
21
+ options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
22
+ http.options = options
23
+ if args.empty?
24
+ request_object = Net::HTTP::Delete.new(uri.request_uri)
25
+ else
26
+ request_object = Net::HTTP::Delete.new(uri.request_uri + '?' + args.x_www_form_urlencode)
27
+ end
28
+ request_object.headers = headers
29
+ request_object.basic_auth(uri.user, uri.password) if uri.user
30
+ response = http.request(request_object)
31
+ if response.code =~ /^3/
32
+ if block_given? && no_redirect
33
+ yield response
34
+ elsif no_redirect
35
+ return response
36
+ end
37
+ redirect_uri = URI.parse(response.header['location'])
38
+ if redirect_uri.scheme
39
+ response = get(response.header['location'], {}, {}, options, &block)
40
+ else
41
+ new_location = "http://#{uri.host}:#{uri.port}#{response.header['location']}"
42
+ response = get(new_location, {}, {}, options, &block)
43
+ end
44
+ end
45
+ if block_given?
46
+ yield response
47
+ else
48
+ response
49
+ end
50
+ end
51
+
52
+ module_function :delete
53
+
54
+ end
@@ -0,0 +1,14 @@
1
+ # Net/HTTP/Delete/set_headers.rb
2
+ # Net::HTTP::Delete#set_headers
3
+
4
+ # 20250710
5
+ # 0.0.0
6
+
7
+ class Net::HTTP::Delete
8
+
9
+ def set_headers(headers = {})
10
+ headers.each{|k,v| self[k] = v}
11
+ end
12
+ alias_method :headers=, :set_headers
13
+
14
+ end
@@ -0,0 +1,247 @@
1
+ # spec/HTTP/delete_spec.rb
2
+
3
+ require_relative '../spec_helper'
4
+ require 'HTTP/delete'
5
+
6
+ describe ".delete" do
7
+ context "with uri-only supplied" do
8
+ before do
9
+ stub_request(:delete, 'http://example.com/path').
10
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
11
+ to_return(status: 200, body: '', headers: {})
12
+ end
13
+
14
+ context "uri as a string" do
15
+ let(:uri){'http://example.com/path'}
16
+ let(:parsed_uri){URI.parse(uri)}
17
+ let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
18
+
19
+ it "creates an instance of URI" do
20
+ expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
21
+ response = HTTP.delete(uri)
22
+ expect(response.success?).to eq(true)
23
+ end
24
+
25
+ it "creates a new Net::HTTP object" do
26
+ expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
27
+ response = HTTP.delete(uri)
28
+ expect(response.success?).to eq(true)
29
+ end
30
+ end
31
+
32
+ context "uri as a URI" do
33
+ let(:uri_string){'http://example.com/path'}
34
+ let(:uri){URI.parse(uri_string)}
35
+ let(:net_http_object){Net::HTTP.new(uri.host, uri.port)}
36
+
37
+ it "returns an instance of URI" do
38
+ expect(uri).to eq(uri)
39
+ HTTP.delete(uri)
40
+ end
41
+
42
+ it "creates a new Net::HTTP object" do
43
+ expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
44
+ response = HTTP.delete(uri)
45
+ expect(response.success?).to eq(true)
46
+ end
47
+ end
48
+ end
49
+
50
+ context "with args supplied" do
51
+ let(:uri){'http://example.com/path'}
52
+ let(:parsed_uri){URI.parse(uri)}
53
+ let(:args) do; {a: 1, b: 2}; end
54
+ let(:x_www_form_urlencoded_arguments) do; args.x_www_form_urlencode; end
55
+ let(:get_argument){parsed_uri.request_uri + '?' + x_www_form_urlencoded_arguments}
56
+ let(:request_object){Net::HTTP::Delete.new(get_argument)}
57
+
58
+ before do
59
+ stub_request(:delete, 'http://example.com/path?a=1&b=2').
60
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
61
+ to_return(status: 200, body: '', headers: {})
62
+ end
63
+
64
+ it "x_www_form_urlencode's the args" do
65
+ expect(args).to receive(:x_www_form_urlencode).and_return(x_www_form_urlencoded_arguments)
66
+ response = HTTP.delete(uri, args)
67
+ expect(response.success?).to eq(true)
68
+ end
69
+
70
+ it "creates a new Net::HTTP::Delete object" do
71
+ expect(Net::HTTP::Delete).to receive(:new).with(get_argument).and_return(request_object)
72
+ response = HTTP.delete(uri, args)
73
+ expect(response.success?).to eq(true)
74
+ end
75
+ end
76
+
77
+ context "with headers supplied" do
78
+ let(:uri){'http://example.com/path'}
79
+ let(:parsed_uri){URI.parse(uri)}
80
+ let(:headers) do; {'User-Agent' => 'Rspec'}; end
81
+ let(:get_argument){parsed_uri.request_uri}
82
+ let(:request_object){Net::HTTP::Delete.new(get_argument)}
83
+
84
+ before do
85
+ stub_request(:delete, 'http://example.com/path').
86
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Rspec'}).
87
+ to_return(status: 200, body: '', headers: {})
88
+ end
89
+
90
+ it "sets the headers on the request object" do
91
+ allow(Net::HTTP::Delete).to receive(:new).with(get_argument).and_return(request_object)
92
+ response = HTTP.delete(uri, {}, headers)
93
+ expect(request_object['User-Agent']).to eq('Rspec')
94
+ expect(response.success?).to eq(true)
95
+ end
96
+ end
97
+
98
+ context "with options supplied" do
99
+ let(:uri){'http://example.com/path'}
100
+ let(:parsed_uri){URI.parse(uri)}
101
+ let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
102
+ let(:options) do; {use_ssl: true}; end
103
+
104
+ before do
105
+ stub_request(:delete, 'https://example.com:80/path').
106
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
107
+ to_return(status: 200, body: '', headers: {})
108
+ end
109
+
110
+ it "sets the use_ssl option on the Net::HTTP instance" do
111
+ allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
112
+ response = HTTP.delete(uri, {}, {}, options)
113
+ expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
114
+ expect(response.success?).to eq(true)
115
+ end
116
+ end
117
+
118
+ context "with block supplied" do
119
+ let(:uri){'http://example.com/path'}
120
+
121
+ before do
122
+ stub_request(:delete, 'http://example.com/path').
123
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
124
+ to_return(status: 200, body: '', headers: {})
125
+ end
126
+
127
+ it "yields an instance of Net::HTTPResponse" do
128
+ expect{|b| HTTP.delete(uri, &b)}.to yield_with_args(Net::HTTPResponse)
129
+ end
130
+ end
131
+
132
+ context "with redirection" do
133
+ let(:request_uri){'http://example.com/path'}
134
+ let(:redirect_uri){'http://redirected.com'}
135
+
136
+ before do
137
+ stub_request(:get, redirect_uri).
138
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
139
+ to_return(status: 200, body: '', headers: {})
140
+ end
141
+
142
+ context "via 301" do
143
+ before do
144
+ stub_request(:delete, request_uri).
145
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
146
+ to_return(status: 301, body: '', headers: {'location' => redirect_uri})
147
+ end
148
+
149
+ it "does a redirect" do
150
+ expect(HTTP).to receive(:delete).once.with(request_uri).and_call_original
151
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
152
+ response = HTTP.delete(request_uri)
153
+ expect(response.success?).to eq(true)
154
+ end
155
+ end
156
+
157
+ context "via 302" do
158
+ before do
159
+ stub_request(:delete, request_uri).
160
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
161
+ to_return(status: 302, body: '', headers: {'location' => redirect_uri})
162
+ end
163
+
164
+ it "does a redirect" do
165
+ expect(HTTP).to receive(:delete).once.with(request_uri).and_call_original
166
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
167
+ response = HTTP.delete(request_uri)
168
+ expect(response.success?).to eq(true)
169
+ end
170
+ end
171
+ end
172
+
173
+ context "with path only redirection" do
174
+ let(:request_uri){'http://example.com/path'}
175
+ let(:redirect_path){'/new_path'}
176
+ let(:redirect_uri){"http://example.com:80#{redirect_path}"}
177
+
178
+ before do
179
+ stub_request(:get, redirect_uri).
180
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
181
+ to_return(status: 200, body: '', headers: {})
182
+ end
183
+
184
+ context "via 301" do
185
+ before do
186
+ stub_request(:delete, request_uri).
187
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
188
+ to_return(status: 301, body: '', headers: {'location' => redirect_path})
189
+ end
190
+
191
+ it "does a redirect" do
192
+ expect(HTTP).to receive(:delete).once.with(request_uri).and_call_original
193
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
194
+ response = HTTP.delete(request_uri)
195
+ expect(response.success?).to eq(true)
196
+ end
197
+ end
198
+
199
+ context "via 302" do
200
+ before do
201
+ stub_request(:delete, request_uri).
202
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
203
+ to_return(status: 302, body: '', headers: {'location' => redirect_path})
204
+ end
205
+
206
+ it "does a redirect" do
207
+ expect(HTTP).to receive(:delete).once.with(request_uri).and_call_original
208
+ expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: 0}).and_call_original
209
+ response = HTTP.delete(request_uri)
210
+ expect(response.success?).to eq(true)
211
+ end
212
+ end
213
+ end
214
+
215
+ context "no_redirect true" do
216
+ let(:request_uri){'http://example.com/path'}
217
+ let(:redirect_uri){'http://redirected.com'}
218
+
219
+ context "via 301" do
220
+ before do
221
+ stub_request(:delete, request_uri).
222
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
223
+ to_return(status: 301, body: '', headers: {'location' => redirect_uri})
224
+ end
225
+
226
+ it "doesn't redirect" do
227
+ expect(HTTP).to receive(:delete).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
228
+ response = HTTP.delete(request_uri, {}, {}, {no_redirect: true})
229
+ expect(response.redirection?).to eq(true)
230
+ end
231
+ end
232
+
233
+ context "via 302" do
234
+ before do
235
+ stub_request(:delete, request_uri).
236
+ with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
237
+ to_return(status: 302, body: '', headers: {'location' => redirect_uri})
238
+ end
239
+
240
+ it "doesn't redirect" do
241
+ expect(HTTP).to receive(:delete).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
242
+ response = HTTP.delete(request_uri, {}, {}, {no_redirect: true})
243
+ expect(response.redirection?).to eq(true)
244
+ end
245
+ end
246
+ end
247
+ end
@@ -1,9 +1,6 @@
1
1
  # spec/HTTP/get_spec.rb
2
2
 
3
- spec_dir = File.expand_path(File.join(__FILE__, '..', '..'))
4
- $LOAD_PATH.unshift(spec_dir) unless $LOAD_PATH.include?(spec_dir)
5
-
6
- require 'spec_helper'
3
+ require_relative '../spec_helper'
7
4
  require 'HTTP/get'
8
5
 
9
6
  describe ".get" do
@@ -81,7 +78,7 @@ describe ".get" do
81
78
  let(:uri){'http://example.com/path'}
82
79
  let(:parsed_uri){URI.parse(uri)}
83
80
  let(:headers) do; {'User-Agent' => 'Rspec'}; end
84
- let(:get_argument){parsed_uri.request_uri + '?'}
81
+ let(:get_argument){parsed_uri.request_uri}
85
82
  let(:request_object){Net::HTTP::Get.new(get_argument)}
86
83
 
87
84
  before do
@@ -1,9 +1,6 @@
1
1
  # spec/HTTP/post_spec.rb
2
2
 
3
- spec_dir = File.expand_path(File.join(__FILE__, '..', '..'))
4
- $LOAD_PATH.unshift(spec_dir) unless $LOAD_PATH.include?(spec_dir)
5
-
6
- require 'spec_helper'
3
+ require_relative '../spec_helper'
7
4
  require 'HTTP/post'
8
5
 
9
6
  describe ".post" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.3
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-05-01 00:00:00.000000000 Z
10
+ date: 2025-07-11 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: HTTP is the simplest HTTP mezzanine library for Ruby. Supply a URI, some
13
13
  optional query arguments, some optional headers, and some Net::HTTP options,
@@ -23,15 +23,18 @@ files:
23
23
  - http.rb.gemspec
24
24
  - lib/HTTP.rb
25
25
  - lib/HTTP/VERSION.rb
26
+ - lib/HTTP/delete.rb
26
27
  - lib/HTTP/get.rb
27
28
  - lib/HTTP/post.rb
28
29
  - lib/Hash/x_www_form_urlencode.rb
30
+ - lib/Net/HTTP/Delete/set_headers.rb
29
31
  - lib/Net/HTTP/Get/set_headers.rb
30
32
  - lib/Net/HTTP/Post/set_headers.rb
31
33
  - lib/Net/HTTP/set_options.rb
32
34
  - lib/Net/HTTPResponse/StatusPredicates.rb
33
35
  - lib/String/url_encode.rb
34
36
  - lib/URI/Generic/use_sslQ.rb
37
+ - spec/HTTP/delete_spec.rb
35
38
  - spec/HTTP/get_spec.rb
36
39
  - spec/HTTP/post_spec.rb
37
40
  - spec/spec_helper.rb
@@ -52,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
55
  - !ruby/object:Gem::Version
53
56
  version: '0'
54
57
  requirements: []
55
- rubygems_version: 3.6.8
58
+ rubygems_version: 3.6.9
56
59
  specification_version: 4
57
60
  summary: HTTP made easy.
58
61
  test_files: []