Sha256: 2f2f7f688f4559c655cdbd21e859c76599e54f725874e5cebf05fd39be828643

Contents?: true

Size: 912 Bytes

Versions: 2

Compression:

Stored size: 912 Bytes

Contents

# frozen_string_literal: true

module SlackRubyBot
  module Config
    extend self

    ATTRS = %i[token url aliases user user_id team team_id allow_bot_messages allow_message_loops send_gifs logger].freeze
    attr_accessor(*ATTRS)

    def allow_bot_messages?
      !!allow_bot_messages
    end

    def allow_message_loops?
      !!allow_message_loops
    end

    def send_gifs?
      return false unless defined?(Giphy)

      v = boolean_from_env('SLACK_RUBY_BOT_SEND_GIFS')
      send_gifs.nil? ? (v.nil? || v) : send_gifs
    end

    def reset!
      ATTRS.each { |attr| send("#{attr}=", nil) }
    end

    private

    def boolean_from_env(key)
      value = ENV[key]
      case value
      when nil
        nil
      when 0, 'false', 'no'
        false
      when 1, 'true', 'yes'
        true
      else
        raise ArgumentError, "Invalid value for #{key}: #{value}."
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slack-ruby-bot-0.15.0 lib/slack-ruby-bot/config.rb
slack-ruby-bot-0.14.0 lib/slack-ruby-bot/config.rb