Class: Generator::API

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

Constant Summary collapse

OBSOLETE_APIS =
["reports_2020_09_04", "feeds_2020_09_04"]

Constants included from Formatter

Formatter::MAX_LINE_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#pascalcase, #snakecase

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.



20
21
22
# File 'lib/generator/api.rb', line 20

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

Instance Method Details

#class_nameObject



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

def class_name
  pascalcase(name_with_version)
end

#descriptionObject



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

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



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

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

#generateObject



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

def generate
  return if obsolete?

  handle_duplicate_operations
  File.write(file_path, render)
end

#has_helper?Boolean

Returns:

  • (Boolean)


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

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

#helper_class_nameObject



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

def helper_class_name
  "Peddler::Helpers::#{pascalcase(name_with_version)}"
end

#helper_library_nameObject



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

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

#library_nameObject



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

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

#name_with_versionObject



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

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

#obsolete?Boolean

Returns:

  • (Boolean)


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

def obsolete?
  OBSOLETE_APIS.any? { |api| name_with_version.include?(api) }
end

#operationsObject



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

def operations
  @operations ||= paths.flat_map(&:operations).compact
end

#pathsObject



74
75
76
# File 'lib/generator/api.rb', line 74

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

#titleObject



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

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