Sha256: c7990429d9710c51421cd4ce559d63774bde90e68e3a130c72dac785a6a43d78

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require_relative 'messages'

module Voom
  module Commands
    class Response
      attr_reader :data, :status, :messages
      SUCCESS = 0
      FAILURE = 1

      def initialize(data: [],
                     status: SUCCESS,
                     messages: {})
        @data = data
        @status = status
        @messages = Messages.new(messages)
      end

      # If your data either is a hash or is an array containing one hash
      # This helper allows you to treat the result object as a hash
      # returns nil if there is more than one element in the data
      # Or if the data is empty
      def [](key)
        return data[key] if data.respond_to?(:key?) #quacks like a hash
        return data.send(key.to_sym) if data.respond_to?(key.to_sym) #behaves like a model/entity
        return nil if data.empty? or data.size > 1
        data.first[key] if data.first.respond_to?(:key?)
      end

      def <<(output)
        @data << output
      end

      def succeeded?
        @status == SUCCESS
      end

      alias success? succeeded?

      def failed?
        !succeeded?
      end

      alias fail? failed?

      def to_h
        {data: data,
         status: status,
         message: messages.to_h}
      end

      def errors
        @messages.errors
      end

      def warnings
        @messages.warnings
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
voom-commands-0.1.1 lib/voom/commands/response.rb
voom-commands-0.1.0 lib/voom/commands/response.rb