Module: Chatterbot::Config
- Included in:
- Bot
- Defined in:
- lib/chatterbot/config.rb
Overview
routines for storing config information for the bot
Constant Summary collapse
- MAX_TWEET_ID =
9223372036854775807
Instance Attribute Summary collapse
-
#config ⇒ Object
the entire config for the bot, loaded from YAML files and the DB if applicable.
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.
-
#config_to_save ⇒ Object
figure out what we should save to the local config file.
-
#db_config ⇒ Object
load the config settings from the db, if possible.
- #debug_mode=(d) ⇒ Object
-
#debug_mode? ⇒ Boolean
are we in debug mode?.
-
#global_config ⇒ Object
get any config from our global config files.
-
#global_config_files ⇒ Object
our list of “global config files”.
-
#has_config? ⇒ Boolean
has the config been loaded yet?.
-
#has_db? ⇒ Boolean
do we have a DB connection string?.
-
#has_sequel? ⇒ Boolean
Check to see if Sequel was loaded successfully.
-
#load_config(params = {}) ⇒ Object
load in the config from the assortment of places it can be specified.
-
#log_dest ⇒ Object
destination for log entries.
-
#log_tweets? ⇒ Boolean
should we log tweets to the database?.
-
#logging? ⇒ Boolean
should we write to a log file?.
- #max_id_from(s) ⇒ Object
-
#needs_api_key? ⇒ Boolean
do we have an API key specified?.
-
#needs_auth_token? ⇒ Boolean
has this script validated with Twitter OAuth?.
- #no_update=(d) ⇒ Object
-
#reset_bot? ⇒ Boolean
should we reset the since_id for this bot?.
-
#since_id ⇒ Object
return the ID of the most recent tweet pulled up in searches.
-
#since_id=(x) ⇒ Object
store since_id to a different key so that it doesn’t actually get updated until the bot is done running.
-
#since_id_reply ⇒ Object
return the ID of the most recent tweet pulled up in mentions or since_id if since_id_reply is nil.
-
#since_id_reply=(x) ⇒ Object
store since_id_reply to a different key so that it doesn’t actually get updated until the bot is done running.
-
#slurp_file(f) ⇒ Object
load in a config file.
-
#store_database_config ⇒ Object
store config settings in the database, if possible.
-
#store_local_config ⇒ Object
write out the config file for this bot.
-
#update_config ⇒ Object
write out our config file.
-
#update_config? ⇒ Boolean
Should we run any config updates?.
- #update_config_at_exit ⇒ Object
-
#update_since_id(search) ⇒ Object
update the since_id with either the highest ID of the specified tweets, unless it is lower than what we have already.
-
#update_since_id_reply(tweet) ⇒ Object
update the since_id_reply with the id of the given tweet, unless it is lower thant what we have already.
- #verbose=(v) ⇒ Object
- #verbose? ⇒ Boolean
-
#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
12 13 14 |
# File 'lib/chatterbot/config.rb', line 12 def config @config end |
Instance Method Details
#bot_config ⇒ Object
bot-specific config settings
261 262 263 264 265 266 267 268 |
# File 'lib/chatterbot/config.rb', line 261 def bot_config { :consumer_key => ENV["chatterbot_consumer_key"], :consumer_secret => ENV["chatterbot_consumer_secret"], :token => ENV["chatterbot_token"], :secret => ENV["chatterbot_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
195 196 197 |
# File 'lib/chatterbot/config.rb', line 195 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
171 172 173 174 175 176 177 178 |
# File 'lib/chatterbot/config.rb', line 171 def client_params { :consumer_key => config[:consumer_key], :consumer_secret => config[:consumer_secret], :token => config[:token].nil? ? nil : config[:token], :secret => config[:secret].nil? ? nil : config[:secret] } end |
#config_file ⇒ Object
figure out what config file to load based on the name of the bot
215 216 217 218 |
# File 'lib/chatterbot/config.rb', line 215 def config_file dest = working_dir File.join(File.(dest), "#{botname}.yml") end |
#config_to_save ⇒ Object
figure out what we should save to the local config file. we don’t save anything that exists in the global config, unless it’s been modified for this particular bot.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/chatterbot/config.rb', line 281 def config_to_save # remove keys that are duped in the global config tmp = config.delete_if { |k, v| global_config.has_key?(k) && global_config[k] == config[k] } # let's not store these, they're just command-line options tmp.delete(:debug_mode) tmp.delete(:no_update) tmp.delete(:verbose) # update the since_id now tmp[:since_id] = tmp.delete(:tmp_since_id) unless ! tmp.has_key?(:tmp_since_id) tmp[:since_id_reply] = tmp.delete(:tmp_since_id_reply) unless ! tmp.has_key?(:tmp_since_id_reply) tmp end |
#db_config ⇒ Object
load the config settings from the db, if possible
272 273 274 275 |
# File 'lib/chatterbot/config.rb', line 272 def db_config return {} if db.nil? db[:config][:id => botname] end |
#debug_mode=(d) ⇒ Object
40 41 42 |
# File 'lib/chatterbot/config.rb', line 40 def debug_mode=(d) config[:debug_mode] = d end |
#debug_mode? ⇒ Boolean
are we in debug mode?
57 58 59 |
# File 'lib/chatterbot/config.rb', line 57 def debug_mode? config[:debug_mode] || false end |
#global_config ⇒ Object
get any config from our global config files
251 252 253 254 255 256 257 |
# File 'lib/chatterbot/config.rb', line 251 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”
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/chatterbot/config.rb', line 236 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 |
#has_config? ⇒ Boolean
has the config been loaded yet?
18 19 20 |
# File 'lib/chatterbot/config.rb', line 18 def has_config? ! @config.nil? end |
#has_db? ⇒ Boolean
do we have a DB connection string?
36 37 38 |
# File 'lib/chatterbot/config.rb', line 36 def has_db? has_sequel? && config.has_key?(:db_uri) end |
#has_sequel? ⇒ Boolean
Check to see if Sequel was loaded successfully. If not, we won’t make any DB calls
30 31 32 |
# File 'lib/chatterbot/config.rb', line 30 def has_sequel? ! defined?(Sequel).nil? end |
#load_config(params = {}) ⇒ Object
load in the config from the assortment of places it can be specified.
300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/chatterbot/config.rb', line 300 def load_config(params={}) # load the flat-files first @config = global_config.merge(bot_config) @config[:db_uri] ||= ENV["chatterbot_db"] unless ENV["chatterbot_db"].nil? # if we have a key to load from the DB, do that now if @config.has_key?(:db_uri) && @config[:db_uri] tmp = db_config @config = @config.merge(tmp) unless tmp.nil? end @config.merge(params) end |
#log_dest ⇒ Object
destination for log entries
83 84 85 |
# File 'lib/chatterbot/config.rb', line 83 def log_dest config[:log_dest] end |
#log_tweets? ⇒ Boolean
should we log tweets to the database?
24 25 26 |
# File 'lib/chatterbot/config.rb', line 24 def log_tweets? config.has_key?(:db_uri) end |
#logging? ⇒ Boolean
should we write to a log file?
69 70 71 |
# File 'lib/chatterbot/config.rb', line 69 def logging? has_config? && config.has_key?(:log_dest) end |
#max_id_from(s) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/chatterbot/config.rb', line 131 def max_id_from(s) # don't use max_id if it's this ridiculous number # @see https://dev.twitter.com/issues/1300 sorted = s.reject { |t| !t || t.id == MAX_TWEET_ID }.max { |a, b| a.id <=> b.id } sorted && sorted.id end |
#needs_api_key? ⇒ Boolean
do we have an API key specified?
182 183 184 |
# File 'lib/chatterbot/config.rb', line 182 def needs_api_key? config[:consumer_key].nil? || config[:consumer_secret].nil? end |
#needs_auth_token? ⇒ Boolean
has this script validated with Twitter OAuth?
188 189 190 |
# File 'lib/chatterbot/config.rb', line 188 def needs_auth_token? config[:token].nil? end |
#no_update=(d) ⇒ Object
44 45 46 |
# File 'lib/chatterbot/config.rb', line 44 def no_update=(d) config[:no_update] = d end |
#reset_bot? ⇒ Boolean
should we reset the since_id for this bot?
51 52 53 |
# File 'lib/chatterbot/config.rb', line 51 def reset_bot? config[:reset_since_id] || false end |
#since_id ⇒ Object
return the ID of the most recent tweet pulled up in searches
96 97 98 |
# File 'lib/chatterbot/config.rb', line 96 def since_id config[:since_id] || 0 end |
#since_id=(x) ⇒ Object
store since_id to a different key so that it doesn’t actually get updated until the bot is done running
90 91 92 |
# File 'lib/chatterbot/config.rb', line 90 def since_id=(x) config[:tmp_since_id] = x end |
#since_id_reply ⇒ Object
return the ID of the most recent tweet pulled up in mentions or since_id if since_id_reply is nil
109 110 111 |
# File 'lib/chatterbot/config.rb', line 109 def since_id_reply config[:since_id_reply] || since_id end |
#since_id_reply=(x) ⇒ Object
store since_id_reply to a different key so that it doesn’t actually get updated until the bot is done running
103 104 105 |
# File 'lib/chatterbot/config.rb', line 103 def since_id_reply=(x) config[:tmp_since_id_reply] = x end |
#slurp_file(f) ⇒ Object
load in a config file
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/chatterbot/config.rb', line 222 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 |
#store_database_config ⇒ Object
store config settings in the database, if possible
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/chatterbot/config.rb', line 321 def store_database_config return false if db.nil? configs = db[:config] data = { :since_id => config.has_key?(:tmp_since_id) ? config[:tmp_since_id] : config[:since_id], :since_id_reply => config.has_key?(:tmp_since_id_reply) ? config[:tmp_since_id_reply] : config[:since_id_reply], :token => config[:token], :secret => config[:secret], :consumer_secret => config[:consumer_secret], :consumer_key => config[:consumer_key], :updated_at => Time.now #:NOW.sql_function } row = configs.filter('id = ?', botname) if row.count > 0 row.update(data) else data[:id] = botname data[:created_at] = Time.now #:NOW.sql_function configs.insert data end true end |
#store_local_config ⇒ Object
write out the config file for this bot
315 316 317 |
# File 'lib/chatterbot/config.rb', line 315 def store_local_config File.open(config_file, 'w') { |f| YAML.dump(config_to_save, f) } end |
#update_config ⇒ Object
write out our config file
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/chatterbot/config.rb', line 115 def update_config return if ! update_config? # don't update flat file if we can store to the DB instead if has_db? debug "storing config to database -- you don't need local file anymore" store_database_config else store_local_config end end |
#update_config? ⇒ Boolean
Should we run any config updates?
63 64 65 |
# File 'lib/chatterbot/config.rb', line 63 def update_config? config.has_key?(:no_update) ? ! config[:no_update] : true end |
#update_config_at_exit ⇒ Object
127 128 129 |
# File 'lib/chatterbot/config.rb', line 127 def update_config_at_exit update_config end |
#update_since_id(search) ⇒ Object
update the since_id with either the highest ID of the specified tweets, unless it is lower than what we have already
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/chatterbot/config.rb', line 153 def update_since_id(search) return if search.nil? tmp_id = if search.is_a?(Twitter::SearchResults) search.attrs[:search_metadata][:max_id] elsif search.respond_to?(:max) max_id_from(search) elsif search.is_a?(Twitter::Tweet) search.id else search end.to_i config[:tmp_since_id] = [config[:tmp_since_id].to_i, tmp_id].max end |
#update_since_id_reply(tweet) ⇒ Object
update the since_id_reply with the id of the given tweet, unless it is lower thant what we have already
142 143 144 145 146 147 148 |
# File 'lib/chatterbot/config.rb', line 142 def update_since_id_reply(tweet) return if tweet.nil? or tweet.class != Twitter::Tweet || tweet.id == MAX_TWEET_ID tmp_id = tweet.id config[:tmp_since_id_reply] = [config[:tmp_since_id_reply].to_i, tmp_id].max end |
#verbose=(v) ⇒ Object
73 74 75 |
# File 'lib/chatterbot/config.rb', line 73 def verbose=(v) config[:verbose] = v end |
#verbose? ⇒ Boolean
77 78 79 |
# File 'lib/chatterbot/config.rb', line 77 def verbose? config[:verbose] || false 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
204 205 206 207 208 209 210 211 |
# File 'lib/chatterbot/config.rb', line 204 def working_dir if chatterbot_helper? Dir.getwd else File.dirname($0) #Dir.pwd end end |