Skip to content

Commit 718aa80

Browse files
committed
Switch to base url
1 parent bc17f08 commit 718aa80

4 files changed

Lines changed: 38 additions & 126 deletions

File tree

lib/detect_language.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55

66
module DetectLanguage
77
class << self
8-
attr_writer :configuration
8+
attr_writer :config
99

1010
def configure
11-
yield(configuration)
11+
yield(config)
1212
end
1313

14-
# The configuration object.
15-
# @see DetectLanguage.configure
16-
def configuration
17-
@configuration ||= Configuration.new
14+
def config
15+
@config ||= Configuration.new
1816
end
1917

2018
def client
21-
Thread.current[:detect_language_client] ||= Client.new(configuration)
19+
Thread.current[:detect_language_client] ||= Client.new(config)
2220
end
2321

2422
def detect(data)

lib/detect_language/client.rb

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
1-
require 'cgi'
21
require 'net/http'
32
require 'net/https'
43
require 'json'
54

65
module DetectLanguage
76
class Client
8-
attr_reader :configuration
7+
attr_reader :config
98

10-
def initialize(configuration)
11-
@configuration = configuration
9+
def initialize(config)
10+
@config = config
1211
end
1312

14-
def post(method, params = {})
15-
execute(method, params, :http_method => Net::HTTP::Post)
13+
def post(path, params = {})
14+
execute(Net::HTTP::Post, path, params)
1615
end
1716

18-
def get(method, params = {})
19-
execute(method, params, :http_method => Net::HTTP::Get)
17+
def get(path, params = {})
18+
execute(Net::HTTP::Get, path, params)
2019
end
2120

2221
private
2322

24-
def execute(method, params, options)
25-
http = setup_http_connection
26-
http_method = options[:http_method]
27-
request = http_method.new(request_uri(method))
23+
def execute(method, path, params)
24+
uri = URI.parse(config.base_url)
25+
http = setup_http_connection(uri)
26+
request = method.new(uri.path + path.to_s)
27+
request.set_form_data(params)
2828

29-
if RUBY_VERSION == '1.8.7'
30-
set_form_data_18(request, params)
31-
else
32-
request.set_form_data(params)
33-
end
34-
35-
request['Authorization'] = 'Bearer ' + configuration.api_key.to_s
36-
request['User-Agent'] = configuration.user_agent
29+
request['Authorization'] = 'Bearer ' + config.api_key.to_s
30+
request['User-Agent'] = config.user_agent
3731

3832
response = http.request(request)
3933

@@ -55,44 +49,22 @@ def parse_response(response_body)
5549
end
5650
end
5751

58-
def request_uri(method)
59-
"/#{configuration.api_version}/#{method}"
60-
end
61-
62-
def setup_http_connection
63-
http =
64-
Net::HTTP::Proxy(configuration.proxy_host, configuration.proxy_port, configuration.proxy_user,
65-
configuration.proxy_pass).
66-
new(configuration.host, configuration.port)
52+
def setup_http_connection(uri)
53+
http = Net::HTTP::Proxy(
54+
config.proxy_host, config.proxy_port, config.proxy_user, config.proxy_pass
55+
).new(uri.host, uri.port)
6756

68-
http.read_timeout = configuration.http_read_timeout
69-
http.open_timeout = configuration.http_open_timeout
57+
http.read_timeout = config.http_read_timeout
58+
http.open_timeout = config.http_open_timeout
7059

71-
if configuration.secure?
72-
http.use_ssl = true
73-
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
60+
if uri.scheme == 'https'
61+
http.use_ssl = true
62+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
7463
else
75-
http.use_ssl = false
64+
http.use_ssl = false
7665
end
7766

