Class: RuboCop::Cop::Alchemrest::RequestHashReturningBlock

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

Constant Summary collapse

MSG =
<<~MSG
  Returning a hash to set url params is deprecated. Instead make your block take a `url` argument and call `url.params = {...}`

    BAD:
    ```
    endpoint :get, '/api/users/:id', -> { { id: user_id } }
    ```

    GOOD:
    ```
    endpoint :get, '/api/users/:id' do |url|
      url.params = { id: user_id }
    end
    ```
MSG

Instance Method Summary collapse

Instance Method Details

#build_autocorrection(http_method, url, block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/alchemrest/request_hash_returning_block.rb', line 38

def build_autocorrection(http_method, url, block)
  block_body = block.body

  params_value = block_body.source

  url_string = url.source

  <<~CODE.chomp
    endpoint :#{http_method.value}, #{url_string} do |url|
      url.params = #{params_value}
    end
  CODE
end

#on_send(node) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/alchemrest/request_hash_returning_block.rb', line 29

def on_send(node)
  on_endpoint_params_proc_call?(node) do |http_method, url, block_node|
    add_offense(block_node) do |corrector|
      correction = build_autocorrection(http_method, url, block_node)
      corrector.replace(node, correction) if correction
    end
  end
end