15
15
'content-type' => 'application/json'
16
16
}
17
17
18
+ # GraphQL 用クエリはテンプレートから生成
18
19
def get_query_from_template ( filter )
19
20
query_template = <<~GRAPH_QL
20
21
query (
@@ -53,6 +54,7 @@ def get_query_from_template(filter)
53
54
GRAPH_QL
54
55
end
55
56
57
+ # Clubs API (GraphQL) へのクエリ送付結果を返す
56
58
def request_to_clubs_api ( query :, variables :)
57
59
request = Net ::HTTP ::Post . new ( API_URI . request_uri , HEADERS )
58
60
request . body = { query :, variables : } . to_json
@@ -73,6 +75,10 @@ def request_to_clubs_api(query:, variables:)
73
75
response_data [ :data ] [ :clubs ]
74
76
end
75
77
78
+ # Clubs API (GraphQL) 特有のループ処理と、
79
+ # 複数回クエリを投げるためクラブ情報の重複もチェックする
80
+ @dojo_data = [ ]
81
+ @unique_ids = Set . new
76
82
def fetch_responses_by_request ( page_number = 0 , query :, variables :)
77
83
begin
78
84
print "#{ page_number = page_number . succ } .."
@@ -88,42 +94,32 @@ def fetch_responses_by_request(page_number=0, query:, variables:)
88
94
end while page_info [ :hasNextPage ]
89
95
end
90
96
91
- # Variables to set fetched_data
92
- @dojo_data = [ ]
93
- @unique_ids = Set . new
94
-
95
97
# 日本国内の全クラブ情報を取得する
96
98
# CoderDojo Japan API で 'verified' は突合できるため省略
97
- variables = { after : nil } # Initialize to fetch from start.
99
+ # https://graphql.org/learn/queries/#variables
100
+ variables = { after : nil } # 次に読むページ番号の初期化
98
101
filter_by_jp = 'filterBy: { countryCode: "JP" }'
99
102
query = get_query_from_template ( filter_by_jp )
100
103
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
- # }
110
104
fetch_responses_by_request ( query : query , variables : variables )
111
105
puts " (JP: #{ @dojo_data . count } )"
112
106
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 } # 次に読むページ番号の初期化
115
110
filter_by_brands = 'filterBy: { brand: CODERDOJO, verified: true }'
116
111
query = get_query_from_template ( filter_by_brands )
117
112
print 'ALL_DOJOS_QUERY: '
118
113
fetch_responses_by_request ( query : query , variables : variables )
119
114
puts " (Total: #{ @dojo_data . count } )"
120
115
116
+ # API から取得した結果を JSON にしてファイルに書き込む
121
117
File . write ( 'tmp/number_of_dojos' , @dojo_data . length )
122
118
File . open ( 'dojos_earth.json' , 'w' ) do |file |
123
119
file . puts JSON . pretty_generate ( @dojo_data . sort_by { |dojo | dojo [ :id ] } )
124
120
end
125
121
126
- # Show next step for developers
122
+ # 次のステップを出力
127
123
#puts DOJOS_JSON
128
124
puts ''
129
125
puts 'Fetched number of dojos: ' + File . read ( 'tmp/number_of_dojos' )
0 commit comments