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" <<~RUBY.strip config.client_id = "your_client_id" config.client_secret = "your_client_secret" config.access_token = "your_access_token" RUBY when "basic" <<~RUBY.strip config.username = "your_username" config.password = "your_password" RUBY when "api_key" <<~RUBY.strip config.api_key = "your_api_key" config.api_key_header = "#{config["api_key_header"] || "X-Api-Key"}" RUBY else "# No authentication required" end end |
.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) <<~MARKDOWN # #{module_name} API Client A Ruby API wrapper for the #{api_name} API. ## Installation Add this line to your application's Gemfile: ```ruby gem '#{api_name}' ``` And then execute: ```bash $ bundle install ``` Or install it yourself as: ```bash $ gem install #{api_name} ``` ## Configuration ```ruby #{module_name}.configure do |config| config.base_url = "#{base_url}" #{auth_instructions} end ``` ## Usage ### Initializing the client ```ruby # Initialize the client client = #{module_name}.client ``` ### Examples ```ruby #{usage_examples(module_name, config)} ``` ### Working with responses Objects returned by the API can be accessed using dot notation: ```ruby user = client.users.get(123) puts user.id # => 123 puts user.name # => "John Doe" puts user.email # => "[email protected]" ``` Collections include pagination support: ```ruby users = client.users.list # Iterate through items users.data.each do |user| puts user.name end # Check pagination info if users.next_href # More results available end ``` ## Resources and Endpoints #{resource_docs} ## Error Handling ```ruby begin response = client.users.get(123) rescue #{module_name}::Error => e puts "Error: \#{e.message}" puts "Status: \#{e.status}" puts "Details: \#{e.body}" end ``` ## API Documentation Detailed API documentation is available in the `docs/api.md` file, which includes: - All available endpoints - Required parameters - Example requests and responses - Authentication details ## Advanced Usage ### Caching #{module_name} uses a caching solution to improve efficiency (e.g., for caching tokens). By default, it uses a simple memory cache, but you can change the cache method by setting the `#{module_name}.cache` attribute. ```ruby # Use Redis cache #{module_name}.cache = Redis.new # Or use Rails cache #{module_name}.cache = Rails.cache # Or use file-based cache #{module_name}.cache = #{module_name}::FileCache.new # Or any object that responds to read/write/delete/clear #{module_name}.cache = YourCustomCache.new ``` ### Custom Headers You can set custom headers for all requests: ```ruby #{module_name}.configure do |config| config.headers["User-Agent"] = "MyApp/1.0" config.headers["X-Custom-Header"] = "Value" end ``` MARKDOWN end |
.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 |