Class: Generator::API

Inherits:
Object
  • Object
show all
Includes:
Formatter, Logger
Defined in:
lib/generator/api.rb

Constant Summary

Constants included from Formatter

Formatter::MAX_LINE_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger

Methods included from Formatter

#convert_doc_links_to_full_url, #convert_html_links_to_yard, #format_method_definition, #split_long_comment_line

Constructor Details

#initialize(file) ⇒ API

Returns a new instance of API.



18
19
20
# File 'lib/generator/api.rb', line 18

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



16
17
18
# File 'lib/generator/api.rb', line 16

def file
  @file
end

Instance Method Details

#class_nameObject



53
54
55
# File 'lib/generator/api.rb', line 53

def class_name
  name_with_version.camelize
end

#descriptionObject



30
31
32
33
34
35
# File 'lib/generator/api.rb', line 30

def description
  description = model["info"]["description"]
  description = convert_doc_links_to_full_url(description)

  split_long_comment_line(description, base_indent: 4)
end

#file_pathObject



87
88
89
# File 'lib/generator/api.rb', line 87

def file_path
  File.join(Config::BASE_PATH, "lib/#{library_name}.rb")
end

#generateObject



22
23
24
# File 'lib/generator/api.rb', line 22

def generate
  File.write(file_path, render)
end

#github_model_filenameObject



95
96
97
# File 'lib/generator/api.rb', line 95

def github_model_filename
  File.basename(file)
end

#has_helper?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/generator/api.rb', line 41

def has_helper?
  File.exist?("lib/" + helper_library_name + ".rb")
end

#helper_class_nameObject



45
46
47
# File 'lib/generator/api.rb', line 45

def helper_class_name
  "Peddler::Helpers::#{name_with_version.camelize}"
end

#helper_library_nameObject



49
50
51
# File 'lib/generator/api.rb', line 49

def helper_library_name
  "peddler/helpers/#{name_with_version}"
end

#library_nameObject



37
38
39
# File 'lib/generator/api.rb', line 37

def library_name
  "peddler/apis/#{name_with_version}"
end

#nameObject



57
58
59
# File 'lib/generator/api.rb', line 57

def name
  file.split("/")[-2].tr("-", "_").sub(/(?:_api)?_model$/, "")
end

#name_with_versionObject



65
66
67
# File 'lib/generator/api.rb', line 65

def name_with_version
  [name, version].join("_")
end

#openapi_specObject



91
92
93
# File 'lib/generator/api.rb', line 91

def openapi_spec
  model
end

#operationsObject



69
70
71
72
73
74
# File 'lib/generator/api.rb', line 69

def operations
  @operations ||= begin
    ops = paths.flat_map { |path| path.operations(name_with_version, openapi_spec) }.compact
    deduplicate_operations(ops)
  end
end

#pathsObject



83
84
85
# File 'lib/generator/api.rb', line 83

def paths
  model["paths"].map { |path, methods| Path.new(path, methods) }
end

#titleObject



26
27
28
# File 'lib/generator/api.rb', line 26

def title
  split_long_comment_line(model["info"]["title"], base_indent: 4)
end

#type_namesObject



76
77
78
79
80
81
# File 'lib/generator/api.rb', line 76

def type_names
  openapi_spec["definitions"]
    .select { |name, def_| def_["type"] == "object" && name != "Money" }
    .keys
    .sort
end

#versionObject



61
62
63
# File 'lib/generator/api.rb', line 61

def version
  model["info"]["version"].tr("-", "_")
end