Sha256: 823bdf3045250423945db97dd4e7a1920cb9cbd676d827f307c70db8597d4d80

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Monday
  # Utility class to format arguments for Monday.com API.
  class Util
    class << self
      # Converts the arguments object into a valid string for API.
      #
      # input: { key: "multiple word value" }
      # output: "key: \"multiple word value\""
      def format_args(obj)
        obj.map do |key, value|
          "#{key}: #{formatted_args_value(value)}"
        end.join(", ")
      end

      # Converts the select values into a valid string for API.
      #
      # input: ["id", "name", { "columns": ["id"] }]
      # output: "id name columns { id }"
      def format_select(value)
        return format_hash(value) if value.is_a?(Hash)
        return format_array(value) if value.is_a?(Array)

        values
      end

      private

      def format_array(array)
        array.map do |item|
          item.is_a?(Hash) ? format_hash(item) : item.to_s
        end.join(" ")
      end

      def format_hash(hash)
        hash.map do |key, value|
          value.is_a?(Array) ? "#{key} { #{format_array(value)} }" : "#{key} { #{value} }"
        end.join(" ")
      end

      def formatted_args_value(value)
        return "\"#{value}\"" unless single_word?(value)
        return value.to_json.to_json if value.is_a?(Hash)

        value
      end

      def single_word?(word)
        return word unless word.is_a?(String)

        !word.strip.include?(" ")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
monday_ruby-0.2.0 lib/monday/util.rb
monday_ruby-0.1.0 lib/monday/util.rb