Skip to content

Commit 88a2e7e

Browse files
committed
Refactor: それぞれの共通化した処理などに(将来の自分用に)コメントを付記
1 parent d7938a3 commit 88a2e7e

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

get_data_from_earth.rb

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'content-type' => 'application/json'
1616
}
1717

18+
# GraphQL 用クエリはテンプレートから生成
1819
def get_query_from_template(filter)
1920
query_template = <<~GRAPH_QL
2021
query (
@@ -53,6 +54,7 @@ def get_query_from_template(filter)
5354
GRAPH_QL
5455
end
5556

57+
# Clubs API (GraphQL) へのクエリ送付結果を返す
5658
def request_to_clubs_api(query:, variables:)
5759
request = Net::HTTP::Post.new(API_URI.request_uri, HEADERS)
5860
request.body = { query:, variables: }.to_json
@@ -73,6 +75,10 @@ def request_to_clubs_api(query:, variables:)
7375
response_data[:data][:clubs]
7476
end
7577

78+
# Clubs API (GraphQL) 特有のループ処理と、
79+
# 複数回クエリを投げるためクラブ情報の重複もチェックする
80+
@dojo_data = []
81+
@unique_ids = Set.new
7682
def fetch_responses_by_request(page_number=0, query:, variables:)
7783
begin
7884
print "#{page_number = page_number.succ}.."
@@ -88,42 +94,32 @@ def fetch_responses_by_request(page_number=0, query:, variables:)
8894
end while page_info[:hasNextPage]
8995
end
9096

91-
# Variables to set fetched_data
92-
@dojo_data = []
93-
@unique_ids = Set.new
94-
9597
# 日本国内の全クラブ情報を取得する
9698
# CoderDojo Japan API で 'verified' は突合できるため省略
97-
variables = { after: nil } # Initialize to fetch from start.
99+
# https://graphql.org/learn/queries/#variables
100+
variables = { after: nil } # 次に読むページ番号の初期化
98101
filter_by_jp = 'filterBy: { countryCode: "JP" }'
99102
query = get_query_from_template(filter_by_jp)
100103
print ' JP_DOJOS_QUERY: '
101-
102-
103-
# This 'variables' is fixed parameter name and cannot be renamed.
104-
# https://graphql.org/learn/queries/#variables
105-
#
106-
# variables = {
107-
# # MEMO: No need to filter to fetch all dojo data on earth.
108-
# # countryCode: 'JP'
109-
# }
110104
fetch_responses_by_request(query: query, variables: variables)
111105
puts " (JP: #{@dojo_data.count})"
112106

113-
# Fetch clubs for other countries with filtering by brand
114-
variables = { after: nil }
107+
# 'CODERDOJO' ブランドで承認された全クラブ情報を取得する
108+
# https://graphql.org/learn/queries/#variables
109+
variables = { after: nil } # 次に読むページ番号の初期化
115110
filter_by_brands = 'filterBy: { brand: CODERDOJO, verified: true }'
116111
query = get_query_from_template(filter_by_brands)
117112
print 'ALL_DOJOS_QUERY: '
118113
fetch_responses_by_request(query: query, variables: variables)
119114
puts " (Total: #{@dojo_data.count})"
120115

116+
# API から取得した結果を JSON にしてファイルに書き込む
121117
File.write('tmp/number_of_dojos', @dojo_data.length)
122118
File.open('dojos_earth.json', 'w') do |file|
123119
file.puts JSON.pretty_generate(@dojo_data.sort_by{|dojo| dojo[:id]})
124120
end
125121

126-
# Show next step for developers
122+
# 次のステップを出力
127123
#puts DOJOS_JSON
128124
puts ''
129125
puts 'Fetched number of dojos: ' + File.read('tmp/number_of_dojos')

0 commit comments

Comments
 (0)