Class: Sumcli::Commands::Add::Endpoint

Inherits:
Sumcli::Command show all
Defined in:
lib/sumcli/commands/add/endpoint.rb

Constant Summary collapse

ENDPOINT_PATH =
'api/endpoints'
MODELS_PATH =
'app/models'
ENTITIES_PATH =
'api/entities'
TESTS_PATH =
'spec/api'
TEMPLATES_PATH =
File.expand_path('../../templates/add/endpoint', __dir__)

Instance Method Summary collapse

Methods inherited from Sumcli::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(name, method, route, options) ⇒ Endpoint

Returns a new instance of Endpoint.



18
19
20
21
22
23
24
# File 'lib/sumcli/commands/add/endpoint.rb', line 18

def initialize(name, method, route, options)
  @name = name
  @method = method
  @route = route
  @options = options
  @variables = OpenStruct.new(name: @name, route: @route, method: @method)
end

Instance Method Details

#create_endpointObject



79
80
81
82
83
84
85
86
87
# File 'lib/sumcli/commands/add/endpoint.rb', line 79

def create_endpoint
  generator.copy_file(
    "#{TEMPLATES_PATH}/new.rb.erb",
    "#{ENDPOINT_PATH}/#{@name.underscore}.rb",
    context: @variables
  )

  inject_mount
end

#create_entityObject



71
72
73
74
75
76
77
# File 'lib/sumcli/commands/add/endpoint.rb', line 71

def create_entity
  generator.copy_file(
    "#{TEMPLATES_PATH}/entity.rb.erb",
    "#{ENTITIES_PATH}/#{@name.underscore}.rb",
    context: @variables
  )
end

#create_modelObject



89
90
91
92
93
94
95
# File 'lib/sumcli/commands/add/endpoint.rb', line 89

def create_model
  generator.copy_file(
    "#{TEMPLATES_PATH}/model.rb.erb",
    "#{MODELS_PATH}/#{@name.underscore}.rb",
    context: @variables
  )
end

#create_testObject



63
64
65
66
67
68
69
# File 'lib/sumcli/commands/add/endpoint.rb', line 63

def create_test
  generator.copy_file(
    "#{TEMPLATES_PATH}/test.rb.erb",
    "#{TESTS_PATH}/#{@name.underscore}_spec.rb",
    context: @variables
  )
end

#execute(input: $stdin, output: $stdout) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sumcli/commands/add/endpoint.rb', line 26

def execute(input: $stdin, output: $stdout)
  unless File.file?(".sumcli")
    pastel = Pastel.new
    output.puts pastel.red("ERROR!\nYou are no in a sumcli app directory.")
    output.puts "    You can execute \#{pastel.green('sumcli help new')} to get more info.\n    OUT\n    return\n  end\n\n  create_entity unless File.file?(\"\#{ENTITIES_PATH}/\#{@name.underscore}.rb\")\n  create_test unless File.file?(\"\#{TESTS_PATH}/\#{@name.underscore}.rb\")\n  create_endpoint unless File.file?(\"\#{ENDPOINT_PATH}/\#{@name.underscore}.rb\")\n  inject_route unless @method.nil?\nend\n".strip

#inject_mountObject



42
43
44
45
46
47
# File 'lib/sumcli/commands/add/endpoint.rb', line 42

def inject_mount
  generator.inject_into_file("#{ENDPOINT_PATH}/base.rb", after: "# APIs\n") do "    mount API::\#{@name.capitalize}\n    RB\n  end\nend\n"

#inject_routeObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sumcli/commands/add/endpoint.rb', line 49

def inject_route
  generator.inject_into_file("#{ENDPOINT_PATH}/#{@name.underscore}.rb", after: "# ENDPOINTS\n") do "\n      desc 'Describe your endpoint'\n      params do\n# requires :id, type: Integer\n      end\n      \#{@method.downcase} '\#{@route}' do\n# write code here..\n      end\n      RB\n  end\nend\n"