lib/chatterbot/config.rb in chatterbot-0.7.1 vs lib/chatterbot/config.rb in chatterbot-0.9.0
- old
+ new
@@ -2,11 +2,13 @@
#
# routines for storing config information for the bot
module Config
attr_accessor :config
-
+
+ MAX_TWEET_ID = 9223372036854775807
+
#
# the entire config for the bot, loaded from YAML files and the DB if applicable
def config
@config ||= load_config
end
@@ -73,11 +75,11 @@
end
def verbose?
config[:verbose] || false
end
-
+
#
# destination for log entries
def log_dest
config[:log_dest]
end
@@ -125,19 +127,22 @@
def update_config_at_exit
update_config
end
def max_id_from(s)
- s.max { |a, b| a.id <=> b.id }.id
+ # 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
#
# update the since_id_reply with the id of the given tweet,
# unless it is lower thant what we have already
#
def update_since_id_reply(tweet)
- return if tweet.nil? or tweet.class != Twitter::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
@@ -145,33 +150,21 @@
#
# update the since_id with either the highest ID of the specified
# tweets, unless it is lower than what we have already
def update_since_id(search)
return if search.nil?
-
-
- tmp_id = case search
- when Twitter::SearchResults then
- # don't use max_id if it's this ridiculous number
- # @see https://dev.twitter.com/issues/1300
- if search.max_id != 9223372036854775807
- search.max_id
- else
- max_id_from(search)
- end
-
- # incoming tweets
- when Twitter::Tweet then search.id
-
- # an enumeration
- when Array then max_id_from(search)
-
- # probably an actual tweet ID at this point,
- # otherwise it will fail and return 0
- else search
+
+ 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
#
# return a hash of the params we need to connect to the Twitter API
@@ -198,11 +191,11 @@
#
# determine if we're being called by one of our internal scripts
#
def chatterbot_helper?
- File.basename($0).include?("chatterbot-")
+ Chatterbot::from_helper == true
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
@@ -219,11 +212,10 @@
#
# figure out what config file to load based on the name of the bot
def config_file
dest = working_dir
-
x = File.join(File.expand_path(dest), "#{botname}.yml")
end
#
# load in a config file
@@ -266,11 +258,16 @@
end
#
# bot-specific config settings
def bot_config
- slurp_file(config_file) || { }
+ {
+ :consumer_key => ENV["chatterbot_consumer_key"],
+ :consumer_secret => ENV["chatterbot_consumer_secret"],
+ :token => ENV["chatterbot_token"],
+ :secret => ENV["chatterbot_secret"]
+ }.merge(slurp_file(config_file) || {})
end
#
# load the config settings from the db, if possible
def db_config
@@ -296,10 +293,9 @@
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
-
#
# load in the config from the assortment of places it can be specified.
def load_config(params={})
# load the flat-files first