Class: TypeSpecFromSerializers::Operation

Inherits:
Struct
  • Object
show all
Defined in:
lib/typespec_from_serializers/generator.rb

Overview

Internal: Represents a TypeSpec operation within a resource

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



234
235
236
# File 'lib/typespec_from_serializers/generator.rb', line 234

def action
  @action
end

#methodObject

Returns the value of attribute method

Returns:

  • (Object)

    the current value of method



234
235
236
# File 'lib/typespec_from_serializers/generator.rb', line 234

def method
  @method
end

#path_paramsObject

Returns the value of attribute path_params

Returns:

  • (Object)

    the current value of path_params



234
235
236
# File 'lib/typespec_from_serializers/generator.rb', line 234

def path_params
  @path_params
end

#response_typeObject

Returns the value of attribute response_type

Returns:

  • (Object)

    the current value of response_type



234
235
236
# File 'lib/typespec_from_serializers/generator.rb', line 234

def response_type
  @response_type
end

Instance Method Details

#as_typespecObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/typespec_from_serializers/generator.rb', line 235

def as_typespec
  method_map = {
    "GET" => "get",
    "POST" => "post",
    "PUT" => "put",
    "PATCH" => "patch",
    "DELETE" => "delete",
  }
  tsp_method = method_map[method] || method.downcase
  operation_name = TypeSpecFromSerializers.config.action_to_operation_mapping[action] || action
  params = params_typespec
  params_str = params.empty? ? "()" : "(#{params})"

  "#{"  " * 1}@#{tsp_method} #{operation_name}#{params_str}: #{response_type.gsub("::", "")};"
end

#params_typespecObject



251
252
253
254
255
256
# File 'lib/typespec_from_serializers/generator.rb', line 251

def params_typespec
  params = []
  params += path_params.map { |param| "@path #{param}: string" } if path_params.any?
  params << "@body body: #{response_type.gsub("::", "")}" if %w[POST PUT PATCH].include?(method)
  params.join(", ")
end