class Swgr2rb::EndpointClassGenerator

EndpointClassGenerator generates a Ruby class file for an endpoint object model from its config.

Public Instance Methods

generate_lines() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 10
def generate_lines
  [generate_requires,
   generate_class_name,
   generate_modules_to_include,
   generate_initialize_method,
   generate_validate_response_schema_method,
   'private',
   generate_end_point_path_method,
   generate_generate_headers_method,
   generate_generate_body_method,
   'end'].compact.flatten
end

Private Instance Methods

default_value_for_type(type) click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 154
def default_value_for_type(type)
  if type == String
    "'string'"
  elsif type == Integer
    '0'
  elsif type == Float
    '0.0'
  elsif type == Boolean
    'false'
  else
    raise "Unexpected type: #{type}"
  end
end
generate_class_name() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 29
def generate_class_name
  RubyFileGeneratorConstants::CLASS_NAME.call(@opts[:name], @opts[:parent_class]&.fetch(:name))
end
generate_default_request_body(schema) click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 124
def generate_default_request_body(schema)
  if schema.instance_of?(Class) || schema == Boolean
    default_value_for_type(schema)
  elsif schema.is_a?(Array)
    "[\n" + generate_default_request_body(schema.first) + "\n]"
  elsif schema.is_a?(Hash)
    schema = schema.map { |name, type| "#{name}: #{generate_default_request_body(type)}," }
                 .join("\n").sub(/,\Z/, '')
    "{\n" + schema + "\n}"
  end
end
generate_end_point_path_method() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 46
def generate_end_point_path_method
  unknown_params = path_params[:snake_case] + query_params[:snake_case]
  param_loading = generate_endpoint_path_param_loading(unknown_params)
  RubyFileGeneratorConstants::END_POINT_PATH.call(unknown_params,
                                                  param_loading)
      .compact.flatten
end
generate_endpoint_path() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 62
def generate_endpoint_path
  path = @config.endpoint_path.gsub('{', '#{')
  path_params[:camel_case].zip(path_params[:snake_case]) do |cc_param, sc_param|
    path.sub!(cc_param, sc_param)
  end
  path.sub!('#{version}', @config.version.to_s)
  if query_params[:camel_case].present?
    path << '?' << query_params[:camel_case].zip(query_params[:snake_case])
                       .map { |cc, sc| "#{cc}=\#{#{sc}}" }.join('&')
  end
  proc_params = path_params[:snake_case] + query_params[:snake_case]
  if proc_params.empty?
    "proc { '#{path}' }"
  else
    "proc { |#{proc_params.sort.join(", ")}| \"#{path}\" }"
  end
end
generate_endpoint_path_param_loading(params) click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 98
def generate_endpoint_path_param_loading(params)
  if params.present?
    lines = params.map do |param|
      RubyFileGeneratorConstants::GET_PARAM_FROM_REQUEST_OPTIONS.call(param)
    end
    lines << RubyFileGeneratorConstants::COMMENT_ADD_SUB_RESULTS
    lines << RubyFileGeneratorConstants::RAISE_UNLESS_PARAMS_PASSED.call(params, @config.endpoint_path)
    lines.flatten
  end
end
generate_generate_body_method() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 58
def generate_generate_body_method
  RubyFileGeneratorConstants::GENERATE_BODY.call(generate_request_body(@config.request_params))
end
generate_generate_headers_method() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 54
def generate_generate_headers_method
  RubyFileGeneratorConstants::GENERATE_HEADERS.call(@config.request_type)
end
generate_initialize_method() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 37
def generate_initialize_method
  RubyFileGeneratorConstants::INITIALIZE.call(generate_endpoint_path,
                                              @config.request_type)
end
generate_modules_to_include() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 33
def generate_modules_to_include
  RubyFileGeneratorConstants::INCLUDES.call(@opts[:modules_to_include].to_a)
end
generate_params(params) click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 88
def generate_params(params)
  camel_case_params = params.map { |hsh| hsh[:name] }
  snake_case_params = camel_case_params.map { |s| RubyFileGeneratorConstants::CAMEL_CASE_TO_SNAKE_CASE.call(s) }
  { camel_case: camel_case_params, snake_case: snake_case_params }
end
generate_request_body(params) click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 109
def generate_request_body(params)
  case params
  in { body: [{ schema: } => param, *] }
    [RubyFileGeneratorConstants::COMMENT_SET_VALID_VALUES,
     "tmp = #{generate_default_request_body(schema)}",
     RubyFileGeneratorConstants::COMMENT_ADD_SUB_RESULTS,
     generate_request_body_set_params(param),
     'tmp.to_json'].flatten
  in { form_data: [Hash, *] }
    RubyFileGeneratorConstants::MULTIPART_REQUEST_BODY
  else
    'nil'
  end
end
generate_request_body_set_params(params) click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 136
def generate_request_body_set_params(params)
  case params
  in { schema: Class | Boolean => schema, name: }
    "tmp = #{RubyFileGeneratorConstants::GET_PARAM_FROM_REQUEST_PARAMS.call(name, schema)}"
  in { schema: [Hash => item, *] }
    item.map do |name, type|
      "tmp.first[:#{name}] = #{RubyFileGeneratorConstants::GET_PARAM_FROM_REQUEST_PARAMS.call(name, type)}"
    end
  in { schema: [type], name: }
    ["tmp = #{RubyFileGeneratorConstants::GET_PARAM_FROM_REQUEST_PARAMS.call(name, type)}",
     'tmp = tmp.split(/,\s*/)']
  in { schema: Hash => schema }
    schema.map do |name, type|
      "tmp[:#{name}] = #{RubyFileGeneratorConstants::GET_PARAM_FROM_REQUEST_PARAMS.call(name, type)}"
    end
  end
end
generate_requires() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 25
def generate_requires
  RubyFileGeneratorConstants::REQUIRES.call([@opts[:parent_class]].compact + @opts[:modules_to_include].to_a)
end
generate_schema_validation() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 94
def generate_schema_validation
  RubyFileGeneratorConstants::JSON_VALIDATOR_VALIDATE_SCHEMA if @config.expected_response.schema.present?
end
generate_validate_response_schema_method() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 42
def generate_validate_response_schema_method
  RubyFileGeneratorConstants::VALIDATE_RESPONSE_SCHEMA.call(generate_schema_validation)
end
path_params() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 80
def path_params
  @path_params ||= generate_params(@config.request_params.path.reject { |p| p[:name] == 'version' })
end
query_params() click to toggle source
# File lib/endpoint_class_generator/endpoint_class_generator.rb, line 84
def query_params
  @query_params ||= generate_params(@config.request_params.query)
end