Class: EndpointGenerator

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

Instance Method Summary collapse

Instance Method Details

#create_api_callObject



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
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generators/endpoint/endpoint_generator.rb', line 9

def create_api_call
  @api_name = endpoint_name
  @endpoint = endpoint
  @endpoint = @endpoint[1..] if @endpoint.first == '/'
  @method = options[:method].to_sym

  if options[:method_params].present?
    @method_params = options[:method_params][0..-2].each_with_object('') do |p, str|
      str << p << ', '
    end
    @method_params << options[:method_params].last
  end

  @api_service = file_name.underscore
  service_dir_path = "app/services/#{@api_service}/"

  template 'api_call.erb', service_dir_path + "api_calls/#{@api_name.underscore}.rb"

  temp = ERB.new <<-'EOF'

def <%= @api_name.underscore%>_endpoint<%="(#{@method_params})" if @method_params.present?%>
  <%= "\"#{@endpoint}\".freeze" if @endpoint.present? %>
end
  EOF
  
  inject_into_file service_dir_path + 'endpoint_helpers.rb', after: "  # API Endpoint Helpers\n" do
    temp.result(binding)
  end

  temp = ERB.new <<-'EOF'
extend <%= @api_name.camelize%>
  EOF

  inject_into_file service_dir_path + 'api_endpoints.rb', after: "  # API call modules\n" do
    temp.result(binding)
  end
end