Sha256: fd41fb6d8367f02675f5f1b67e794395d9a12f55a46a585ea5bd9c86f3df448d

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Qismo
  #
  # Utilities for Qismo Ruby client
  #
  module Helpers
    module BaseHelper
      #
      # Validate the existence of hash attributes
      #
      # @param args [Array<Symbol,String>]
      # @param on [Hash]
      #
      #
      def validate_existence_of!(*args, on:)
        args.each do |arg|
          falsy = (on[arg] == {} || on[arg] == [] || on[arg].nil? || on[arg] == "" || on[arg] == 0)
          raise BadRequest, "#{arg} is required" if falsy
        end
      end

      #
      # Check the argument is false or not
      #
      # @param arg [String, Integer, Hash, Array, NilClass]
      #
      # @return [TrueClass, FalseClass]
      #
      def falsy?(arg)
        (arg == {} || arg == [] || arg.nil? || arg == "" || arg == 0)
      end

      #
      # Safely parse json string
      #
      # @param str [String]
      #
      # @return [Hash,String]
      #
      def safe_parse_json(str, symbolize_names: false)
        str = str.to_json if str.is_a?(Hash)

        JSON.parse(str, symbolize_names: symbolize_names)
      rescue JSON::ParserError
        str
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
qismo-0.1.8 lib/qismo/helpers/base_helper.rb
qismo-0.1.5 lib/qismo/helpers/base_helper.rb
qismo-0.1.4 lib/qismo/helpers/base_helper.rb
qismo-0.1.2 lib/qismo/helpers/base_helper.rb
qismo-0.1.1 lib/qismo/helpers/base_helper.rb
qismo-0.1.0 lib/qismo/helpers/base_helper.rb