Sha256: f5d054e54b67d6a60fcc6c86b07a55740f0af458d5421d3791c5fdfcce202955

Contents?: true

Size: 1.11 KB

Versions: 15

Compression:

Stored size: 1.11 KB

Contents

module GraphitiSpecHelpers
  class ErrorsProxy
    class Error
      attr_reader :json

      def initialize(json)
        @json = json
      end

      def attribute
        @json[:meta][:attribute]
      end

      # TODO: move to top-level code in errorable
      def code
        @json[:meta][:code]
      end

      def message
        @json[:meta][:message]
      end

      def title
        @json[:title]
      end

      def detail
        @json[:detail]
      end

      def status
        @json[:status]
      end
    end

    include Enumerable

    def initialize(array)
      @errors = array.map { |e| Error.new(e) }
    end

    def [](key)
      @errors[key]
    end

    def each(&blk)
      @errors.each(&blk)
    end

    def length
      count
    end

    def to_h
      {}.tap do |hash|
        @errors.each do |e|
          hash[e.attribute] = e.message
        end
      end
    end

    def method_missing(id, *args, &blk)
      if error = @errors.select { |e| e.attribute.to_sym == id }
        error = error[0] if error.length == 1
        return error
      else
        super
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
graphiti_spec_helpers-1.2.0 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.1.0 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.5 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.3 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.2 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.1 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.0 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.rc.2 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.rc.1 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.beta.6 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.beta.4 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.beta.3 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.beta.2 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.beta.1 lib/graphiti_spec_helpers/errors_proxy.rb
graphiti_spec_helpers-1.0.alpha.8 lib/graphiti_spec_helpers/errors_proxy.rb