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.



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

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



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

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

  inject_mount
end

#create_entityObject



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

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

#create_modelObject



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

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

#create_testObject



53
54
55
56
57
58
59
# File 'lib/sumcli/commands/add/endpoint.rb', line 53

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



25
26
27
28
29
30
# File 'lib/sumcli/commands/add/endpoint.rb', line 25

def execute(input: $stdin, output: $stdout)
  create_entity unless File.file?("#{ENTITIES_PATH}/#{@name.underscore}.rb")
  create_test unless File.file?("#{TESTS_PATH}/#{@name.underscore}.rb")
  create_endpoint unless File.file?("#{ENDPOINT_PATH}/#{@name.underscore}.rb")
  inject_route unless @method.nil?
end

#inject_mountObject



32
33
34
35
36
37
# File 'lib/sumcli/commands/add/endpoint.rb', line 32

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

#inject_routeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sumcli/commands/add/endpoint.rb', line 39

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