Sha256: a9a2ace0ee3fec18205b52933baf690e2cfe7361fbf5b957e8f46bc922328467

Contents?: true

Size: 1.03 KB

Versions: 21

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module NeetoCommonsBackend
  module ApiResponders
    def render_message(notice_code, status, **context)
      render status:, json: { notice_code: }.merge(context)
    end

    def render_success(**context)
      render_message "thumbs_up", :ok, **context
    end

    def render_error(error, status = :unprocessable_entity, context = {})
      error_message = error
      is_exception = error.kind_of?(StandardError)
      if is_exception
        is_having_record = error.methods.include? "record"
        error_message = is_having_record ? error.record.errors_to_sentence : error.message
      end
      error_message = error_message.first if error_message.is_a?(Array) && error_message.length == 1
      key = error_message.is_a?(Array) ? "errors" : "error"
      render_json({ key => error_message }.merge(context), status)
    end

    def render_notice(notice, context = {})
      render_json({ notice: }.merge(context))
    end

    def render_json(json = {}, status = :ok)
      render status:, json:
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
neeto-commons-backend-1.0.109 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.108 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.107 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.106 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.105 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.104 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.103 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.102 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.101 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.100 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.99 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.98 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.97 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.96 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.95 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.94 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.93 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.92 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.91 lib/neeto_commons_backend/controllers/concerns/api_responders.rb
neeto-commons-backend-1.0.90 lib/neeto_commons_backend/controllers/concerns/api_responders.rb