Module: Chatterbot::Config
- Included in:
- Bot
- Defined in:
- lib/chatterbot/config.rb
Overview
routines for storing config information for the bot
Instance Attribute Summary collapse
-
#config ⇒ Object
the entire config for the bot, loaded from YAML files and the DB if applicable.
Class Method Summary collapse
-
.attr_boolean(key, default = false) ⇒ Object
simple boolean attribute generator.
-
.attr_since_id(key = nil) ⇒ Object
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.
Instance Method Summary collapse
-
#bot_config ⇒ Object
bot-specific config settings.
-
#chatterbot_helper? ⇒ Boolean
determine if we’re being called by one of our internal scripts.
-
#client_params ⇒ Object
return a hash of the params we need to connect to the Twitter API.
-
#config_file ⇒ Object
figure out what config file to load based on the name of the bot.
-
#global_config ⇒ Object
get any config from our global config files.
-
#global_config_files ⇒ Object
our list of “global config files”.
-
#load_config(params = {}) ⇒ Object
load in the config from the assortment of places it can be specified.
-
#log_dest ⇒ Object
destination for log entries.
-
#logging? ⇒ Boolean
should we write to a log file?.
-
#max_id_from(s) ⇒ Object
given an array or object, return the highest id we can find.
-
#needs_api_key? ⇒ Boolean
do we have an API key specified?.
-
#needs_auth_token? ⇒ Boolean
has this script validated with Twitter OAuth?.
-
#no_update=(val) ⇒ Object
should we update our config values?.
-
#no_update? ⇒ Boolean
should we update our config values?.
-
#slurp_file(f) ⇒ Object
load in a config file.
-
#update_config? ⇒ Boolean
Should we run any config updates?.
-
#working_dir ⇒ Object
if we are called by a bot, we want to use the directory of that script.
Instance Attribute Details
#config ⇒ Object
the entire config for the bot, loaded from YAML files and the DB if applicable
10 11 12 |
# File 'lib/chatterbot/config.rb', line 10 def config @config end |
Class Method Details
.attr_boolean(key, default = false) ⇒ Object
simple boolean attribute generator. define the attribute and a default value and you get a setter and predicate method
21 22 23 24 25 26 27 28 29 |
# 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 |
.attr_since_id(key = nil) ⇒ Object
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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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 |
Instance Method Details
#bot_config ⇒ Object
bot-specific config settings
209 210 211 212 213 214 215 216 |
# 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 |
#chatterbot_helper? ⇒ Boolean
determine if we’re being called by one of our internal scripts
143 144 145 |
# File 'lib/chatterbot/config.rb', line 143 def chatterbot_helper? Chatterbot::from_helper == true end |
#client_params ⇒ Object
return a hash of the params we need to connect to the Twitter API
80 81 82 83 84 85 86 87 |
# 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 |
#config_file ⇒ Object
figure out what config file to load based on the name of the bot
163 164 165 166 |
# File 'lib/chatterbot/config.rb', line 163 def config_file dest = working_dir File.join(File.(dest), "#{botname}.yml") end |
#global_config ⇒ Object
get any config from our global config files
199 200 201 202 203 204 205 |
# File 'lib/chatterbot/config.rb', line 199 def global_config tmp = {} global_config_files.each { |f| tmp.merge!(slurp_file(f) || {}) } tmp end |
#global_config_files ⇒ Object
our list of “global config files”
184 185 186 187 188 189 190 191 192 193 194 195 |
# 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_config(params = {}) ⇒ Object
load in the config from the assortment of places it can be specified.
221 222 223 224 |
# 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 |
#log_dest ⇒ Object
destination for log entries
117 118 119 |
# File 'lib/chatterbot/config.rb', line 117 def log_dest config[:log_dest] end |
#logging? ⇒ Boolean
should we write to a log file?
111 112 113 |
# File 'lib/chatterbot/config.rb', line 111 def logging? config[:log_dest] != nil end |
#max_id_from(s) ⇒ Object
given an array or object, return the highest id we can find
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# 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 |
#needs_api_key? ⇒ Boolean
do we have an API key specified?
91 92 93 |
# File 'lib/chatterbot/config.rb', line 91 def needs_api_key? config[:consumer_key].nil? || config[:consumer_secret].nil? end |
#needs_auth_token? ⇒ Boolean
has this script validated with Twitter OAuth?
98 99 100 |
# File 'lib/chatterbot/config.rb', line 98 def needs_auth_token? config[:access_token].nil? end |
#no_update=(val) ⇒ Object
should we update our config values?
69 70 71 |
# File 'lib/chatterbot/config.rb', line 69 def no_update=(val) config.no_update = val end |
#no_update? ⇒ Boolean
should we update our config values?
74 75 76 |
# File 'lib/chatterbot/config.rb', line 74 def no_update? config.no_update || false end |
#slurp_file(f) ⇒ Object
load in a config file
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/chatterbot/config.rb', line 170 def slurp_file(f) f = File.(f) tmp = {} if File.exist?(f) File.open( f ) { |yf| tmp = YAML::load( yf ) } end tmp.symbolize_keys! unless tmp == false end |
#update_config? ⇒ Boolean
Should we run any config updates?
105 106 107 |
# File 'lib/chatterbot/config.rb', line 105 def update_config? !no_update? end |
#working_dir ⇒ Object
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
152 153 154 155 156 157 158 159 |
# File 'lib/chatterbot/config.rb', line 152 def working_dir if chatterbot_helper? Dir.getwd else File.dirname($0) #Dir.pwd end end |