Class: Phantasma::Documentation
- Inherits:
-
Object
- Object
- Phantasma::Documentation
- Defined in:
- lib/phantasma/documentation.rb
Constant Summary collapse
- SWAGGER_URL =
Based on test swagger json generate documentation testnet.phantasma.info/swagger/v1/swagger.json
'https://testnet.phantasma.info/swagger/v1/swagger.json'
Class Method Summary collapse
- .documentation_dir ⇒ Object
- .generate_documentation ⇒ Object
- .generate_ruby_hash(parameters) ⇒ Object
- .http_request ⇒ Object
Class Method Details
.documentation_dir ⇒ Object
68 69 70 |
# File 'lib/phantasma/documentation.rb', line 68 def self.documentation_dir "#{Dir.pwd.gsub('lib/phantasma', 'docs')}/api_documentation.md" end |
.generate_documentation ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/phantasma/documentation.rb', line 12 def self.generate_documentation api_data = http_request # Start generating the documentation File.open(documentation_dir, 'w') do |file| api_data['paths'].each do |path, methods| methods.each do |method, details| endpoint = path.gsub('/api/v1/', '') if endpoint == 'GetValidators/{type}' # special case endpoint = 'GetValidatorByType' end ruby_method_name = Phantasma::Helpers.camel_to_snake(endpoint.split('/').last) parameters = details['parameters'] || [] file.puts "### #{details['tags'].first} - #{ruby_method_name}" file.puts "- **Endpoint**: `#{path}`" file.puts "- **Method**: `#{method.upcase}`" file.puts '- **Parameters**:' if parameters.empty? file.puts ' - None' else parameters.each do |param| if param.dig('schema', 'default').nil? file.puts " - `#{param['name']}`: #{param.dig('schema', 'type')}" else file.puts " - `#{param['name']}`: #{param.dig('schema', 'type')}, `default`: #{param.dig('schema', 'default')}" end end end file.puts '- **Ruby Example**:' file.puts '```ruby' if parameters.empty? file.puts "api.#{ruby_method_name}" else file.puts "api.#{ruby_method_name}(#{generate_ruby_hash(parameters) unless parameters.empty?})" end file.puts '```' file.puts '' end end end puts "Documentation generated in #{documentation_dir}" end |
.generate_ruby_hash(parameters) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/phantasma/documentation.rb', line 72 def self.generate_ruby_hash(parameters) params = parameters.map do |param| if param.dig('schema', 'default').nil? "#{param['name']}: '#{param.dig('schema', 'type')}'" else if %w(boolean integer).include?(param.dig('schema', 'type')) "#{param['name']}: #{param.dig('schema', 'default')}" else "#{param['name']}: '#{param.dig('schema', 'default')}'" end end end "{#{params.join(', ')}}" end |
.http_request ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/phantasma/documentation.rb', line 58 def self.http_request url = URI(SWAGGER_URL) https = Net::HTTP.new(url.host, url.port) https.use_ssl = true request = Net::HTTP::Get.new(url) response = https.request(request) json_data = response.read_body JSON.parse(json_data) end |