Sha256: 79c24da6fd8cbc417b49484c8c088cc031791b324664f239ad9114d2e5c7e596

Contents?: true

Size: 1.45 KB

Versions: 30

Compression:

Stored size: 1.45 KB

Contents

module JsonApiClient
  class ErrorCollector < Array
    class Error
      delegate :[], to: :attrs

      def initialize(attrs = {})
        @attrs = (attrs || {}).with_indifferent_access
      end

      def id
        attrs[:id]
      end

      def about
        res = attrs.fetch(:links, {})
        res ? res[:about] : {}
      end

      def status
        attrs[:status]
      end

      def code
        attrs[:code]
      end

      def title
        attrs[:title]
      end

      def detail
        attrs[:detail]
      end

      def source_parameter
        source[:parameter]
      end

      def source_pointer
        source[:pointer]
      end

      def error_key
        if source_pointer && source_pointer != "/data"
          source_pointer.split("/").last
        else
          "base"
        end
      end

      def error_msg
        msg = title || detail || "invalid"
        if source_parameter
          "#{source_parameter} #{msg}"
        else
          msg
        end
      end

      def source
        res = attrs.fetch(:source, {})
        res ? res : {}
      end

      def meta
        MetaData.new(attrs.fetch(:meta, {}))
      end

      protected

      attr_reader :attrs
    end

    def initialize(error_data)
      super(error_data.map do |data|
        Error.new(data)
      end)
    end

    def full_messages
      map(&:title)
    end

    def [](source)
      map do |error|
        error.error_key == source
      end
    end

  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
json_api_client-1.23.0 lib/json_api_client/error_collector.rb
json_api_client-1.22.0 lib/json_api_client/error_collector.rb
json_api_client-1.21.1 lib/json_api_client/error_collector.rb
json_api_client-1.21.0 lib/json_api_client/error_collector.rb
json_api_client-1.20.0 lib/json_api_client/error_collector.rb
json_api_client-1.19.0 lib/json_api_client/error_collector.rb
carwow-json_api_client-1.19.0 lib/json_api_client/error_collector.rb
json_api_client-1.18.0 lib/json_api_client/error_collector.rb
json_api_client-1.17.1 lib/json_api_client/error_collector.rb
json_api_client-1.17.0 lib/json_api_client/error_collector.rb
json_api_client-1.16.1 lib/json_api_client/error_collector.rb
json_api_client-1.16.0 lib/json_api_client/error_collector.rb
json_api_client-1.15.0 lib/json_api_client/error_collector.rb
json_api_client-1.14.1 lib/json_api_client/error_collector.rb
json_api_client-1.14.0 lib/json_api_client/error_collector.rb
json_api_client-1.13.0 lib/json_api_client/error_collector.rb
json_api_client-1.12.2 lib/json_api_client/error_collector.rb
json_api_client-1.12.1 lib/json_api_client/error_collector.rb
json_api_client-1.12.0 lib/json_api_client/error_collector.rb
json_api_client-1.11.0 lib/json_api_client/error_collector.rb