net-hippie 1.3.0 → 1.4.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: 998433769957af723da2f4803acd93083a124bbefb5af0bf95a5083a0afae59c
4
- data.tar.gz: 45b2708548b30020c6a74f269669545ed3355d4c567a914763313dd516f35afd
3
+ metadata.gz: e3fdff6728d13d3fece5e05908cc04e9c0999909ab392f73c9dc67a3203bbfb6
4
+ data.tar.gz: e3e94c49e148b6931b543b011cf10da959d36d6b8f0f6b2dd1f4620e5ff21521
5
5
  SHA512:
6
- metadata.gz: bd3c51b47e5bc632c105daf5aa589cc1df70952ea2b7baa05c699b63e5a54b74054f19d4d42f2715d9d0339a7b28a55e17714e087fa6a385689b158426f6f0bf
7
- data.tar.gz: b7374b67ac0bcf7f93a0ac9715e5fe8cd517e614dcdd06cdf924bb61ca76c4f222b802e9b3cf3dd5a735c78ac5f076f3db13128ef59c510e826c1748d19c72c0
6
+ metadata.gz: 1f1ec4190b76c30c45efa6c00198326eb08f4e80280557dc42a6afb3c70166e80e624986ac14823f1d3f6a24ba16afb923f5ead14420fd34fdc5f88620f82a90
7
+ data.tar.gz: 0331df5f99e4b75ba58185c9dbba971e97f1233f3c0625dc0d7a6948e41cd2b3cc63bebeb4c7bba08767c677a72a1e2e3aee6df2d45afa54c2b0701096a97dc4
data/CHANGELOG.md CHANGED
@@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
+ ## [1.4.0] - 2025-10-08
12
+ ### Added
13
+ - Streaming response support via block parameter
14
+ - Backward compatible with existing block API (arity-based detection)
15
+
11
16
  ## [1.3.0] - 2025-04-30
12
17
  ### Changed
13
18
  - Ruby 2.3+ required
@@ -95,7 +100,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
95
100
  - with\_retry.
96
101
  - authorization header helpers
97
102
 
98
- [Unreleased]: https://github.com/xlgmokha/net-hippie/compare/v1.3.0...HEAD
103
+ [Unreleased]: https://github.com/xlgmokha/net-hippie/compare/v1.4.0...HEAD
104
+ [1.4.0]: https://github.com/xlgmokha/net-hippie/compare/v1.3.0...v1.4.0
99
105
  [1.3.0]: https://github.com/xlgmokha/net-hippie/compare/v1.2.0...v1.3.0
100
106
  [1.2.0]: https://github.com/xlgmokha/net-hippie/compare/v1.1.1...v1.2.0
101
107
  [1.1.1]: https://github.com/xlgmokha/net-hippie/compare/v1.1.0...v1.1.1
data/README.md CHANGED
@@ -90,6 +90,40 @@ headers = { 'Authorization' => Net::Hippie.bearer_auth('token') }
90
90
  Net::Hippie.get('https://www.example.org', headers: headers)
91
91
  ```
92
92
 
93
+ ### Streaming Responses
94
+
95
+ Net::Hippie supports streaming responses by passing a block that accepts the response object:
96
+
97
+ ```ruby
98
+ Net::Hippie.post('https://api.example.com/stream', body: { prompt: 'Hello' }) do |response|
99
+ response.read_body do |chunk|
100
+ print chunk
101
+ end
102
+ end
103
+ ```
104
+
105
+ This is useful for Server-Sent Events (SSE) or other streaming APIs:
106
+
107
+ ```ruby
108
+ client = Net::Hippie::Client.new
109
+ client.post('https://api.openai.com/v1/chat/completions',
110
+ headers: {
111
+ 'Authorization' => Net::Hippie.bearer_auth(ENV['OPENAI_API_KEY']),
112
+ 'Content-Type' => 'application/json'
113
+ },
114
+ body: { model: 'gpt-4', messages: [{ role: 'user', content: 'Hi' }], stream: true }
115
+ ) do |response|
116
+ buffer = ""
117
+ response.read_body do |chunk|
118
+ buffer += chunk
119
+ while (line = buffer.slice!(/.*\n/))
120
+ next if line.strip.empty?
121
+ puts line if line.start_with?('data: ')
122
+ end
123
+ end
124
+ end
125
+ ```
126
+
93
127
  ## Development
94
128
 
95
129
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests.
@@ -26,14 +26,10 @@ module Net
26
26
 
27
27
  def execute(uri, request, limit: follow_redirects, &block)
28
28
  connection = connection_for(uri)
29
+ return execute_with_block(connection, request, &block) if block_given?
30
+
29
31
  response = connection.run(request)
30
- if limit.positive? && response.is_a?(Net::HTTPRedirection)
31
- url = connection.build_url_for(response['location'])
32
- request = request_for(Net::HTTP::Get, url)
33
- execute(url, request, limit: limit - 1, &block)
34
- else
35
- block_given? ? yield(request, response) : response
36
- end
32
+ follow_redirect?(response, limit) ? follow_redirect(connection, response, limit) : response
37
33
  end
38
34
 
39
35
  def get(uri, headers: {}, body: {}, &block)
@@ -78,6 +74,20 @@ module Net
78
74
 
79
75
  attr_reader :default_headers
80
76
 
77
+ def execute_with_block(connection, request, &block)
78
+ block.arity == 2 ? yield(request, connection.run(request)) : connection.run(request, &block)
79
+ end
80
+
81
+ def follow_redirect?(response, limit)
82
+ limit.positive? && response.is_a?(Net::HTTPRedirection)
83
+ end
84
+
85
+ def follow_redirect(connection, response, limit)
86
+ url = connection.build_url_for(response['location'])
87
+ request = request_for(Net::HTTP::Get, url)
88
+ execute(url, request, limit: limit - 1)
89
+ end
90
+
81
91
  def attempt(attempt, max)
82
92
  yield
83
93
  rescue *CONNECTION_ERRORS => error
@@ -15,8 +15,12 @@ module Net
15
15
  @http = http
16
16
  end
17
17
 
18
- def run(request)
19
- @http.request(request)
18
+ def run(request, &block)
19
+ if block_given?
20
+ @http.request(request, &block)
21
+ else
22
+ @http.request(request)
23
+ end
20
24
  end
21
25
 
22
26
  def build_url_for(path)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Net
4
4
  module Hippie
5
- VERSION = '1.3.0'
5
+ VERSION = '1.4.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-hippie
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-30 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: base64
@@ -198,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
198
  - !ruby/object:Gem::Version
199
199
  version: '0'
200
200
  requirements: []
201
- rubygems_version: 3.6.6
201
+ rubygems_version: 3.7.2
202
202
  specification_version: 4
203
203
  summary: net/http for hippies. ☮️
204
204
  test_files: []