lib/augury/fortune.rb in augury-0.3.0 vs lib/augury/fortune.rb in augury-1.0.0

- old
+ new

@@ -1,81 +1,78 @@ +# frozen_string_literal: true + +require 'cgi' require 'facets/string/word_wrap' -require 'parseconfig' require 'twitter' -require 'wannabe_bool' module Augury class Fortune - def initialize(username, path, width=nil, append=nil, count=nil) - begin - @config = ParseConfig.new(File.expand_path('~/.augury.cfg')) - rescue Errno::EACCES - @config = ParseConfig.new - end - - augury_config = @config.params['augury'] || {} + def initialize(username, path, config) @username = username @path = path - @width = (width || augury_config['width'] || 72).to_i - @append = (append || augury_config['append'] || false).to_b - @count = (count || augury_config['count'] || 200).to_i - - twitter_config = @config.params['twitter'] - raise Augury::TwitterConfigError unless twitter_config - @twitter = Twitter::REST::Client.new do |config| - config.consumer_key = twitter_config['consumer_key'] - config.consumer_secret = twitter_config['consumer_secret'] - config.access_token = twitter_config['access_token'] - config.access_token_secret = twitter_config['access_token_secret'] - end + @config = config + @tweets = [] end - def collect_with_max_id(collection=[], max_id=nil, &block) + def collect_with_max_id(collection = [], max_id = nil, &block) response = yield(max_id) collection += response if response.empty? collection.flatten - elsif ! @count.zero? && collection.length >= @count + elsif !@config[:count].zero? && collection.length >= @config[:count] collection.flatten else collect_with_max_id(collection, response.last.id - 1, &block) end end - def tweets - begin - collect_with_max_id do |max_id| - options = { - count: @count.zero? ? 200 : @count, - include_rts: true, - } - options[:max_id] = max_id unless max_id.nil? - @twitter.user_timeline(@username, options) - end - rescue Twitter::Error::TooManyRequests => e - reset_length = e.rate_limit.reset_in + 1 - puts "Twitter rate limit exceeded. Waiting #{reset_length} minute(s)" - sleep reset_length + def retrieve_tweets + collect_with_max_id do |max_id| + options = { + count: @config[:count].zero? ? 200 : @config[:count], + include_rts: @config[:retweets], + exclude_replies: !@config[:replies], + } + options[:max_id] = max_id unless max_id.nil? + @tweets = @twitter.user_timeline(@username, options) end + rescue Twitter::Error::TooManyRequests => e + reset_length = e.rate_limit.reset_in + 1 + puts "Twitter rate limit exceeded. Waiting #{reset_length} minute(s)" + sleep reset_length end def format_fortune - tweet_texts = self.tweets.flat_map { |tweet| tweet.full_text } - tweet_texts.flat_map { |tweet| tweet.word_wrap(@width) }.join("%\n") + filtered = @tweets.flat_map(&:full_text).reject do |tweet| + tweet.match(/https?:/) unless @config[:links] + end + formatted = filtered.flat_map { |tweet| CGI.unescapeHTML(tweet).word_wrap(@config[:width]) } + author = @config[:attribution] ? "\n-- #{@twitter.user(@username).name}\n" : '' + formatted.join("#{author}%\n") end def write_fortune - text = self.format_fortune # Write out the file begin - mode = @append ? 'a' : 'w' + mode = @config[:append] ? 'a' : 'w' file = File.open(@path, mode) - file.write("%\n") if @append - file.write(text) + file.write("%\n") if @config[:append] + file.write(format_fortune) ensure - file.close unless file.nil? + file&.close end # Create the dat file too `strfile '#{@path}' '#{@path}.dat'` + end + + def twitter_setup + raise Augury::TwitterConfigError unless @config[:twitter] + + @twitter = Twitter::REST::Client.new do |cfg| + cfg.consumer_key = @config[:twitter]['consumer_key'] + cfg.consumer_secret = @config[:twitter]['consumer_secret'] + cfg.access_token = @config[:twitter]['access_token'] + cfg.access_token_secret = @config[:twitter]['access_token_secret'] + end end end end