Skip to content

Commit 3a81104

Browse files
committed
Fetch all clubs data in JP and CODERDOJO brand's data on Earth
1 parent a3942f4 commit 3a81104

File tree

1 file changed

+76
-12
lines changed

1 file changed

+76
-12
lines changed

get_data_from_earth.rb

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'net/http'
44
require 'uri'
55
require 'json'
6+
require 'set'
67

78
# Set default number_of_dojos to detect if
89
# Clubs API has breaking changes in Actions
@@ -59,35 +60,98 @@
5960

6061
# This 'variables' is fixed parameter name and cannot be renamed.
6162
# https://graphql.org/learn/queries/#variables
62-
variables = {
63-
# MEMO: No need to filter to fetch all dojo data on earth.
64-
# countryCode: 'JP'
65-
}
63+
#variables = {
64+
# # MEMO: No need to filter to fetch all dojo data on earth.
65+
# # countryCode: 'JP'
66+
#}
67+
JP_DOJOS_QUERY = <<~GRAPH_QL
68+
query (
69+
$after: String
70+
) {
71+
clubs(
72+
after: $after,
73+
first: 400,
74+
filterBy: {
75+
countryCode: "JP",
76+
verified: true
77+
}
78+
) {
79+
nodes {
80+
name
81+
latitude
82+
longitude
83+
countryCode
84+
stage
85+
urlSlug: url
86+
startTime
87+
endTime
88+
openToPublic
89+
frequency
90+
day
91+
id: uuid
92+
}
93+
pageInfo {
94+
endCursor
95+
hasNextPage
96+
}
97+
}
98+
}
99+
GRAPH_QL
66100

67-
def request_data(variables:)
101+
def request_data(query:, variables:)
68102
request = Net::HTTP::Post.new(API_URI.request_uri, HEADERS)
69-
request.body = { query: ALL_DOJOS_QUERY, variables: }.to_json
103+
request.body = { query:, variables: }.to_json
70104
req_options = { use_ssl: API_URI.scheme == 'https' }
71105

72106
response = Net::HTTP.start(API_URI.hostname, API_URI.port, req_options) do |http|
73107
http.request(request)
74108
end
75109

76110
# pp JSON.parse(response.body, symbolize_names: true)
77-
JSON.parse(response.body, symbolize_names: true)[:data][:clubs]
111+
# JSON.parse(response.body, symbolize_names: true)[:data][:clubs]
112+
response_data = JSON.parse(response.body, symbolize_names: true)
113+
if response_data[:errors]
114+
puts "Error: #{response_data[:errors]}"
115+
return { nodes: [], pageInfo: { endCursor: nil, hasNextPage: false } }
116+
end
117+
118+
response_data[:data][:clubs]
78119
end
79120

80121
dojo_data = []
122+
unique_ids = Set.new
81123
page_number = 0
82124
print 'Fetching page by page: '
125+
126+
# Fetch clubs for Japan without filtering by brand
127+
variables = { after: nil }
128+
query = JP_DOJOS_QUERY
83129
begin
84130
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])
136+
end
137+
end
138+
page_info = fetched_data[:pageInfo]
139+
variables[:after] = page_info[:endCursor]
140+
end while page_info[:hasNextPage]
85141

86-
fetched_data = request_data(variables:)
87-
dojo_data += fetched_data[:nodes]
88-
page_info = fetched_data[:pageInfo]
89-
#puts fetched_data[:pageInfo], fetched_data[:nodes].first
90-
142+
# Fetch clubs for other countries with filtering by brand
143+
variables = { after: nil }
144+
query = ALL_DOJOS_QUERY
145+
begin
146+
print "#{page_number = page_number.succ}.."
147+
fetched_data = request_data(query: query, variables: variables)
148+
fetched_data[:nodes].each do |dojo|
149+
unless unique_ids.include?(dojo[:id])
150+
dojo_data << dojo
151+
unique_ids.add(dojo[:id])
152+
end
153+
end
154+
page_info = fetched_data[:pageInfo]
91155
variables[:after] = page_info[:endCursor]
92156
end while page_info[:hasNextPage]
93157

0 commit comments

Comments
 (0)