Skip to content

Commit 3280bf9

Browse files
committed
Re-generate methods in the existing format.
1 parent ffb1f58 commit 3280bf9

File tree

4 files changed

+176
-11
lines changed

4 files changed

+176
-11
lines changed

.rubocop_todo.yml

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-09-06 22:02:31 UTC using RuboCop version 1.80.2.
3+
# on 2025-09-06 22:39:09 UTC using RuboCop version 1.80.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
9+
# Offense count: 3
1010
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
1111
Metrics/AbcSize:
12-
Max: 24
12+
Max: 49
13+
14+
# Offense count: 1
15+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
16+
# AllowedMethods: refine
17+
Metrics/BlockLength:
18+
Max: 27
1319

1420
# Offense count: 1
21+
# Configuration parameters: CountComments, CountAsOne.
22+
Metrics/ClassLength:
23+
Max: 108
24+
25+
# Offense count: 2
26+
# Configuration parameters: AllowedMethods, AllowedPatterns.
27+
Metrics/CyclomaticComplexity:
28+
Max: 20
29+
30+
# Offense count: 5
1531
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
1632
Metrics/MethodLength:
17-
Max: 16
33+
Max: 32
34+
35+
# Offense count: 1
36+
# Configuration parameters: AllowedMethods, AllowedPatterns.
37+
Metrics/PerceivedComplexity:
38+
Max: 19
39+
40+
# Offense count: 1
41+
# This cop supports safe autocorrection (--autocorrect).
42+
Rake/Desc:
43+
Exclude:
44+
- 'tasks/update.rake'
1845

1946
# Offense count: 3
2047
Security/Open:
2148
Exclude:
2249
- 'tasks/lib/docs/events_downloader.rb'
2350
- 'tasks/lib/docs/methods_downloader.rb'
2451

25-
# Offense count: 3
52+
# Offense count: 4
2653
# Configuration parameters: AllowedConstants.
2754
Style/Documentation:
2855
Exclude:
@@ -31,8 +58,9 @@ Style/Documentation:
3158
- 'tasks/lib/docs/downloader.rb'
3259
- 'tasks/lib/docs/events_downloader.rb'
3360
- 'tasks/lib/docs/methods_downloader.rb'
61+
- 'tasks/lib/slack_api/methods_generator.rb'
3462

35-
# Offense count: 5
63+
# Offense count: 6
3664
# This cop supports unsafe autocorrection (--autocorrect-all).
3765
# Configuration parameters: EnforcedStyle.
3866
# SupportedStyles: always, always_true, never
@@ -41,6 +69,7 @@ Style/FrozenStringLiteralComment:
4169
- '**/*.arb'
4270
- 'tasks/events.rake'
4371
- 'tasks/lib/docs/downloader.rb'
72+
- 'tasks/lib/slack_api/methods_generator.rb'
4473
- 'tasks/methods.rake'
4574
- 'tasks/update.rake'
4675
- 'tasks/validate.rake'

tasks/lib/docs/downloader.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ def download!
1313
events_downloader.download!
1414
end
1515

16-
private
17-
1816
def methods_downloader
1917
@methods_downloader ||= MethodsDownloader.new(target_path)
2018
end
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
module SlackApi
2+
class MethodsGenerator
3+
def downloader
4+
@downloader ||= SlackApi::Docs::Downloader.new
5+
end
6+
7+
def generate!
8+
methods = JSON.load_file(downloader.methods_downloader.methods_path)
9+
groups = Set.new
10+
11+
methods.each do |method_data|
12+
groups += method_data['family']
13+
14+
method_filename = downloader.methods_downloader.method_target_path(method_data['name'])
15+
data = JSON.load_file(method_filename)
16+
17+
process_method(
18+
data.merge(
19+
'name' => method_data['name'],
20+
'group' => method_data['family'].first.split('.').first
21+
)
22+
)
23+
end
24+
end
25+
26+
private
27+
28+
def process_method(data)
29+
args, fields = parse_args(data)
30+
31+
errors = parse_errors(data)
32+
response = parse_response(data)
33+
34+
result = {
35+
group: data['group'],
36+
name: data['name'],
37+
deprecated: false,
38+
desc: data['desc'],
39+
args: args,
40+
response: response,
41+
errors: errors
42+
}.merge(fields)
43+
44+
filename = "methods/#{data['group']}/#{data['name']}.json"
45+
FileUtils.mkdir_p("methods/#{data['group']}")
46+
File.write(filename, JSON.pretty_generate(result))
47+
end
48+
49+
def parse_args(data)
50+
args = {}
51+
fields = {}
52+
53+
data['args']['properties'].each_pair do |name, arg|
54+
all = []
55+
arg['anyOf']&.each do |coll|
56+
all << coll['type'] unless coll['type'] == 'null'
57+
end
58+
arg['type'] = Array(all).length == 1 ? all.first : all if all.any?
59+
arg.delete('anyOf')
60+
61+
type = massage_type(name, arg, data)
62+
required = data['args']['required']&.include?(name)
63+
64+
desc = arg['desc']
65+
&.tap { |t| t.slice!("\n") }
66+
&.tap { |t| t << '.' unless t.end_with?('.') }
67+
&.gsub('’', "'")
68+
example = arg['example'] || arg['default']
69+
70+
case name
71+
when 'token'
72+
# ignore token, always required
73+
when 'count', 'page'
74+
fields['has_paging'] = true
75+
fields['default_count'] = 100
76+
else
77+
h = {}
78+
h['required'] = required
79+
h['example'] = example if example
80+
h['desc'] = desc if desc
81+
h['type'] = type if type
82+
h['format'] = 'json' if desc&.include?('JSON')
83+
args[name] = h
84+
end
85+
end
86+
87+
[args.sort.to_h, fields]
88+
end
89+
90+
def massage_type(name, arg, data = {})
91+
return 'enum' if arg.key?('enum')
92+
93+
case name
94+
when 'date' then 'date'
95+
when 'latest', 'oldest', 'ts' then 'timestamp'
96+
when 'file' then 'file'
97+
when 'bot', 'user' then 'user'
98+
when 'channel'
99+
case data[:method_group]
100+
when 'im' then 'im'
101+
when 'mpim' then 'mpim'
102+
when 'groups' then 'group'
103+
else 'channel'
104+
end
105+
else
106+
case detected = arg['type']
107+
when '', 'null' then nil
108+
else detected
109+
end
110+
end
111+
end
112+
113+
def parse_response(data)
114+
examples = []
115+
data['examples']&.each_pair do |_example_type, response|
116+
example = JSON.pretty_generate(response['example'], indent: ' ')
117+
example.gsub!(/\{\n\s*\}/, '{}')
118+
example.gsub!(/\[\n\s*\]/, '[]')
119+
examples.push example
120+
end
121+
{ 'examples' => examples }
122+
end
123+
124+
def parse_errors(data)
125+
errors = {}
126+
data['errors']&.each_pair do |name, desc|
127+
errors[name] = desc['desc']
128+
end
129+
errors
130+
end
131+
end
132+
end

tasks/update.rake

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
require_relative 'lib/slack_api/spec_validator'
1+
require_relative 'lib/slack_api/methods_generator'
22

33
namespace :api do
4-
desc 'Validate scraped methods and events are valid.'
4+
namespace :methods do
5+
desc 'Update methods.'
6+
task :update do
7+
SlackApi::MethodsGenerator.new.generate!
8+
end
9+
end
10+
511
task :update do
6-
puts 'TODO'
12+
Rake::Task['api:methods:update'].invoke
713
end
814
end

0 commit comments

Comments
 (0)