Class: Wrappix::Templates::Resource
- Inherits:
-
Object
- Object
- Wrappix::Templates::Resource
- Defined in:
- lib/wrappix/templates/resource.rb
Class Method Summary collapse
- .endpoint_method(module_name, _resource_name, endpoint, resource_config) ⇒ Object
- .generate_response_transform(module_name, is_collection, response_format) ⇒ Object
- .render(module_name, resource_name, resource_config) ⇒ Object
Class Method Details
.endpoint_method(module_name, _resource_name, endpoint, resource_config) ⇒ Object
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 |
# File 'lib/wrappix/templates/resource.rb', line 34 def self.endpoint_method(module_name, _resource_name, endpoint, resource_config) name = endpoint["name"] method = endpoint["method"] || "get" path = endpoint["path"] || name response_format = resource_config["response_format"] || {} is_collection = endpoint["collection"] || %w[all list index search].include?(name) has_params = path.include?("{") param_list = has_params ? path.scan(/\{([^}]+)\}/).flatten : [] endpoint_params = [] endpoint_params.concat(param_list) endpoint_params << "params = {}" if endpoint["params"] endpoint_params << "body = {}" if %w[post put patch].include?(method) request_args = [] request_args << "params: params" if endpoint["params"] request_args << "body: body" if %w[post put patch].include?(method) = request_args.empty? ? "" : "(#{request_args.join(", ")})" path_with_params = path param_list.each do |param| path_with_params = path_with_params.gsub(/\{#{param}\}/, "\#{#{param}}") end response_transform = if is_collection "#{module_name}::Collection.from_response(response, type: #{module_name}::Object)" else item_root = response_format["item_root"] if item_root "#{module_name}::Object.new(response[:#{item_root}] || response[\"#{item_root}\"] || response)" else "#{module_name}::Object.new(response)" end end template = " def \#{name}(\#{endpoint_params.join(\", \")})\n request = \#{module_name}::Request.new(\"\#{path_with_params}\")\n response = request.\#{method}\#{request_options}\n\n \#{response_transform}\n end\n RUBY\n template.gsub(/^ +/, \"\")\nend\n".strip |
.generate_response_transform(module_name, is_collection, response_format) ⇒ Object
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 |
# File 'lib/wrappix/templates/resource.rb', line 82 def self.generate_response_transform(module_name, is_collection, response_format) if is_collection collection_root = response_format["collection_root"] || "data" pagination_config = response_format["pagination"] || {} next_page_key = pagination_config["next_page_key"] || "next_href" template = " data = response[:\#{collection_root}] || response[\"\#{collection_root}\"] || []\n next_href = response[:\#{next_page_key}] || response[\"\#{next_page_key}\"]\n\n \#{module_name}::Collection.from_response({\n data: data,\n next_href: next_href\n }, type: \#{module_name}::Object)\n RUBY\n template.gsub(/^ +/, \"\")\n else\n item_root = response_format[\"item_root\"]\n if item_root\n \"\#{module_name}::Object.new(response[:\#{item_root}] || response[\\\"\#{item_root}\\\"] || response)\"\n else\n \"\#{module_name}::Object.new(response)\"\n end\n end\nend\n" |
.render(module_name, resource_name, resource_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 |
# File 'lib/wrappix/templates/resource.rb', line 6 def self.render(module_name, resource_name, resource_config) endpoints = resource_config["endpoints"] || [] methods = endpoints.map do |endpoint| endpoint_method(module_name, resource_name, endpoint, resource_config) end.join("\n\n") class_name = resource_name.capitalize template = " # frozen_string_literal: true\n\n module \#{module_name}\n module Resources\n class \#{class_name}\n def initialize(client)\n @client = client\n @config = \#{module_name}.configuration\n end\n\n \#{methods}\n end\n end\n end\n RUBY\n # Remove leading whitespace from each line\n template.gsub(/^ +/, \"\")\nend\n" |