http.rb 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 751b8b4052533b0498e04222713234378e7a0eb2
4
- data.tar.gz: f14d21a8c49ac437c252f03be490a139e5551126
2
+ SHA256:
3
+ metadata.gz: 64b5868b1582ef96a23021bef26cbfaa4e7ee7a63f28566e20c5f53d6ba91356
4
+ data.tar.gz: 9af58e51678e5b6ba65d55ffdca2d2382cde427fb7e2db2a5999a1e137344969
5
5
  SHA512:
6
- metadata.gz: 6ac92d7be20b5d4e260ed369a974b36c988f6f059eefeb6eddb04cbeb3fc6bd67c325ebd02093660deb6201f799ae028a13fed64f4ce1e0289199742d4b0afe1
7
- data.tar.gz: 0b4abaec5371343017d3d42c2d24b9f3e84fcc2b0a29feef493a8f695bd9f3f1b4162dde3522cd4b26cd508790db4145a08a94e04e254c9bdb28c8df87bd4686
6
+ metadata.gz: 291e79fc695b67dde10145d69311a79c2529f9cac03a217abbbeb254920c8aa137e0220f0e3f2eba3d46b482757a5c1777197fb4dc758dc0adb67918039a5924
7
+ data.tar.gz: ec2e4b6caf7878518de41ceec6881ebb957dfcca50bc9e3aaa4295b9893ff825459e374c3ebaaf22c6ec1eadb9c6e762459d833f71fbe71397c6b91fb44a217c
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  group :test do
4
+ gem 'pry', require: true
4
5
  gem 'rspec', require: false
5
6
  gem 'webmock', require: false
6
7
  end
data/README.md CHANGED
@@ -19,7 +19,7 @@ Perhaps someone will appreciate its relative simplicity, since it is much smalle
19
19
 
20
20
  Add this line to your application's Gemfile:
21
21
 
22
- gem 'HTTP'
22
+ gem 'http.rb'
23
23
 
24
24
  And then execute:
25
25
 
@@ -27,7 +27,7 @@ And then execute:
27
27
 
28
28
  Or install it yourself as:
29
29
 
30
- $ gem install HTTP
30
+ $ gem install http.rb
31
31
 
32
32
 
33
33
  ## Usage
@@ -43,6 +43,10 @@ HTTP.post('http://example.com') # Admittedly doing a POST without providing form
43
43
  HTTP.get('http://example.com', {a: 1, b: 2})
44
44
  HTTP.post('http://example.com', {a: 1, b: 2})
45
45
 
46
+ # With JSON data
47
+
48
+ HTTP.post('http://example.com', {a: 1, b: 2}, {'Content-type' => 'application/json'})
49
+
46
50
  # With custom headers only
47
51
 
48
52
  HTTP.get('http://example.com', {}, {'User-Agent'=>'Custom'})
data/http.rb.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'http.rb' # I would have preferred 'http', but there's a library called http.rb with the gem name of http. Confusing, eh?
3
- s.version = '0.12.0'
4
- s.date = '2017-10-07'
3
+ s.version = '0.12.1'
4
+ s.date = '2025-02-07'
5
5
 
6
6
  s.summary = "HTTP made easy."
7
7
  s.description = "HTTP is the simplest HTTP mezzanine library for Ruby. Supply a URI, \
data/lib/HTTP/post.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # HTTP/post.rb
2
2
  # HTTP.post
3
3
 
4
+ require 'json'
4
5
  require 'net/http'
5
6
  require 'openssl'
6
7
  require 'uri'
@@ -15,14 +16,18 @@ require 'URI/Generic/use_sslQ'
15
16
 
16
17
  module HTTP
17
18
 
18
- def post(uri, form_data = {}, headers = {}, options = {}, &block)
19
+ def post(uri, data = {}, headers = {}, options = {}, &block)
19
20
  uri = uri.is_a?(URI) ? uri : URI.parse(uri)
20
21
  http = Net::HTTP.new(uri.host, uri.port)
21
22
  options[:use_ssl] ||= uri.use_ssl?
22
23
  options[:verify_mode] ||= OpenSSL::SSL::VERIFY_NONE
23
24
  http.options = options
24
25
  request_object = Net::HTTP::Post.new(uri.request_uri)
25
- request_object.form_data = form_data
26
+ if headers['Content-Type'] == 'application/json'
27
+ request_object.body = JSON.dump(data)
28
+ else
29
+ request_object.form_data = data
30
+ end
26
31
  request_object.headers = headers
27
32
  request_object.basic_auth(uri.user, uri.password) if uri.user
28
33
  response = http.request(request_object)
data/lib/HTTP.rb CHANGED
@@ -1,16 +1,6 @@
1
1
  # HTTP.rb
2
2
  # HTTP
3
3
 
