Skip to content

Commit a80451c

Browse files
committed
Refactor: 重複している API レスポンスの Loop 処理を共通化
1 parent 87d3b67 commit a80451c

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

get_data_from_earth.rb

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
}
9999
GRAPH_QL
100100

101-
def request_data(query:, variables:)
101+
def request_to_clubs_api(query:, variables:)
102102
request = Net::HTTP::Post.new(API_URI.request_uri, HEADERS)
103103
request.body = { query:, variables: }.to_json
104104
req_options = { use_ssl: API_URI.scheme == 'https' }
@@ -118,49 +118,41 @@ def request_data(query:, variables:)
118118
response_data[:data][:clubs]
119119
end
120120

121-
dojo_data = []
122-
unique_ids = Set.new
123-
page_number = 0
121+
@dojo_data = []
122+
@unique_ids = Set.new
124123

125124
# Fetch clubs for Japan without filtering by brand
126125
variables = { after: nil }
127126
query = JP_DOJOS_QUERY
128127
print ' JP_DOJOS_QUERY: '
129-
begin
130-
print "#{page_number = page_number.succ}.."
131-
fetched_data = request_data(query: query, variables: variables)
132-
fetched_data[:nodes].each do |dojo|
133-
unless unique_ids.include?(dojo[:id])
134-
dojo_data << dojo
135-
unique_ids.add(dojo[:id])
128+
129+
def fetch_responses_by_request(page_number=0, query:, variables:)
130+
begin
131+
print "#{page_number = page_number.succ}.."
132+
fetched_data = request_to_clubs_api(query: query, variables: variables)
133+
fetched_data[:nodes].each do |dojo|
134+
unless @unique_ids.include?(dojo[:id])
135+
@dojo_data << dojo
136+
@unique_ids.add(dojo[:id])
137+
end
136138
end
137-
end
138-
page_info = fetched_data[:pageInfo]
139-
variables[:after] = page_info[:endCursor]
140-
end while page_info[:hasNextPage]
141-
puts " (JP: #{dojo_data.count})"
139+
page_info = fetched_data[:pageInfo]
140+
variables[:after] = page_info[:endCursor]
141+
end while page_info[:hasNextPage]
142+
end
143+
fetch_responses_by_request(query: query, variables: variables)
144+
puts " (JP: #{@dojo_data.count})"
142145

143146
# Fetch clubs for other countries with filtering by brand
144147
variables = { after: nil }
145148
query = ALL_DOJOS_QUERY
146149
print 'ALL_DOJOS_QUERY: '
147-
begin
148-
print "#{page_number = page_number.succ}.."
149-
fetched_data = request_data(query: query, variables: variables)
150-
fetched_data[:nodes].each do |dojo|
151-
unless unique_ids.include?(dojo[:id])
152-
dojo_data << dojo
153-
unique_ids.add(dojo[:id])
154-
end
155-
end
156-
page_info = fetched_data[:pageInfo]
157-
variables[:after] = page_info[:endCursor]
158-
end while page_info[:hasNextPage]
159-
puts " (Total: #{dojo_data.count})"
150+
fetch_responses_by_request(query: query, variables: variables)
151+
puts " (Total: #{@dojo_data.count})"
160152

161-
File.write('tmp/number_of_dojos', dojo_data.length)
153+
File.write('tmp/number_of_dojos', @dojo_data.length)
162154
File.open('dojos_earth.json', 'w') do |file|
163-
file.puts JSON.pretty_generate(dojo_data.sort_by{|dojo| dojo[:id]})
155+
file.puts JSON.pretty_generate(@dojo_data.sort_by{|dojo| dojo[:id]})
164156
end
165157

166158
# Show next step for developers

0 commit comments

Comments
 (0)