Skip to content

Commit 47f56c5

Browse files
committed
Refactor proxy
1 parent 941821f commit 47f56c5

5 files changed

Lines changed: 45 additions & 32 deletions

File tree

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ gem 'rake'
66
gem 'json'
77

88
group :test do
9-
gem "rspec"
10-
end
9+
gem 'rspec'
10+
end

lib/detect_language.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def client
1919
Thread.current[:detect_language_client] ||= Client.new(config)
2020
end
2121

22+
def client=(client)
23+
Thread.current[:detect_language_client] = client
24+
end
25+
2226
def detect(query)
2327
client.post('detect', q: query)
2428
end

lib/detect_language/client.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@ def get(path)
2121
private
2222

2323
def execute(method, path, body: nil)
24-
uri = URI(config.base_url)
25-
http = setup_http_connection(uri)
26-
request = method.new(uri.path + path)
24+
request = method.new(base_uri.path + path)
2725
request.body = body
2826

2927
request['Content-Type'] = 'application/json'
3028
request['Authorization'] = 'Bearer ' + config.api_key.to_s
3129
request['User-Agent'] = config.user_agent
3230

33-
response = http.request(request)
31+
response = connection.request(request)
3432

3533
case response
36-
when Net::HTTPSuccess, Net::HTTPUnauthorized then
34+
when Net::HTTPSuccess, Net::HTTPUnauthorized
3735
parse_response(response.body)
3836
else
3937
raise(Error, "Failure: #{response.class}")
@@ -50,21 +48,29 @@ def parse_response(response_body)
5048
end
5149
end
5250

53-
def setup_http_connection(uri)
54-
http = Net::HTTP::Proxy(
55-
config.proxy_host, config.proxy_port, config.proxy_user, config.proxy_pass
56-
).new(uri.host, uri.port)
51+
def base_uri
52+
@base_uri ||= URI(config.base_url)
53+
end
5754

58-
http.read_timeout = config.http_read_timeout
59-
http.open_timeout = config.http_open_timeout
55+
def connection
56+
@connection ||= setup_connection
57+
end
6058

61-
if uri.scheme == 'https'
62-
http.use_ssl = true
63-
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
59+
def setup_connection
60+
http = if config.proxy
61+
proxy = URI(config.proxy)
62+
proxy_use_ssl = proxy.scheme == 'https'
63+
Net::HTTP.new(base_uri.hostname, base_uri.port,
64+
proxy.hostname, proxy.port, proxy.user, proxy.password, nil, proxy_use_ssl)
6465
else
65-
http.use_ssl = false
66+
Net::HTTP.new(base_uri.hostname, base_uri.port)
6667
end
6768

69+
http.use_ssl = base_uri.scheme == 'https'
70+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER if http.use_ssl?
71+
http.read_timeout = config.http_read_timeout
72+
http.open_timeout = config.http_open_timeout
73+
6874
http
6975
end
7076
end

lib/detect_language/configuration.rb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
module DetectLanguage
22
class Configuration
3-
# The API key for your project, found on your homepage after you login into detectlanguage.com website
3+
# The API key for your project, found on your homepage after you log in into
4+
# https://detectlanguage.com website
45
attr_accessor :api_key
56

6-
# HTTP request user agent (defaults to 'Detect Language API ruby gem').
7-
attr_accessor :user_agent
8-
97
# API base URL
108
attr_accessor :base_url
119

10+
# HTTP request user agent (defaults to 'Detect Language API ruby gem').
11+
attr_accessor :user_agent
12+
1213
# The HTTP open timeout in seconds.
1314
attr_accessor :http_open_timeout
1415

1516
# The HTTP read timeout in seconds.
1617
attr_accessor :http_read_timeout
1718

18-
# The hostname of your proxy server (if using a proxy).
19-
attr_accessor :proxy_host
20-
21-
# The port of your proxy server (if using a proxy).
22-
attr_accessor :proxy_port
23-
24-
# The username to use when logging into your proxy server (if using a proxy).
25-
attr_accessor :proxy_user
26-
27-
# The password to use when logging into your proxy server (if using a proxy).
28-
attr_accessor :proxy_pass
19+
# The HTTP proxy to use for requests. Example: 'http://my-proxy:8080'
20+
attr_accessor :proxy
2921

3022
def initialize
3123
@api_key = nil
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
RSpec.describe DetectLanguage do
44
let(:api_key) { ENV['DETECTLANGUAGE_API_KEY'] }
5+
let(:proxy) { nil }
56

67
before do
8+
described_class.client = nil
79
described_class.config.api_key = api_key
10+
described_class.config.proxy = proxy
811
end
912

1013
describe '.config' do
@@ -42,6 +45,14 @@
4245
expect { subject }.to raise_error(DetectLanguage::Error)
4346
end
4447
end
48+
49+
xcontext 'with proxy' do
50+
let(:proxy) { 'http://my-proxy:8080' }
51+
52+
it 'uses the proxy for requests' do
53+
expect { subject }.not_to raise_error
54+
end
55+
end
4556
end
4657

4758
describe '.detect_code' do

0 commit comments

Comments
 (0)