|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "yaml" |
| 5 | +require "erb" |
| 6 | +require "fileutils" |
| 7 | + |
| 8 | +SPEC_PATH = File.expand_path("../openapi/paystack.yaml", __dir__) |
| 9 | +TEMPLATE_PATH = File.expand_path("templates/resource.erb", __dir__) |
| 10 | +OUTPUT_DIR = File.expand_path("../lib/paystack/resources", __dir__) |
| 11 | + |
| 12 | +RESOURCES = %w[Transaction Customer].freeze |
| 13 | + |
| 14 | +def snake_case(str) |
| 15 | + str |
| 16 | + .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') |
| 17 | + .gsub(/([a-z\d])([A-Z])/, '\1_\2') |
| 18 | + .downcase |
| 19 | +end |
| 20 | + |
| 21 | +# Strip the tag prefix from an operationId and convert to snake_case. |
| 22 | +# e.g. "transaction_chargeAuthorization" -> "charge_authorization" |
| 23 | +# "customer_riskAction" -> "risk_action" |
| 24 | +def method_name(operation_id, tag) |
| 25 | + without_prefix = operation_id.sub(/\A#{Regexp.escape(tag)}_/i, "") |
| 26 | + snake_case(without_prefix) |
| 27 | +end |
| 28 | + |
| 29 | +# Build the method signature from path params and whether the op has a body or query. |
| 30 | +def build_signature(path_params, http_method, has_body) |
| 31 | + parts = path_params.map { |p| "#{snake_case(p)}:" } |
| 32 | + |
| 33 | + if has_body |
| 34 | + parts << "**params" |
| 35 | + elsif %w[get delete].include?(http_method) |
| 36 | + parts << "**params" |
| 37 | + end |
| 38 | + |
| 39 | + parts.join(", ") |
| 40 | +end |
| 41 | + |
| 42 | +# Build the transport path, interpolating path params as Ruby string interpolation. |
| 43 | +def build_transport_path(path, path_params) |
| 44 | + result = path.dup |
| 45 | + path_params.each do |p| |
| 46 | + result = result.gsub("{#{p}}", "\#{#{snake_case(p)}}") |
| 47 | + end |
| 48 | + result |
| 49 | +end |
| 50 | + |
| 51 | +# Build the trailing args for the transport call. |
| 52 | +def build_transport_args(http_method, has_body, path_params) |
| 53 | + uses_params = has_body || %w[get delete].include?(http_method) |
| 54 | + return "" unless uses_params |
| 55 | + |
| 56 | + if has_body |
| 57 | + ", body: params" |
| 58 | + else |
| 59 | + ", query: params" |
| 60 | + end |
| 61 | +end |
| 62 | + |
| 63 | +spec = YAML.load_file(SPEC_PATH, aliases: true) |
| 64 | +template = ERB.new(File.read(TEMPLATE_PATH), trim_mode: "-") |
| 65 | + |
| 66 | +RESOURCES.each do |tag| |
| 67 | + operations = [] |
| 68 | + |
| 69 | + spec["paths"].each do |path, path_item| |
| 70 | + # Path-level parameters (shared across methods on this path) |
| 71 | + shared_params = (path_item["parameters"] || []) |
| 72 | + .select { |p| p.is_a?(Hash) && p["in"] == "path" } |
| 73 | + .map { |p| p["name"] } |
| 74 | + |
| 75 | + path_item.each do |http_method, op| |
| 76 | + next unless op.is_a?(Hash) && op["tags"]&.include?(tag) |
| 77 | + |
| 78 | + op_path_params = shared_params + op.fetch("parameters", []) |
| 79 | + .select { |p| p.is_a?(Hash) && p["in"] == "path" } |
| 80 | + .map { |p| p["name"] } |
| 81 | + |
| 82 | + has_body = op.key?("requestBody") |
| 83 | + op_id = op["operationId"] |
| 84 | + name = method_name(op_id, tag) |
| 85 | + |
| 86 | + transport_path = build_transport_path(path, op_path_params) |
| 87 | + uses_params = has_body || %w[get delete].include?(http_method) |
| 88 | + |
| 89 | + operations << { |
| 90 | + summary: op["summary"] || op_id, |
| 91 | + http_method: http_method, |
| 92 | + path: path, |
| 93 | + method_name: name, |
| 94 | + signature: build_signature(op_path_params, http_method, has_body), |
| 95 | + transport_path: transport_path, |
| 96 | + transport_args: build_transport_args(http_method, has_body, op_path_params), |
| 97 | + uses_params: uses_params |
| 98 | + } |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + class_name = tag |
| 103 | + output = template.result(binding) |
| 104 | + |
| 105 | + FileUtils.mkdir_p(OUTPUT_DIR) |
| 106 | + out_path = File.join(OUTPUT_DIR, "#{snake_case(tag)}.rb") |
| 107 | + File.write(out_path, output) |
| 108 | + puts "Generated #{out_path} (#{operations.size} operations)" |
| 109 | +end |
0 commit comments