4
- # 20150518
5
- # 0.11.1
6
-
7
- # Changes since 0.10:
8
- # 1. Removed HTTP/write.rb, since I wanted to reimplement it using standard File methods and not my custom written File.write. It may stay gone, but I'm not sure yet...
9
- # 2. + HTTP.gemspec
10
- # 3. + README.md
11
- # 0/1
12
- # 4. + String/url_encode, which was left out previously.
13
-
14
4
  lib_dir = File.expand_path(File.join(__FILE__, '..'))
15
5
  $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
16
6
 
@@ -7,7 +7,6 @@ require 'spec_helper'
7
7
  require 'HTTP/get'
8
8
 
9
9
  describe ".get" do
10
-
11
10
  context "with uri-only supplied" do
12
11
  before do
13
12
  stub_request(:get, 'http://example.com/path').
@@ -205,5 +204,4 @@ describe ".get" do
205
204
  end
206
205
  end
207
206
  end
208
-
209
207
  end
@@ -7,7 +7,6 @@ require 'spec_helper'
7
7
  require 'HTTP/post'
8
8
 
9
9
  describe ".post" do
10
-
11
10
  context "with uri-only supplied" do
12
11
  before do
13
12
  stub_request(:post, 'http://example.com/path').
@@ -51,26 +50,54 @@ describe ".post" do
51
50
  context "with form data supplied" do
52
51
  let(:uri){'http://example.com/path'}
53
52
  let(:parsed_uri){URI.parse(uri)}
54
- let(:form_data) do; {a: 1, b: 2}; end
55
- let(:encoded_form_data) do; form_data.x_www_form_urlencode; end
53
+ let(:args) do; {a: 1, b: 2}; end
54
+ let(:headers) do; {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}; end
55
+ let(:encoded_form_data){args.x_www_form_urlencode}
56
56
  let(:request_uri){parsed_uri.request_uri}
57
57
  let(:request_object){Net::HTTP::Post.new(request_uri)}
58
58
 
59
59
  before do
60
60
  stub_request(:post, "http://example.com/path").
61
- with(body: {"a"=>"1", "b"=>"2"}, headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
61
+ with(body: encoded_form_data, headers: headers).
62
62
  to_return(status: 200, body: '', headers: {})
63
63
  end
64
64
 
65
65
  it "sets the form data" do
66
66
  allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
67
- HTTP.post(uri, form_data)
67
+ HTTP.post(uri, args, headers)
68
68
  expect(request_object.body).to eq(encoded_form_data)
69
69
  end
70
70
 
71
71
  it "creates a new Net::HTTP::Post object" do
72
72
  expect(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
73
- HTTP.post(uri, form_data)
73
+ HTTP.post(uri, args, headers)
74
+ end
75
+ end
76
+
77
+ context "with JSON data supplied" do
78
+ let(:uri){'http://example.com/path'}
79
+ let(:parsed_uri){URI.parse(uri)}
80
+ let(:args) do; {a: 1, b: 2}; end
81
+ let(:headers) do; {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}; end
82
+ let(:json_data){JSON.dump(args)}
83
+ let(:request_uri){parsed_uri.request_uri}
84
+ let(:request_object){Net::HTTP::Post.new(request_uri)}
85
+
86
+ before do
87
+ stub_request(:post, "http://example.com/path").
88
+ with(body: json_data, headers: headers).
89
+ to_return(status: 200, body: '', headers: {})
90
+ end
91
+
92
+ it "sets the body" do
93
+ allow(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
94
+ HTTP.post(uri, args, headers)
95
+ expect(request_object.body).to eq(json_data)
96
+ end
97
+
98
+ it "creates a new Net::HTTP::Post object" do
99
+ expect(Net::HTTP::Post).to receive(:new).with(request_uri).and_return(request_object)
100
+ HTTP.post(uri, args, headers)
74
101
  end
75
102
  end
76
103
 
@@ -206,5 +233,4 @@ describe ".post" do
206
233
  end
207
234
  end
208
235
  end
209
-
210
236
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2017-10-07 00:00:00.000000000 Z
10
+ date: 2025-02-07 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: HTTP is the simplest HTTP mezzanine library for Ruby. Supply a URI, some
14
13
  optional query arguments, some optional headers, and some Net::HTTP options,
@@ -36,7 +35,6 @@ files:
36
35
  homepage: http://github.com/thoran/HTTP
37
36
  licenses: []
38
37
  metadata: {}
39
- post_install_message:
40
38
  rdoc_options: []
41
39
  require_paths:
42
40
  - lib
@@ -51,9 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
49
  - !ruby/object:Gem::Version
52
50
  version: '0'
53
51
  requirements: []
54
- rubyforge_project:
55
- rubygems_version: 2.5.2
56
- signing_key:
52
+ rubygems_version: 3.6.3
57
53
  specification_version: 4
58
54
  summary: HTTP made easy.
59
55
  test_files: []