mini_http 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9c08f77f0d125e88a3232a1a297b02c63b58e603035eb270d6f831afc5125b60
4
+ data.tar.gz: 8d1c560f5a1c7c96b16958f76ef11249058948293fad0d27edcc289b07aada43
5
+ SHA512:
6
+ metadata.gz: e6030620de4d5dfebda56fdb0e1a3ecad646cacfbd32c37835f66ecff9c0e5ac4ef3657e8b1da5f86635c7c996b6aed6d70cf5fabc9bf3ff42100c022f8fac4d
7
+ data.tar.gz: 33d519e99803cedc6227a02db5214203f46139df17b040e8a5a5a13eb2e11a16e86f40a339989856a949d1420b1c532c09b0e802a097a7123a52419ed1f84dda
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ EnforcedStyle: double_quotes
11
+
12
+ # Allow longer blocks in tests
13
+ Metrics/BlockLength:
14
+ Exclude:
15
+ - 'spec/**/*'
16
+
17
+ # Allow longer methods for specific cases
18
+ Metrics/MethodLength:
19
+ Max: 15
20
+
21
+ # Allow missing documentation for now
22
+ Style/Documentation:
23
+ Enabled: false
24
+
25
+ Layout/LineLength:
26
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-06-23
4
+
5
+ - Initial release of MiniHttp
6
+ - Support for GET, POST, PUT, DELETE requests
7
+ - Automatic JSON parsing and serialization
8
+ - SSL/HTTPS support with proper certificate verification
9
+ - Configurable timeouts
10
+ - Response helper methods for status checking
11
+ - No external dependencies beyond Ruby standard library
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Felipe Cicolin Rocha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # MiniHttp
2
+
3
+ A minimal, lightweight HTTP client library for Ruby that provides a clean interface for making HTTP requests with automatic JSON handling, SSL support, and customizable timeouts.
4
+
5
+ ## Features
6
+
7
+ - Simple API for GET, POST, PUT, and DELETE requests
8
+ - Automatic JSON parsing and serialization
9
+ - SSL/HTTPS support with proper certificate verification
10
+ - Configurable timeouts
11
+ - Response helper methods for status checking
12
+ - No external dependencies beyond Ruby standard library
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'mini_http'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```bash
25
+ bundle install
26
+ ```
27
+
28
+ Or install it yourself as:
29
+
30
+ ```bash
31
+ gem install mini_http
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ### Basic GET request
37
+
38
+ ```ruby
39
+ response = MiniHttp.get("https://api.example.com/users")
40
+
41
+ if response.success?
42
+ users = response.json
43
+ puts users
44
+ else
45
+ puts "Error: #{response.code}"
46
+ end
47
+ ```
48
+
49
+ ### POST request with JSON body
50
+
51
+ ```ruby
52
+ response = MiniHttp.post(
53
+ "https://api.example.com/users",
54
+ body: { name: "John", email: "john@example.com" },
55
+ headers: { "Authorization" => "Bearer token123" }
56
+ )
57
+
58
+ if response.success?
59
+ puts "User created: #{response.json}"
60
+ end
61
+ ```
62
+
63
+ ### PUT request
64
+
65
+ ```ruby
66
+ response = MiniHttp.put(
67
+ "https://api.example.com/users/1",
68
+ body: { name: "John Updated" },
69
+ timeout: 60
70
+ )
71
+ ```
72
+
73
+ ### DELETE request
74
+
75
+ ```ruby
76
+ response = MiniHttp.delete(
77
+ "https://api.example.com/users/1",
78
+ headers: { "Authorization" => "Bearer token123" }
79
+ )
80
+ ```
81
+
82
+ ### Response methods
83
+
84
+ ```ruby
85
+ response = MiniHttp.get("https://api.example.com/data")
86
+
87
+ # Status checking
88
+ response.success? # true for 2xx status codes
89
+ response.client_error? # true for 4xx status codes
90
+ response.server_error? # true for 5xx status codes
91
+
92
+ # Response data
93
+ response.code # HTTP status code (integer)
94
+ response.body # Raw response body (string)
95
+ response.headers # Response headers (hash)
96
+ response.json # Parsed JSON (hash/array, nil if not valid JSON)
97
+ ```
98
+
99
+ ### Customizing requests
100
+
101
+ All methods support these optional parameters:
102
+
103
+ - `headers`: Hash of HTTP headers
104
+ - `timeout`: Request timeout in seconds (default: 30)
105
+ - `body`: Request body for POST/PUT (string or object that responds to `to_json`)
106
+
107
+ ```ruby
108
+ response = MiniHttp.get(
109
+ "https://api.example.com/data",
110
+ headers: {
111
+ "User-Agent" => "MyApp/1.0",
112
+ "Accept" => "application/json"
113
+ },
114
+ timeout: 45
115
+ )
116
+ ```
117
+
118
+ ## Development
119
+
120
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
121
+
122
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome on GitHub at https://github.com/felipecicolin/simple_http. This project is intended to be a safe, welcoming space for collaboration.
127
+
128
+ ## License
129
+
130
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MiniHttp
4
+ VERSION = "0.1.0"
5
+ end
data/lib/mini_http.rb ADDED
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+ require "openssl"
7
+ require_relative "mini_http/version"
8
+
9
+ class MiniHttp
10
+ class Response
11
+ SUCCESS_RANGE = (200..299).freeze
12
+ CLIENT_ERROR_RANGE = (400..499).freeze
13
+ SERVER_ERROR_RANGE = (500..599).freeze
14
+
15
+ attr_reader :code, :body, :headers
16
+
17
+ def initialize(net_response)
18
+ @code = extract_status_code(net_response)
19
+ @body = net_response.body
20
+ @headers = net_response.to_hash
21
+ end
22
+
23
+ def success?
24
+ SUCCESS_RANGE.cover?(@code)
25
+ end
26
+
27
+ def client_error?
28
+ CLIENT_ERROR_RANGE.cover?(@code)
29
+ end
30
+
31
+ def server_error?
32
+ SERVER_ERROR_RANGE.cover?(@code)
33
+ end
34
+
35
+ def json
36
+ @json ||= parse_json_safely
37
+ end
38
+
39
+ private
40
+
41
+ def extract_status_code(net_response)
42
+ net_response.code.to_i
43
+ end
44
+
45
+ def parse_json_safely
46
+ return nil if @body.nil? || @body.empty?
47
+
48
+ JSON.parse(@body)
49
+ rescue JSON::ParserError
50
+ nil
51
+ end
52
+ end
53
+
54
+ def self.get(url, headers: {}, timeout: 30)
55
+ make_request(:get, url, headers: headers, timeout: timeout)
56
+ end
57
+
58
+ def self.post(url, body: nil, headers: {}, timeout: 30)
59
+ make_request(:post, url, body: body, headers: headers, timeout: timeout)
60
+ end
61
+
62
+ def self.put(url, body: nil, headers: {}, timeout: 30)
63
+ make_request(:put, url, body: body, headers: headers, timeout: timeout)
64
+ end
65
+
66
+ def self.delete(url, headers: {}, timeout: 30)
67
+ make_request(:delete, url, headers: headers, timeout: timeout)
68
+ end
69
+
70
+ class << self
71
+ private
72
+
73
+ def make_request(method, url, body: nil, headers: {}, timeout: 30)
74
+ uri = URI(url)
75
+ http = build_http_client(uri, timeout)
76
+ request = build_request(method, uri, body, headers)
77
+
78
+ net_response = http.request(request)
79
+ Response.new(net_response)
80
+ end
81
+
82
+ def build_http_client(uri, timeout)
83
+ http = Net::HTTP.new(uri.host, uri.port)
84
+ configure_ssl(http, uri)
85
+ configure_timeouts(http, timeout)
86
+ http
87
+ end
88
+
89
+ def configure_ssl(http, uri)
90
+ return unless uri.scheme == "https"
91
+
92
+ http.use_ssl = true
93
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
94
+ end
95
+
96
+ def configure_timeouts(http, timeout)
97
+ http.read_timeout = timeout
98
+ http.open_timeout = [timeout / 3, 10].min
99
+ end
100
+
101
+ def build_request(method, uri, body, headers)
102
+ request = create_request_object(method, uri)
103
+ set_headers(request, headers)
104
+ set_body(request, method, body)
105
+ request
106
+ end
107
+
108
+ def create_request_object(method, uri)
109
+ case method
110
+ when :get
111
+ Net::HTTP::Get.new(uri)
112
+ when :post
113
+ Net::HTTP::Post.new(uri)
114
+ when :put
115
+ Net::HTTP::Put.new(uri)
116
+ when :delete
117
+ Net::HTTP::Delete.new(uri)
118
+ else
119
+ raise ArgumentError, "Unsupported HTTP method: #{method}"
120
+ end
121
+ end
122
+
123
+ def set_headers(request, headers)
124
+ headers.each { |key, value| request[key] = value }
125
+ end
126
+
127
+ def set_body(request, method, body)
128
+ return unless body && body_allowed?(method)
129
+
130
+ request.body = serialize_body(body)
131
+ request["Content-Type"] ||= "application/json"
132
+ end
133
+
134
+ def body_allowed?(method)
135
+ %i[post put].include?(method)
136
+ end
137
+
138
+ def serialize_body(body)
139
+ body.is_a?(String) ? body : body.to_json
140
+ end
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Felipe Cicolin Rocha
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: MiniHttp provides a clean, easy-to-use interface for making HTTP requests
13
+ with automatic JSON handling, SSL support, and customizable timeouts.
14
+ email:
15
+ - felipecicolinrocha@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/mini_http.rb
27
+ - lib/mini_http/version.rb
28
+ homepage: https://github.com/felipecicolin/mini_http
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/felipecicolin/mini_http
33
+ source_code_uri: https://github.com/felipecicolin/mini_http
34
+ changelog_uri: https://github.com/felipecicolin/mini_http/blob/main/CHANGELOG.md
35
+ rubygems_mfa_required: 'true'
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.7.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.6.7
51
+ specification_version: 4
52
+ summary: A minimal HTTP client library for Ruby
53
+ test_files: []