class RuboCop::Cop::Rails::ResponseParsedBody
response.parsed_body
# good
Nokogiri::HTML5.parse(response.body)
# bad
Nokogiri::HTML.parse(response.body)
# bad
JSON.parse(response.body)
# bad
@example
so you still have to use ‘JSON.parse(response.body)` there.
`application/vnd.github+json` is not supported at `response.parsed_body` by default,
For example, the proprietary Content-Type provided by corporate entities such as
This cop is unsafe because Content-Type may not be `application/json` or `text/html`.
@safety
Prefer `response.parsed_body` to custom parsing logic for `response.body`.
def autocorrect(corrector, node)
def autocorrect(corrector, node) corrector.replace(node, 'response.parsed_body') end
def check_json_parse_response_body(node)
def check_json_parse_response_body(node) return unless json_parse_response_body?(node) add_offense( node, message: 'Prefer `response.parsed_body` to `JSON.parse(response.body)`.' ) do |corrector| autocorrect(corrector, node) end end
def check_nokogiri_html_parse_response_body(node)
def check_nokogiri_html_parse_response_body(node) return unless (const = nokogiri_html_parse_response_body(node)) add_offense( node, message: "Prefer `response.parsed_body` to `Nokogiri::#{const}.parse(response.body)`." ) do |corrector| autocorrect(corrector, node) end end
def on_send(node)
def on_send(node) check_json_parse_response_body(node) return unless target_rails_version >= 7.1 check_nokogiri_html_parse_response_body(node) end