module Chatterbot::Config
routines for storing config information for the bot
Attributes
Public Class Methods
simple boolean attribute generator. define the attribute and a default value and you get a setter and predicate method
@param [Symbol] key the key for the variable @param [Boolean] default default value
# File lib/chatterbot/config.rb, line 21 def attr_boolean(key, default=false) class_eval <<-EVAL attr_writer :#{key.to_s} def #{key.to_s}? (@#{key.to_s} == true) || #{default} end EVAL end
generate a set of methods to manage checks around the assortment of since_id values we use to track most recent data retrieve from twitter
@param [Symbol] key the key for the variable
# File lib/chatterbot/config.rb, line 37 def attr_since_id(key = nil) attr_name = key.nil? ? "since_id" : ["since_id", key.to_s].join("_") class_eval <<-EVAL def #{attr_name}=(x) config[:#{attr_name}] = x end def #{attr_name} config[:#{attr_name}] || 1 end def update_#{attr_name}(input) max = max_id_from(input) config[:#{attr_name}] = [config[:#{attr_name}].to_i, max.to_i].max end EVAL end
Public Instance Methods
bot-specific config settings
# File lib/chatterbot/config.rb, line 209 def bot_config { :consumer_key => ENV["chatterbot_consumer_key"], :consumer_secret => ENV["chatterbot_consumer_secret"], :access_token => ENV["chatterbot_access_token"], :access_token_secret => ENV["chatterbot_access_secret"] || ENV["chatterbot_access_token_secret"] }.delete_if { |k, v| v.nil? }.merge(slurp_file(config_file) || {}) end
determine if we're being called by one of our internal scripts
# File lib/chatterbot/config.rb, line 143 def chatterbot_helper? Chatterbot::from_helper == true end
return a hash of the params we need to connect to the Twitter API
# File lib/chatterbot/config.rb, line 80 def client_params { :consumer_key => config[:consumer_key], :consumer_secret => config[:consumer_secret], :access_token => config[:access_token], :access_token_secret => config[:access_token_secret] } end
figure out what config file to load based on the name of the bot
# File lib/chatterbot/config.rb, line 163 def config_file dest = working_dir File.join(File.expand_path(dest), "#{botname}.yml") end
get any config from our global config files
# File lib/chatterbot/config.rb, line 199 def global_config tmp = {} global_config_files.each { |f| tmp.merge!(slurp_file(f) || {}) } tmp end
our list of “global config files”
# File lib/chatterbot/config.rb, line 184 def global_config_files [ # a system-wide global path "/etc/chatterbot.yml", # a file specified in ENV ENV["chatterbot_config"], # 'global' config file local to the path of the ruby script File.join(working_dir, "global.yml") ].compact end
load in the config from the assortment of places it can be specified.
# File lib/chatterbot/config.rb, line 221 def load_config(params={}) read_only_data = global_config.merge(bot_config).merge(params) @config = Chatterbot::ConfigManager.new(config_file, read_only_data) end
destination for log entries
# File lib/chatterbot/config.rb, line 117 def log_dest config[:log_dest] end
should we write to a log file?
# File lib/chatterbot/config.rb, line 111 def logging? config[:log_dest] != nil end
given an array or object, return the highest id we can find @param [Enumerable] s the array to check
# File lib/chatterbot/config.rb, line 125 def max_id_from(s) if ! s.respond_to?(:max) if s.respond_to?(:id) return s.id else return s end end sorted = s.max { |a, b| a.id.to_i <=> b.id.to_i } sorted && sorted.id end
do we have an API key specified?
# File lib/chatterbot/config.rb, line 91 def needs_api_key? config[:consumer_key].nil? || config[:consumer_secret].nil? end
has this script validated with Twitter OAuth?
# File lib/chatterbot/config.rb, line 98 def needs_auth_token? config[:access_token].nil? end
should we update our config values? @param [Boolean] val true/false
# File lib/chatterbot/config.rb, line 69 def no_update=(val) config.no_update = val end
should we update our config values?
# File lib/chatterbot/config.rb, line 74 def no_update? config.no_update || false end
load in a config file
# File lib/chatterbot/config.rb, line 170 def slurp_file(f) f = File.expand_path(f) tmp = {} if File.exist?(f) File.open( f ) { |yf| tmp = YAML::load( yf ) } end tmp.symbolize_keys! unless tmp == false end
Should we run any config updates?
# File lib/chatterbot/config.rb, line 105 def update_config? !no_update? end
if we are called by a bot, we want to use the directory of that script. If we are called by chatterbot-register or another helper script, we want to use the current working directory
# File lib/chatterbot/config.rb, line 152 def working_dir if chatterbot_helper? Dir.getwd else File.dirname($0) #Dir.pwd end end