15
15
'content-type' => 'application/json'
16
16
}
17
17
18
- ALL_DOJOS_QUERY = <<~GRAPH_QL
18
+ def get_query_from_template ( filter )
19
+ query_template = <<~GRAPH_QL
19
20
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,
22
24
#
23
25
# This 'after' has which page we have read and the next page to read.
24
26
$after: String,
25
27
) {
26
28
clubs(
27
29
after: $after,
28
30
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 }
78
32
) {
79
33
nodes {
80
34
name
96
50
}
97
51
}
98
52
}
99
- GRAPH_QL
53
+ GRAPH_QL
54
+ end
100
55
101
56
def request_to_clubs_api ( query :, variables :)
102
57
request = Net ::HTTP ::Post . new ( API_URI . request_uri , HEADERS )
@@ -118,14 +73,6 @@ def request_to_clubs_api(query:, variables:)
118
73
response_data [ :data ] [ :clubs ]
119
74
end
120
75
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
-
129
76
def fetch_responses_by_request ( page_number = 0 , query :, variables :)
130
77
begin
131
78
print "#{ page_number = page_number . succ } .."
@@ -140,12 +87,33 @@ def fetch_responses_by_request(page_number=0, query:, variables:)
140
87
variables [ :after ] = page_info [ :endCursor ]
141
88
end while page_info [ :hasNextPage ]
142
89
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
+ # }
143
110
fetch_responses_by_request ( query : query , variables : variables )
144
111
puts " (JP: #{ @dojo_data . count } )"
145
112
146
113
# 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 )
149
117
print 'ALL_DOJOS_QUERY: '
150
118
fetch_responses_by_request ( query : query , variables : variables )
151
119
puts " (Total: #{ @dojo_data . count } )"
0 commit comments