slack-ruby/slack-ruby-client

View on GitHub
lib/slack/web/api/templates/method.erb

Summary

Maintainability
Test Coverage
# frozen_string_literal: true
# This file was auto-generated by lib/tasks/web.rake

module Slack
  module Web
    module Api
      module Endpoints
        module <%= group.gsub(".", "_").camelize %>
<% names.sort.each_with_index do |(name, data), index| %>
<% next if data['mixin'] %>
<% if index > 0 %>

<% end %>
          #
<% data['desc'].split("\n").each_with_index do |line, index| %>
  <% line += ' (undocumented)' if data['undocumented'] && index == 0 %>
          # <%= line %>
<% end %>
          #
<% data['args'].each do |arg_name, arg_v| %>
          # @option options [<%= arg_v['type'] %>] :<%= arg_name %>
<% arg_v['desc'].lines.each do |l| %>
<% if l.strip.length > 0 %>
          #   <%= l.strip %>
<% else %>
          #
<% end %>
<% end %>
<% end %>
<% if data['undocumented'] %>
          # @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/undocumented/<%= group %>/<%= group %>.<%= name %>.json
<% else %>
          # @see https://api.slack.com/methods/<%= group %>.<%= name %>
          # @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/<%= group %>/<%= group %>.<%= name %>.json
<% end %>
          def <%= group.gsub(".", "_") %>_<%= name %>(options = {})
<% data['args'].select{ |k, v| v['required'] }.each do |arg_name, arg_v| %>
            raise ArgumentError, 'Required arguments :<%= arg_name %> missing' if options[:<%= arg_name %>].nil?
<% end %>
<% data['arg_groups']&.each do |arg_group| %>
  <% next if arg_group['args'].size <= 1 %>
<% arg_string = arg_group['args'].map { |arg_name| ":#{arg_name}" }.join(', ') %>
<% if arg_group['mutually_exclusive'] %>
            raise ArgumentError, 'Exactly one of <%= arg_string %> is required' unless <%= arg_group['args'].map { |arg_name| "options[:#{arg_name}].nil?" }.join(' ^ ') %>
<% else %>
            raise ArgumentError, 'At least one of <%= arg_string %> is required' if <%= arg_group['args'].map { |arg_name| "options[:#{arg_name}].nil?"}.join(' && ') %>
<% end %>
<% end %>
<% if data['group'] == 'groups' && data['args']['channel'] && !data['args']['channel']['desc'].include?('Can be an encoded ID, or a name.') %>
            options = options.merge(channel: groups_id(options)['group']['id']) if options[:channel]
<% elsif data['args']['channel'] && !data['args']['channel']['desc'].include?('Can be an encoded ID, or a name.') %>
            options = options.merge(channel: conversations_id(options)['channel']['id']) if options[:channel]
<% end %>
<% if data['args']['user'] %>
            options = options.merge(user: users_id(options)['user']['id']) if options[:user]
<% end %>
<% if data['undocumented'] %>
            logger.warn('The <%= group %>.<%= name %> method is undocumented.')
<% end %>
<% if data['deprecated'] %>
            logger.warn('<%= group %>.<%= name %>: <%= data['deprecation']['deprecation_warning']%> Alternative methods: <%= data['deprecation']['alternative_methods'].join(', ')%>.')
<% end %>
<% json_args = data['args'].map { |arg_name, arg_v| arg_name if arg_v['format'] == 'json' }.compact %>
<% if json_args.any? %>
            options = encode_options_as_json(options, %i[<%= json_args.join(' ') %>])
<% end %>
<% if data['args'].keys.include?('cursor') %>
            if block_given?
              Pagination::Cursor.new(self, :<%= group.gsub(".", "_") %>_<%= name %>, options).each do |page|
                yield page
              end
            else
              post('<%= group %>.<%= name %>', options)
            end
<% else %>
            post('<%= group %>.<%= name %>', options)
<% end %>
          end
<% end %>
        end
      end
    end
  end
end