Sha256: 9b4c4f3a81b7b9049584988967dff01850b86c39319caaf284f6303d77d73b86

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require "rspec/api_helpers/version"

module Rspec
  module ApiHelpers
    module ExampleMethods
      def objectize_resources(json, root:)
        array = []
        array_hash = HashWithIndifferentAccess.new(JSON.load(json))

        if root
          array_hash = array_hash[root]
        end

        array_hash.each do |resource|
          array << object_hash(resource)
        end

        return array
      end

      def objectize_resource(json, root:)
        hash = HashWithIndifferentAccess.new(JSON.load(json))
        if root
          obj = object_hash(hash[root])
        else
          obj = object_hash(hash)
        end

        return obj
      end

      def object_hash(hash)
        ObjectHash.new(hash)
      end

      class ObjectHash
        attr_accessor :hash
        def initialize(hash)
          @hash = HashWithIndifferentAccess.new(hash)
        end
        def method_missing(name)
          return hash[name] if hash.key? name
          raise KeyError.new("Attribute not found in resource: #{name}")
        end
      end
    end
  end
end


Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec-api_helpers-0.0.2 lib/rspec/api_helpers/example_methods.rb
rspec-api_helpers-0.0.1 lib/rspec/api_helpers/example_methods.rb