Class: RuboCop::Cop::Alchemrest::EndpointDefinitionUsingGenericParams

Inherits:
Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Defined in:
lib/rubocop/cop/alchemrest/endpoint_definition_using_generic_params.rb

Constant Summary collapse

MSG =
<<~MSG
  You are using a generic params hash to build a url, rather than explicitly defining which values in the
  hash should be inserted into the path of the url, vs which are query string params. This is deprecated,
  and you should update your code to use `values=` and `query=` instead.

  BAD:
  endpoint :get, '/api/v1/users/:id' do |url|
    url.params = { id: @id, includeDetails: true }
  end

  GOOD:
  endpoint :get, '/api/v1/users/:id' do |url|
    url.values = { id: @id }
    url.query = { includeDetails: true }
  end
MSG

Instance Method Summary collapse

Instance Method Details

#build_autocorrection(block) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/rubocop/cop/alchemrest/endpoint_definition_using_generic_params.rb', line 45

def build_autocorrection(block)
  params_value = block.arguments.first.source

  <<~CODE.chomp
    url.values = #{params_value}
  CODE
end

#on_block(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/alchemrest/endpoint_definition_using_generic_params.rb', line 34

def on_block(node)
  on_endpoint_call?(node) do |block_node|
    if generic_params_assignment?(block_node)
      add_offense(block_node) do |corrector|
        correction = build_autocorrection(block_node)
        corrector.replace(block_node, correction) if correction
      end
    end
  end
end