Class: Wrappix::Templates::Readme
- Inherits:
-
Object
- Object
- Wrappix::Templates::Readme
- Defined in:
- lib/wrappix/templates/readme.rb
Class Method Summary collapse
- .auth_setup_instructions(config) ⇒ Object
- .generate_resource_docs(_module_name, config) ⇒ Object
- .render(api_name, module_name, config) ⇒ Object
- .usage_examples(_module_name, config) ⇒ Object
Class Method Details
.auth_setup_instructions(config) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/wrappix/templates/readme.rb', line 146 def self.auth_setup_instructions(config) case config["auth_type"] when "oauth" " config.client_id = \"your_client_id\"\n config.client_secret = \"your_client_secret\"\n config.access_token = \"your_access_token\"\n RUBY\n when \"basic\"\n <<~RUBY.strip\n config.username = \"your_username\"\n config.password = \"your_password\"\n RUBY\n when \"api_key\"\n <<~RUBY.strip\n config.api_key = \"your_api_key\"\n config.api_key_header = \"\#{config[\"api_key_header\"] || \"X-Api-Key\"}\"\n RUBY\n else\n \"# No authentication required\"\n end\nend\n".strip |
.generate_resource_docs(_module_name, config) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/wrappix/templates/readme.rb', line 205 def self.generate_resource_docs(_module_name, config) resources = config["resources"] || {} docs = [] resources.each do |resource_name, resource_config| docs << "### #{resource_name.capitalize}" endpoints = resource_config["endpoints"] || [] endpoints.each do |endpoint| name = endpoint["name"] method = endpoint["method"] || "get" path = endpoint["path"] || name has_params = path.include?("{") docs << "#### `#{name}`" docs << "- HTTP Method: `#{method.upcase}`" docs << "- Path: `#{path}`" if has_params params = path.scan(/\{([^}]+)\}/).flatten docs << "- Path Parameters: #{params.map { |p| "`#{p}`" }.join(", ")}" end docs << "- Accepts additional query parameters" if endpoint["params"] # Usage example docs << "\n```ruby" if has_params params = path.scan(/\{([^}]+)\}/).flatten param_args = params.map { |p| "#{p}: 123" }.join(", ") docs << if endpoint["params"] "client.#{resource_name}.#{name}(#{param_args}, {param1: 'value', param2: 'value'})" else "client.#{resource_name}.#{name}(#{param_args})" end else docs << if %w[post put patch].include?(method) "client.#{resource_name}.#{name}({field1: 'value', field2: 'value'})" elsif endpoint["params"] "client.#{resource_name}.#{name}({param1: 'value', param2: 'value'})" else "client.#{resource_name}.#{name}" end end docs << "```\n" end end docs.join("\n") end |
.render(api_name, module_name, config) ⇒ Object
6 7 8 9 10 11 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/wrappix/templates/readme.rb', line 6 def self.render(api_name, module_name, config) base_url = config["base_url"] || "https://api.example.com" auth_instructions = auth_setup_instructions(config) resource_docs = generate_resource_docs(module_name, config) " # \#{module_name} API Client\n\n A Ruby API wrapper for the \#{api_name} API.\n\n ## Installation\n\n Add this line to your application's Gemfile:\n\n ```ruby\n gem '\#{api_name}'\n ```\n\n And then execute:\n\n ```bash\n $ bundle install\n ```\n\n Or install it yourself as:\n\n ```bash\n $ gem install \#{api_name}\n ```\n\n ## Configuration\n\n ```ruby\n \#{module_name}.configure do |config|\n config.base_url = \"\#{base_url}\"\n \#{auth_instructions}\n end\n ```\n\n ## Usage\n\n ### Initializing the client\n\n ```ruby\n # Initialize the client\n client = \#{module_name}.client\n ```\n\n ### Examples\n\n ```ruby\n \#{usage_examples(module_name, config)}\n ```\n\n ### Working with responses\n\n Objects returned by the API can be accessed using dot notation:\n\n ```ruby\n user = client.users.get(123)\n puts user.id # => 123\n puts user.name # => \"John Doe\"\n puts user.email # => \"[email protected]\"\n ```\n\n Collections include pagination support:\n\n ```ruby\n users = client.users.list\n\n # Iterate through items\n users.data.each do |user|\n puts user.name\n end\n\n # Check pagination info\n if users.next_href\n # More results available\n end\n ```\n\n ## Resources and Endpoints\n\n \#{resource_docs}\n\n ## Error Handling\n\n ```ruby\n begin\n response = client.users.get(123)\n rescue \#{module_name}::Error => e\n puts \"Error: \\\#{e.message}\"\n puts \"Status: \\\#{e.status}\"\n puts \"Details: \\\#{e.body}\"\n end\n ```\n\n ## API Documentation\n\n Detailed API documentation is available in the `docs/api.md` file, which includes:\n\n - All available endpoints\n - Required parameters\n - Example requests and responses\n - Authentication details\n\n ## Advanced Usage\n\n ### Caching\n\n \#{module_name} uses a caching solution to improve efficiency (e.g., for caching tokens). By default, it uses a simple memory cache,\n but you can change the cache method by setting the `\#{module_name}.cache` attribute.\n\n ```ruby\n # Use Redis cache\n \#{module_name}.cache = Redis.new\n\n # Or use Rails cache\n \#{module_name}.cache = Rails.cache\n\n # Or use file-based cache\n \#{module_name}.cache = \#{module_name}::FileCache.new\n\n # Or any object that responds to read/write/delete/clear\n \#{module_name}.cache = YourCustomCache.new\n ```\n\n ### Custom Headers\n\n You can set custom headers for all requests:\n\n ```ruby\n \#{module_name}.configure do |config|\n config.headers[\"User-Agent\"] = \"MyApp/1.0\"\n config.headers[\"X-Custom-Header\"] = \"Value\"\n end\n ```\n MARKDOWN\nend\n" |
.usage_examples(_module_name, config) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/wrappix/templates/readme.rb', line 169 def self.usage_examples(_module_name, config) resources = config["resources"] || {} examples = [] resources.each do |resource_name, resource_config| endpoints = resource_config["endpoints"] || [] next if endpoints.empty? # Take the first endpoint as an example endpoint = endpoints.first method = endpoint["method"] || "get" has_params = endpoint["path"].to_s.include?("{") if has_params # Extract parameters params = endpoint["path"].scan(/\{([^}]+)\}/).flatten param_values = params.map { |_p| "123" } # Example values args = param_values.join(", ") examples << "# #{resource_name.capitalize} - #{endpoint["name"]} example" examples << "response = client.#{resource_name}.#{endpoint["name"]}(#{args})" else examples << "# #{resource_name.capitalize} - #{endpoint["name"]} example" examples << if %w[post put patch].include?(method) "response = client.#{resource_name}.#{endpoint["name"]}({name: 'value', other_field: 'value'})" else "response = client.#{resource_name}.#{endpoint["name"]}" end end examples << "" end examples.join("\n") end |