Skip to content

Commit ca80fea

Browse files
author
Jacob Perkins
committed
Add comprehensive Ruby proxy examples
Adds examples for 9 Ruby HTTP libraries: - Net::HTTP (stdlib) - Faraday - HTTParty - RestClient - Typhoeus - HTTP.rb - Excon - HTTPClient - Mechanize Includes Gemfile, test runner, and updated README with documentation. Each example demonstrates basic proxy configuration with environment variables. Made-with: Cursor
1 parent 10ba227 commit ca80fea

12 files changed

Lines changed: 578 additions & 1 deletion

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,44 @@ python python/run_tests.py requests-proxy-headers httpx-proxy-headers
7171

7272
## Ruby Proxy Examples
7373

74-
* [requests_proxy.rb](ruby/requests_proxy.rb) - Ruby HTTP with proxy, from [rpolley](https://github.com/rpolley)
74+
**Installation:**
75+
76+
```bash
77+
cd ruby
78+
bundle install
79+
```
80+
81+
**Running Examples:**
82+
83+
```bash
84+
# Required: Set your proxy URL
85+
export PROXY_URL='http://user:pass@proxy.example.com:8080'
86+
87+
# Run a single example
88+
ruby ruby/faraday_proxy.rb
89+
90+
# Run all examples as tests
91+
ruby ruby/run_tests.rb
92+
93+
# Run specific examples
94+
ruby ruby/run_tests.rb faraday httparty
95+
```
96+
97+
**Examples:**
98+
99+
| Library | Example | Description |
100+
|---------|---------|-------------|
101+
| [Net::HTTP](https://ruby-doc.org/stdlib/libdoc/net/http/rdoc/Net/HTTP.html) | [net_http_proxy.rb](ruby/net_http_proxy.rb) | Ruby standard library HTTP client |
102+
| [Faraday](https://lostisland.github.io/faraday/) | [faraday_proxy.rb](ruby/faraday_proxy.rb) | HTTP client with middleware support |
103+
| [HTTParty](https://github.com/jnunemaker/httparty) | [httparty_proxy.rb](ruby/httparty_proxy.rb) | Makes HTTP fun again |
104+
| [RestClient](https://github.com/rest-client/rest-client) | [rest_client_proxy.rb](ruby/rest_client_proxy.rb) | Simple REST client |
105+
| [Typhoeus](https://typhoeus.github.io/) | [typhoeus_proxy.rb](ruby/typhoeus_proxy.rb) | Fast HTTP client (libcurl wrapper) |
106+
| [HTTP.rb](https://github.com/httprb/http) | [http_rb_proxy.rb](ruby/http_rb_proxy.rb) | Simple Ruby DSL for HTTP |
107+
| [Excon](https://github.com/excon/excon) | [excon_proxy.rb](ruby/excon_proxy.rb) | Fast, simple HTTP(S) client |
108+
| [HTTPClient](https://github.com/nahi/httpclient) | [httpclient_proxy.rb](ruby/httpclient_proxy.rb) | LWP-like HTTP client |
109+
| [Mechanize](https://github.com/sparklemotion/mechanize) | [mechanize_proxy.rb](ruby/mechanize_proxy.rb) | Web automation library |
110+
111+
> **Note:** None of these libraries currently support sending custom headers to the proxy during HTTPS CONNECT tunneling or reading proxy response headers. See [ruby-proxy-headers](https://github.com/proxymeshai/ruby-proxy-headers) for extension modules that add this capability.
75112
76113
## Documentation
77114

ruby/Gemfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
# HTTP clients
6+
gem 'faraday', '~> 2.9'
7+
gem 'httparty', '~> 0.21'
8+
gem 'rest-client', '~> 2.1'
9+
gem 'typhoeus', '~> 1.4'
10+
gem 'http', '~> 5.2'
11+
gem 'excon', '~> 0.110'
12+
gem 'httpclient', '~> 2.8'
13+
gem 'patron', '~> 0.13'
14+
gem 'mechanize', '~> 2.10'
15+
16+
# For running tests
17+
gem 'minitest', '~> 5.22'

ruby/excon_proxy.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Excon with proxy example.
5+
#
6+
# Configuration via environment variables:
7+
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
8+
# TEST_URL - URL to request (default: https://api.ipify.org?format=json)
9+
#
10+
# Excon is a fast, simple HTTP(S) client. It supports proxies but does NOT
11+
# support sending custom headers during HTTPS CONNECT or reading proxy response headers.
12+
13+
require 'excon'
14+
15+
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
16+
unless proxy_url
17+
warn 'Error: Set PROXY_URL environment variable'
18+
exit 1
19+
end
20+
21+
test_url = ENV['TEST_URL'] || 'https://api.ipify.org?format=json'
22+
23+
begin
24+
response = Excon.get(test_url, proxy: proxy_url)
25+
26+
puts "Status: #{response.status}"
27+
puts "Body: #{response.body}"
28+
rescue StandardError => e
29+
warn "Error: #{e.message}"
30+
exit 1
31+
end

ruby/faraday_proxy.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Faraday with proxy example.
5+
#
6+
# Configuration via environment variables:
7+
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
8+
# TEST_URL - URL to request (default: https://api.ipify.org?format=json)
9+
#
10+
# Faraday is a popular HTTP client with middleware support. It supports proxies
11+
# but does NOT support sending custom headers during HTTPS CONNECT or reading
12+
# proxy response headers.
13+
14+
require 'faraday'
15+
require 'json'
16+
17+
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
18+
unless proxy_url
19+
warn 'Error: Set PROXY_URL environment variable'
20+
exit 1
21+
end
22+
23+
test_url = ENV['TEST_URL'] || 'https://api.ipify.org?format=json'
24+
25+
begin
26+
conn = Faraday.new(url: test_url) do |f|
27+
f.proxy = proxy_url
28+
f.adapter Faraday.default_adapter
29+
end
30+
31+
response = conn.get
32+
33+
puts "Status: #{response.status}"
34+
puts "Body: #{response.body}"
35+
rescue StandardError => e
36+
warn "Error: #{e.message}"
37+
exit 1
38+
end

ruby/http_rb_proxy.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# HTTP.rb (http gem) with proxy example.
5+
#
6+
# Configuration via environment variables:
7+
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
8+
# TEST_URL - URL to request (default: https://api.ipify.org?format=json)
9+
#
10+
# HTTP.rb is a simple Ruby DSL for making HTTP requests. It supports proxies
11+
# but does NOT support sending custom headers during HTTPS CONNECT or reading
12+
# proxy response headers.
13+
14+
require 'http'
15+
require 'uri'
16+
17+
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
18+
unless proxy_url
19+
warn 'Error: Set PROXY_URL environment variable'
20+
exit 1
21+
end
22+
23+
test_url = ENV['TEST_URL'] || 'https://api.ipify.org?format=json'
24+
25+
proxy_uri = URI.parse(proxy_url)
26+
27+
proxy_options = [proxy_uri.host, proxy_uri.port]
28+
if proxy_uri.user
29+
proxy_options << proxy_uri.user
30+
proxy_options << proxy_uri.password
31+
end
32+
33+
begin
34+
response = HTTP.via(*proxy_options).get(test_url)
35+
36+
puts "Status: #{response.status}"
37+
puts "Body: #{response.body}"
38+
rescue StandardError => e
39+
warn "Error: #{e.message}"
40+
exit 1
41+
end

ruby/httparty_proxy.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# HTTParty with proxy example.
5+
#
6+
# Configuration via environment variables:
7+
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
8+
# TEST_URL - URL to request (default: https://api.ipify.org?format=json)
9+
#
10+
# HTTParty makes HTTP fun! It supports proxies but does NOT support sending
11+
# custom headers during HTTPS CONNECT or reading proxy response headers.
12+
13+
require 'httparty'
14+
require 'uri'
15+
16+
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
17+
unless proxy_url
18+
warn 'Error: Set PROXY_URL environment variable'
19+
exit 1
20+
end
21+
22+
test_url = ENV['TEST_URL'] || 'https://api.ipify.org?format=json'
23+
24+
proxy_uri = URI.parse(proxy_url)
25+
26+
http_proxy_options = {
27+
http_proxyaddr: proxy_uri.host,
28+
http_proxyport: proxy_uri.port
29+
}
30+
31+
if proxy_uri.user
32+
http_proxy_options[:http_proxyuser] = proxy_uri.user
33+
http_proxy_options[:http_proxypass] = proxy_uri.password
34+
end
35+
36+
begin
37+
response = HTTParty.get(test_url, **http_proxy_options)
38+
39+
puts "Status: #{response.code}"
40+
puts "Body: #{response.body}"
41+
rescue StandardError => e
42+
warn "Error: #{e.message}"
43+
exit 1
44+
end

ruby/httpclient_proxy.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# HTTPClient with proxy example.
5+
#
6+
# Configuration via environment variables:
7+
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
8+
# TEST_URL - URL to request (default: https://api.ipify.org?format=json)
9+
#
10+
# HTTPClient provides LWP-like functionality. It has advanced proxy support
11+
# but does NOT expose custom CONNECT headers or proxy response headers.
12+
13+
require 'httpclient'
14+
15+
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
16+
unless proxy_url
17+
warn 'Error: Set PROXY_URL environment variable'
18+
exit 1
19+
end
20+
21+
test_url = ENV['TEST_URL'] || 'https://api.ipify.org?format=json'
22+
23+
begin
24+
client = HTTPClient.new(proxy_url)
25+
response = client.get(test_url)
26+
27+
puts "Status: #{response.status}"
28+
puts "Body: #{response.body}"
29+
rescue StandardError => e
30+
warn "Error: #{e.message}"
31+
exit 1
32+
end

ruby/mechanize_proxy.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Mechanize with proxy example.
5+
#
6+
# Configuration via environment variables:
7+
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
8+
# TEST_URL - URL to request (default: https://example.com)
9+
#
10+
# Mechanize is a web automation library. It uses Net::HTTP internally and
11+
# supports proxies, but does NOT support custom CONNECT headers or proxy
12+
# response headers.
13+
14+
require 'mechanize'
15+
require 'uri'
16+
17+
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
18+
unless proxy_url
19+
warn 'Error: Set PROXY_URL environment variable'
20+
exit 1
21+
end
22+
23+
test_url = ENV['TEST_URL'] || 'https://example.com'
24+
25+
proxy_uri = URI.parse(proxy_url)
26+
27+
begin
28+
agent = Mechanize.new
29+
agent.set_proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
30+
31+
page = agent.get(test_url)
32+
33+
puts "Status: #{page.code}"
34+
puts "Title: #{page.title}"
35+
rescue StandardError => e
36+
warn "Error: #{e.message}"
37+
exit 1
38+
end

ruby/net_http_proxy.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Net::HTTP with proxy example.
5+
#
6+
# Configuration via environment variables:
7+
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
8+
# TEST_URL - URL to request (default: https://api.ipify.org?format=json)
9+
#
10+
# Net::HTTP is Ruby's built-in HTTP client. It supports proxies but does NOT
11+
# support sending custom headers during HTTPS CONNECT or reading proxy response headers.
12+
13+
require 'net/http'
14+
require 'uri'
15+
16+
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
17+
unless proxy_url
18+
warn 'Error: Set PROXY_URL environment variable'
19+
exit 1
20+
end
21+
22+
test_url = ENV['TEST_URL'] || 'https://api.ipify.org?format=json'
23+
24+
proxy_uri = URI.parse(proxy_url)
25+
target_uri = URI.parse(test_url)
26+
27+
proxy_options = {
28+
p_addr: proxy_uri.host,
29+
p_port: proxy_uri.port,
30+
p_user: proxy_uri.user,
31+
p_pass: proxy_uri.password
32+
}
33+
34+
begin
35+
Net::HTTP.start(target_uri.host, target_uri.port, **proxy_options, use_ssl: target_uri.scheme == 'https') do |http|
36+
request = Net::HTTP::Get.new(target_uri)
37+
response = http.request(request)
38+
39+
puts "Status: #{response.code}"
40+
puts "Body: #{response.body}"
41+
end
42+
rescue StandardError => e
43+
warn "Error: #{e.message}"
44+
exit 1
45+
end

ruby/rest_client_proxy.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# RestClient with proxy example.
5+
#
6+
# Configuration via environment variables:
7+
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
8+
# TEST_URL - URL to request (default: https://api.ipify.org?format=json)
9+
#
10+
# RestClient is a simple REST client. It reads proxy from environment variables
11+
# (HTTP_PROXY/HTTPS_PROXY) or can be set via RestClient.proxy.
12+
# Does NOT support custom CONNECT headers or proxy response headers.
13+
14+
require 'rest-client'
15+
16+
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
17+
unless proxy_url
18+
warn 'Error: Set PROXY_URL environment variable'
19+
exit 1
20+
end
21+
22+
test_url = ENV['TEST_URL'] || 'https://api.ipify.org?format=json'
23+
24+
# Set proxy globally
25+
RestClient.proxy = proxy_url
26+
27+
begin
28+
response = RestClient.get(test_url)
29+
30+
puts "Status: #{response.code}"
31+
puts "Body: #{response.body}"
32+
rescue RestClient::ExceptionWithResponse => e
33+
warn "HTTP Error: #{e.response.code}"
34+
exit 1
35+
rescue StandardError => e
36+
warn "Error: #{e.message}"
37+
exit 1
38+
end

0 commit comments

Comments
 (0)