7867
http
7968
end
80-
81-
def set_form_data_18(request, params, sep = '&')
82-
request.body = params.map {|k,v|
83-
if v.instance_of?(Array)
84-
v.map {|e| "#{urlencode(k.to_s)}=#{urlencode(e.to_s)}"}.join(sep)
85-
else
86-
"#{urlencode(k.to_s)}=#{urlencode(v.to_s)}"
87-
end
88-
}.join(sep)
89-
90-
request.content_type = 'application/x-www-form-urlencoded'
91-
end
92-
93-
def urlencode(str)
94-
CGI::escape(str)
95-
end
96-
9769
end
9870
end
Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
module DetectLanguage
22
class Configuration
33
# The API key for your project, found on your homepage after you login into detectlanguage.com website
4-
# Defaults to 'demo', which has a limited number of requests.
54
attr_accessor :api_key
65

7-
# The API version you are using (defaults to 0.2).
8-
attr_accessor :api_version
9-
106
# HTTP request user agent (defaults to 'Detect Language API ruby gem').
117
attr_accessor :user_agent
128

13-
# The host to connect to (defaults to ws.detectlanguage.com).
14-
attr_accessor :host
15-
16-
# The port on which your DetectLanguage server runs (defaults to 443 for secure
17-
# connections, 80 for insecure connections).
18-
attr_accessor :port
19-
20-
# +true+ for https connections, +false+ for http connections.
21-
attr_accessor :secure
9+
# API base URL
10+
attr_accessor :base_url
2211

2312
# The HTTP open timeout in seconds.
2413
attr_accessor :http_open_timeout
@@ -38,46 +27,10 @@ class Configuration
3827
# The password to use when logging into your proxy server (if using a proxy).
3928
attr_accessor :proxy_pass
4029

41-
alias_method :secure?, :secure
42-
4330
def initialize
44-
@api_key = nil
45-
@api_version = "0.2"
46-
@host = "ws.detectlanguage.com"
47-
@user_agent = "detectlanguage-ruby/#{VERSION}"
48-
end
49-
50-
def protocol
51-
if secure?
52-
'https'
53-
else
54-
'http'
55-
end
56-
end
57-
58-
def port
59-
@port || default_port
60-
end
61-
62-
# Allows config options to be read like a hash
63-
#
64-
# @param [Symbol] option Key for a given attribute
65-
def [](option)
66-
send(option)
31+
@api_key = nil
32+
@base_url = "https://ws.detectlanguage.com/0.2/"
33+
@user_agent = "detectlanguage-ruby/#{VERSION}"
6734
end
68-
69-
private
70-
71-
# Determines what port should we use for sending requests.
72-
# @return [Fixnum] Returns 443 if you've set secure to true in your
73-
# configuration, and 80 otherwise.
74-
def default_port
75-
if secure?
76-
443
77-
else
78-
80
79-
end
80-
end
81-
8235
end
8336
end

spec/detect_language_spec.rb

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
RSpec.describe DetectLanguage do
44
let(:api_key) { ENV['DETECTLANGUAGE_API_KEY'] }
5-
let(:secure) { true }
65

76
before do
8-
described_class.configuration.api_key = api_key
9-
described_class.configuration.secure = secure
7+
described_class.config.api_key = api_key
108
end
119

12-
describe '.configuration' do
13-
subject { described_class.configuration }
10+
describe '.config' do
11+
subject { described_class.config }
1412

1513
it 'has default configuration values' do
16-
expect(subject.api_version).to eq('0.2')
17-
expect(subject.host).to eq('ws.detectlanguage.com')
14+
expect(subject.base_url).to eq('https://ws.detectlanguage.com/0.2/')
1815
expect(subject.user_agent).to eq("detectlanguage-ruby/#{DetectLanguage::VERSION}")
1916
end
2017
end
@@ -90,13 +87,5 @@
9087
it 'fetches list of detectable languages' do
9188
expect(subject).to include('code' => 'en', 'name' => 'ENGLISH')
9289
end
93-
94-
context 'with http' do
95-
let(:secure) { false }
96-
97-
it 'fetches languages over http' do
98-
expect(subject).to include('code' => 'en', 'name' => 'ENGLISH')
99-
end
100-
end
10190
end
10291
end

0 commit comments

Comments
 (0)