Class: ServiceGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/service/service_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_api_serviceObject

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/service/service_generator.rb', line 8

def create_api_service
  @api_name = file_name
  @api_version = version
  @api_endpoint = options[:api_endpoint]
  @env_var_opts = options[:key]

  raise ArgumentError, 'Need to give the API version' unless @api_version.present?

  @full_api_reference = @api_name.camelize + '::' + @api_version.upcase
  @api_endpoint = @api_endpoint + '/' unless @api_endpoint.last == '/'

  root_dir = "app/services/"
  Dir.mkdir root_dir unless File.exist?(root_dir)

  api_path = root_dir + @api_name.underscore + '/'

  Dir.mkdir api_path unless File.exist?(api_path)

  service_dir_path = api_path + @api_version.downcase + '/'

  Dir.mkdir service_dir_path unless File.exist?(service_dir_path)
  Dir.mkdir "#{service_dir_path}/api_calls" unless File.exist?("#{service_dir_path}/api_calls")

  template 'api_endpoints.erb', service_dir_path + 'api_endpoints.rb'
  template 'client.erb', service_dir_path + 'client.rb'
  template 'endpoint_helpers.erb', service_dir_path + 'endpoint_helpers.rb'
end