98
98
}
99
99
GRAPH_QL
100
100
101
- def request_data ( query :, variables :)
101
+ def request_to_clubs_api ( query :, variables :)
102
102
request = Net ::HTTP ::Post . new ( API_URI . request_uri , HEADERS )
103
103
request . body = { query :, variables : } . to_json
104
104
req_options = { use_ssl : API_URI . scheme == 'https' }
@@ -118,49 +118,41 @@ def request_data(query:, variables:)
118
118
response_data [ :data ] [ :clubs ]
119
119
end
120
120
121
- dojo_data = [ ]
122
- unique_ids = Set . new
123
- page_number = 0
121
+ @dojo_data = [ ]
122
+ @unique_ids = Set . new
124
123
125
124
# Fetch clubs for Japan without filtering by brand
126
125
variables = { after : nil }
127
126
query = JP_DOJOS_QUERY
128
127
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
136
138
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 } )"
142
145
143
146
# Fetch clubs for other countries with filtering by brand
144
147
variables = { after : nil }
145
148
query = ALL_DOJOS_QUERY
146
149
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 } )"
160
152
161
- File . write ( 'tmp/number_of_dojos' , dojo_data . length )
153
+ File . write ( 'tmp/number_of_dojos' , @ dojo_data. length )
162
154
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 ] } )
164
156
end
165
157
166
158
# Show next step for developers
0 commit comments