Skip to content

Commit d7938a3

Browse files
committed
Refactor: GraphQL の共通部分をテンプレート化
1 parent a80451c commit d7938a3

File tree

1 file changed

+31
-63
lines changed

1 file changed

+31
-63
lines changed

get_data_from_earth.rb

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,66 +15,20 @@
1515
'content-type' => 'application/json'
1616
}
1717

18-
ALL_DOJOS_QUERY = <<~GRAPH_QL
18+
def get_query_from_template(filter)
19+
query_template = <<~GRAPH_QL
1920
query (
20-
# No need to filter to fetch all dojo data on earth.
21-
# $countryCode: String!,
21+
# No need to filter to fetch all dojo data on earth like:
22+
# $countryCode: String!,
23+
# countryCode: $countryCode,
2224
#
2325
# This 'after' has which page we have read and the next page to read.
2426
$after: String,
2527
) {
2628
clubs(
2729
after: $after,
2830
first: 400,
29-
filterBy: {
30-
# No need to filter to fetch all dojo data on earth.
31-
# countryCode: $countryCode,
32-
#
33-
# For DEBUG add this "JP" filter
34-
#countryCode: "JP",
35-
brand: CODERDOJO,
36-
verified: true
37-
}
38-
) {
39-
nodes {
40-
name
41-
latitude
42-
longitude
43-
countryCode
44-
stage
45-
urlSlug: url
46-
id: uuid
47-
#startTime
48-
#endTime
49-
#openToPublic
50-
#frequency
51-
#day
52-
}
53-
pageInfo {
54-
endCursor
55-
hasNextPage
56-
}
57-
}
58-
}
59-
GRAPH_QL
60-
61-
# This 'variables' is fixed parameter name and cannot be renamed.
62-
# https://graphql.org/learn/queries/#variables
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 # This does NOT change on number_of_dojos
77-
}
31+
#{filter}
7832
) {
7933
nodes {
8034
name
@@ -96,7 +50,8 @@
9650
}
9751
}
9852
}
99-
GRAPH_QL
53+
GRAPH_QL
54+
end
10055

10156
def request_to_clubs_api(query:, variables:)
10257
request = Net::HTTP::Post.new(API_URI.request_uri, HEADERS)
@@ -118,14 +73,6 @@ def request_to_clubs_api(query:, variables:)
11873
response_data[:data][:clubs]
11974
end
12075

121-
@dojo_data = []
122-
@unique_ids = Set.new
123-
124-
# Fetch clubs for Japan without filtering by brand
125-
variables = { after: nil }
126-
query = JP_DOJOS_QUERY
127-
print ' JP_DOJOS_QUERY: '
128-
12976
def fetch_responses_by_request(page_number=0, query:, variables:)
13077
begin
13178
print "#{page_number = page_number.succ}.."
@@ -140,12 +87,33 @@ def fetch_responses_by_request(page_number=0, query:, variables:)
14087
variables[:after] = page_info[:endCursor]
14188
end while page_info[:hasNextPage]
14289
end
90+
91+
# Variables to set fetched_data
92+
@dojo_data = []
93+
@unique_ids = Set.new
94+
95+
# 日本国内の全クラブ情報を取得する
96+
# CoderDojo Japan API で 'verified' は突合できるため省略
97+
variables = { after: nil } # Initialize to fetch from start.
98+
filter_by_jp = 'filterBy: { countryCode: "JP" }'
99+
query = get_query_from_template(filter_by_jp)
100+
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+
# }
143110
fetch_responses_by_request(query: query, variables: variables)
144111
puts " (JP: #{@dojo_data.count})"
145112

146113
# Fetch clubs for other countries with filtering by brand
147-
variables = { after: nil }
148-
query = ALL_DOJOS_QUERY
114+
variables = { after: nil }
115+
filter_by_brands = 'filterBy: { brand: CODERDOJO, verified: true }'
116+
query = get_query_from_template(filter_by_brands)
149117
print 'ALL_DOJOS_QUERY: '
150118
fetch_responses_by_request(query: query, variables: variables)
151119
puts " (Total: #{@dojo_data.count})"

0 commit comments

Comments
 (0)