Class: InfinumJsonApiSetup::RSpec::Matchers::Util::BodyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/infinum_json_api_setup/rspec/matchers/util/body_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ BodyParser

Returns a new instance of BodyParser.

Parameters:

  • attribute (String)


9
10
11
# File 'lib/infinum_json_api_setup/rspec/matchers/util/body_parser.rb', line 9

def initialize(attribute)
  @attribute = attribute
end

Instance Method Details

#process(response) ⇒ Array

Parameters:

  • response (ActionDispatch::TestResponse)

Returns:

  • (Array)


15
16
17
18
19
20
21
# File 'lib/infinum_json_api_setup/rspec/matchers/util/body_parser.rb', line 15

def process(response)
  [true, JSON.parse(response.body).fetch(attribute)]
rescue JSON::ParserError => _e
  [false, 'Failed to parse response body']
rescue KeyError => _e
  [false, "Failed to extract #{attribute} from response body"]
end