Skip to content

Commit fbf7341

Browse files
committed
Update json_subdomain_redirector.rb
1 parent 3e042cf commit fbf7341

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

app/middleware/json_subdomain_redirector.rb

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,35 @@ def initialize(app)
1010
def call(env)
1111
request = Rack::Request.new(env)
1212

13-
if is_json?(request) && !already_on_api_subdomain?(request)
14-
is_versioned = request.path.match?(/^\/v\d+\//)
15-
new_path = is_versioned ? request.path : "/#{@api_version}#{request.path}"
16-
17-
# Reconstruct URL
18-
uri = URI.parse(request.url)
19-
uri.host = @api_domain
20-
uri.path = new_path
21-
22-
return [
23-
301,
24-
{ 'Location' => uri.to_s, 'Content-Type' => 'text/html' },
25-
['Moved Permanently']
26-
]
27-
end
28-
29-
@app.call(env)
13+
return @app.call(env) if
14+
!is_json?(request) ||
15+
is_core_json?(request) ||
16+
already_on_api_subdomain?(request)
17+
18+
is_versioned = request.path.match?(/^\/v\d+\//)
19+
new_path = is_versioned ? request.path : "/#{@api_version}#{request.path}"
20+
21+
# Reconstruct URL
22+
uri = URI.parse(request.url)
23+
uri.host = @api_domain
24+
uri.path = new_path
25+
26+
return [
27+
301,
28+
{ 'Location' => uri.to_s, 'Content-Type' => 'text/html' },
29+
['Moved Permanently']
30+
]
3031
end
3132

3233
def is_json?(request)
3334
request.get_header('HTTP_ACCEPT')&.include?('application/json') ||
3435
request.path.end_with?('.json')
3536
end
3637

38+
def is_core_json?(request)
39+
request.path.end_with?('autocomplete.json')
40+
end
41+
3742
def already_on_api_subdomain?(request)
3843
request.host == @api_domain
3944
end

0 commit comments

Comments
 (0)