Skip to content
  • Sean Doyle's avatar
    0f4ab820
    Ensure `response.parsed_body` support for pattern matching · 0f4ab820
    Sean Doyle authored
    Both `Nokogiri` and `Minitest` have merged the PRs mentioned to
    integrate support for Ruby's Pattern matching
    (https://github.com/sparklemotion/nokogiri/pull/2523 and
    https://github.com/minitest/minitest/pull/936, respectively).
    
    This commit adds coverage for those new assertions, and incorporates
    examples into the documentation for the `response.parsed_body` method.
    
    In order to incorporate pattern-matching support for JSON responses,
    this commit changes the response parser to call `JSON.parse` with
    [object_class: ActiveSupport::HashWithIndifferentAccess][object_class],
    since String instances for `Hash` keys are incompatible with Ruby's
    syntactically pattern matching.
    
    For example:
    
    ```ruby
    irb(main):001:0> json = {"key" => "value"}
    => {"key"=>"value"}
    irb(main):002:0> json in {key: /value/}
    => false
    
    irb(main):001:0> json = {"key" => "value"}
    => {"key"=>"value"}
    irb(main):002:0> json in {"key" => /value/}
    .../3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/lib/irb/workspace.rb:113:in `eval': (irb):2: syntax error, unexpected terminator, expecting literal content or tSTRING_DBEG or tSTRING_DVAR or tLABEL_END (SyntaxError)
    json in {"key" => /value/}
                 ^
    
            .../ruby/3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/exe/irb:9:in `<top (required)>'
            .../ruby/3.2.0/bin/irb:25:in `load'
            .../ruby/3.2.0/bin/irb:25:in `<main>'
    ```
    
    When the Hash maps String keys to Symbol keys, it's able to be pattern
    matched:
    
    ```ruby
    irb(main):005:0> json = {"key" => "value"}.with_indifferent_access
    => {"key"=>"value"}
    irb(main):006:0> json in {key: /value/}
    => true
    ```
    
    [object_class]: https://docs.ruby-lang.org/en/3.2/JSON.html#module-JSON-label-Parsing+Options
    0f4ab820
    Ensure `response.parsed_body` support for pattern matching
    Sean Doyle authored
    Both `Nokogiri` and `Minitest` have merged the PRs mentioned to
    integrate support for Ruby's Pattern matching
    (https://github.com/sparklemotion/nokogiri/pull/2523 and
    https://github.com/minitest/minitest/pull/936, respectively).
    
    This commit adds coverage for those new assertions, and incorporates
    examples into the documentation for the `response.parsed_body` method.
    
    In order to incorporate pattern-matching support for JSON responses,
    this commit changes the response parser to call `JSON.parse` with
    [object_class: ActiveSupport::HashWithIndifferentAccess][object_class],
    since String instances for `Hash` keys are incompatible with Ruby's
    syntactically pattern matching.
    
    For example:
    
    ```ruby
    irb(main):001:0> json = {"key" => "value"}
    => {"key"=>"value"}
    irb(main):002:0> json in {key: /value/}
    => false
    
    irb(main):001:0> json = {"key" => "value"}
    => {"key"=>"value"}
    irb(main):002:0> json in {"key" => /value/}
    .../3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/lib/irb/workspace.rb:113:in `eval': (irb):2: syntax error, unexpected terminator, expecting literal content or tSTRING_DBEG or tSTRING_DVAR or tLABEL_END (SyntaxError)
    json in {"key" => /value/}
                 ^
    
            .../ruby/3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/exe/irb:9:in `<top (required)>'
            .../ruby/3.2.0/bin/irb:25:in `load'
            .../ruby/3.2.0/bin/irb:25:in `<main>'
    ```
    
    When the Hash maps String keys to Symbol keys, it's able to be pattern
    matched:
    
    ```ruby
    irb(main):005:0> json = {"key" => "value"}.with_indifferent_access
    => {"key"=>"value"}
    irb(main):006:0> json in {key: /value/}
    => true
    ```
    
    [object_class]: https://docs.ruby-lang.org/en/3.2/JSON.html#module-JSON-label-Parsing+Options
Loading