summaryrefslogtreecommitdiff
path: root/rest-api-spec/utils/thor/templates/ruby/method.erb
blob: 7f13d2fcb2c16fc5ca857dfe38f71669055e4e7a (plain)
1
2
3
4
5
6
7
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Elasticsearch
  module API
    <%- @module_namespace.each_with_index do |name, i| -%>
    <%= '  '*i %>module <%= name.capitalize %>
    <%- end -%>
    <%= '  '*@namespace_depth %>module Actions

      <%= '  '*@namespace_depth %># <%= @spec['description'] || 'TODO: Description' %>
      <%= '  '*@namespace_depth %>#
<%# URL parts -%>
      <%- @spec['url']['parts'].each do |name,info| -%>
      <%- info['type'] = 'String' if info['type'] == 'enum' # Rename 'enums' to 'strings' -%>
      <%= '  '*@namespace_depth + "# @option arguments [#{info['type'] ? info['type'].capitalize : 'String'}] :#{name} #{info['description']}" + ( info['required'] ? ' (*Required*)' : '' ) -%><%= " (options: #{info['options'].join(', ')})" if info['options'] -%>
      <%= "\n" -%>
      <%- end -%>
<%# Body -%>
<%= '  '*(@namespace_depth+3) + '# @option arguments [Hash] :body ' + (@spec['body']['description'] || 'TODO: Description') + (@spec['body']['required'] ? ' (*Required*)' : '') + "\n" if @spec['body'] -%>
<%# URL parameters -%>
      <%- @spec['url']['params'].each do |name,info| -%>
      <%- info['type'] = 'String' if info['type'] == 'enum' # Rename 'enums' to 'strings' -%>
      <%= '  '*@namespace_depth + "# @option arguments [#{info['type'] ? info['type'].capitalize : 'String'}] :#{name} #{info['description']}" -%><%= " (options: #{info['options'].join(', ')})" if info['options'] -%>
      <%=  "\n" -%>
      <%- end -%>
      <%= '  '*@namespace_depth -%>#
<%# Documentation link -%>
      <%= '  '*@namespace_depth %># @see <%= @spec['documentation'] %>
      <%= '  '*@namespace_depth %>#
<%# Method definition -%>
      <%= '  '*@namespace_depth -%>def <%= @method_name %>(arguments={})
<%# Required arguments -%>
      <%- @spec['url']['parts'].select { |name, info| info['required'] }.each do |name, info| -%>
      <%= '  '*(@namespace_depth+1) + "raise ArgumentError, \"Required argument '#{name}' missing\" unless arguments[:#{name}]" + "\n" -%>
      <%- end -%>
<%- if @spec['body'] && @spec['body']['required'] -%>
      <%= '  '*(@namespace_depth+1) + "raise ArgumentError, \"Required argument 'body' missing\" unless arguments[:body]" + "\n" -%>
<%- end -%>
<%# Method, path, params, body  -%>
      <%= '  '*@namespace_depth %>  method = '<%= @spec['methods'].first %>'
      <%- unless @spec['url']['parts'].empty?  -%>
      <%= '  '*@namespace_depth %>  path   = "<%= @spec['url']['path'].split('/').compact.reject {|p| p =~ /^\s*$/}.map do |p|
        p =~ /\{/ ? "\#\{arguments[:#{p.tr('{}', '')}]\}" : p
      end.join('/') %>"
      <%- else -%>
      <%= '  '*@namespace_depth %>  path   = "<%= @spec['url']['path'] %>"
      <%- end -%>
      <%- unless @spec['url']['params'].keys.empty? -%>
      <%= '  '*@namespace_depth %>  params = arguments.select do |k,v|
      <%= '  '*@namespace_depth %>    [ :<%= @spec['url']['params'].keys.join(",\n#{'  '*(@namespace_depth+6)}:") %> ].include?(k)
      <%= '  '*@namespace_depth %>  end
      <%= '  '*@namespace_depth %>  # Normalize Ruby 1.8 and Ruby 1.9 Hash#select behaviour
      <%= '  '*@namespace_depth %>  params = Hash[params] unless params.is_a?(Hash)
      <%- else -%>
      <%= '  '*@namespace_depth %>  params = {}
      <%- end -%>
      <%= '  '*@namespace_depth %>  body   = <%= @spec['body'].nil? ? 'nil' : 'arguments[:body]' %>
<%# Perform request %>
      <%= '  '*@namespace_depth %>  perform_request(method, path, params, body).body
      <%= '  '*@namespace_depth %>end
      <%- @namespace_depth.downto(1) do |i| -%>
      <%= '  '*(i-1) %>end
      <%- end if @namespace_depth > 0 -%>
    end
  end
end