Sha256: d4f622b5f2deb4105dc24bcba178ffa8efa2bac1f15567c02e29ad24ceaa6561

Contents?: true

Size: 978 Bytes

Versions: 6

Compression:

Stored size: 978 Bytes

Contents

require 'hashie'

module RailsStuff
  module TestHelpers
    module Response
      class << self
        # Return `Hashie::Mash` for a given object. When `Array` is given
        # it is mapped to mash recursievly.
        def prepare_json_object(object)
          case object
          when Hash then Hashie::Mash.new(object)
          when Array then object.map(&method(__callee__))
          else object
          end
        end
      end

      # Easy access to json bodies. It parses and return `Hashie::Mash`'es, so
      # properties can be accessed via method calls:
      #
      #     response.json_body.order.items.id
      #     # note that hash methods are still present:
      #     response.json_body.order[:key] # instead of order.key
      def json_body
        @json_body ||= Response.prepare_json_object(JSON.parse(body))
      end

      # Makes it easier to debug failed specs.
      def inspect
        "<Response(#{status})>"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails_stuff-0.5.1 lib/rails_stuff/test_helpers/response.rb
rails_stuff-0.5.0 lib/rails_stuff/test_helpers/response.rb
rails_stuff-0.4.0 lib/rails_stuff/test_helpers/response.rb
rails_stuff-0.3.0 lib/rails_stuff/test_helpers/response.rb
rails_stuff-0.2.0 lib/rails_stuff/test_helpers/response.rb
rails_stuff-0.1.0 lib/rails_stuff/test_helpers/response.rb