Skip to content

Commit 574ece8

Browse files
authored
Improve rails document client response matching (#138)
* Fix spacing between constants and sigs * Use HTTP response classes instead of code
1 parent ac98b59 commit 574ece8

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/ruby_lsp/ruby_lsp_rails/support/rails_document_client.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Rails
88
module Support
99
class RailsDocumentClient
1010
RAILS_DOC_HOST = "https://api.rubyonrails.org"
11+
1112
SUPPORTED_RAILS_DOC_NAMESPACES = T.let(
1213
Regexp.union(
1314
/ActionDispatch/,
@@ -31,9 +32,8 @@ class RailsDocumentClient
3132

3233
class << self
3334
extend T::Sig
34-
sig do
35-
params(name: String).returns(T::Array[String])
36-
end
35+
36+
sig { params(name: String).returns(T::Array[String]) }
3737
def generate_rails_document_urls(name)
3838
docs = search_index&.fetch(name, nil)
3939

@@ -70,18 +70,20 @@ def generate_rails_document_urls(name)
7070

7171
response = Net::HTTP.get_response(URI("#{RAILS_DOC_HOST}/v#{RAILTIES_VERSION}/js/search_index.js"))
7272

73-
if response.code == "302"
73+
body = case response
74+
when Net::HTTPSuccess
75+
response.body
76+
when Net::HTTPRedirection
7477
# If the version's doc is not found, e.g. Rails main, it'll be redirected
7578
# In this case, we just fetch the latest doc
7679
response = Net::HTTP.get_response(URI("#{RAILS_DOC_HOST}/js/search_index.js"))
77-
end
78-
79-
if response.code == "200"
80-
process_search_index(response.body)
80+
response.body if response.is_a?(Net::HTTPSuccess)
8181
else
8282
warn("Response failed: #{response.inspect}")
8383
nil
8484
end
85+
86+
process_search_index(body) if body
8587
rescue StandardError => e
8688
warn("Exception occurred when fetching Rails document index: #{e.inspect}")
8789
end

0 commit comments

Comments
 (0)