Sha256: 041748fedea48c29c796666d7a5204aca120e2e4e4ad5284fdd2cd17b9188765

Contents?: true

Size: 1.36 KB

Versions: 26

Compression:

Stored size: 1.36 KB

Contents

module Pageflow
  module JsonTestHelper
    def json_response(options = {})
      json_get(response.body, options)
    end

    def json_get(text, options = {})
      object = JSON.parse(text)
      path = options[:path] || []
      pretty_path = nil

      Array.wrap(path).inject(object) do |component, key|
        case key
        when Integer, '*'
          expect(component).to be_a(Array),
                               "Expected json response to have array at '#{pretty_path || 'root'}'"
          pretty_path = [pretty_path, "[#{key}]"].compact.join

          if key == '*'
            component
          else
            expect(component[key]).to be_present,
                                      "Expected json response to have item at '#{pretty_path}'"
            component[key]
          end
        when String, Symbol
          key = key.to_s
          pretty_path = [pretty_path, key].compact * '.'

          case component
          when Array
            component.map do |c|
              expect(c).to have_key(key),
                           "Expected json response to have key '#{pretty_path}'"
              c[key]
            end
          else
            expect(component).to have_key(key),
                                 "Expected json response to have key '#{pretty_path}'"
            component[key]
          end
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
pageflow-support-17.0.4 pageflow/json_test_helper.rb
pageflow-support-17.0.3 pageflow/json_test_helper.rb
pageflow-support-17.0.2 pageflow/json_test_helper.rb
pageflow-support-17.0.1 pageflow/json_test_helper.rb
pageflow-support-17.0.0 pageflow/json_test_helper.rb
pageflow-support-16.2.0 pageflow/json_test_helper.rb
pageflow-support-16.1.0 pageflow/json_test_helper.rb
pageflow-support-16.0.0 pageflow/json_test_helper.rb
pageflow-support-15.8.0 pageflow/json_test_helper.rb
pageflow-support-15.7.1 pageflow/json_test_helper.rb
pageflow-support-15.7.0 pageflow/json_test_helper.rb
pageflow-support-15.6.1 pageflow/json_test_helper.rb
pageflow-support-15.6.0 pageflow/json_test_helper.rb
pageflow-support-15.5.0 pageflow/json_test_helper.rb
pageflow-support-15.4.0 pageflow/json_test_helper.rb
pageflow-support-15.3.0 pageflow/json_test_helper.rb
pageflow-support-15.2.2 pageflow/json_test_helper.rb
pageflow-support-15.2.1 pageflow/json_test_helper.rb
pageflow-support-15.2.0 pageflow/json_test_helper.rb
pageflow-support-15.1.2 pageflow/json_test_helper.rb