lib/augury/fortune.rb in augury-0.1.0 vs lib/augury/fortune.rb in augury-0.2.0
- old
+ new
@@ -1,21 +1,55 @@
-require 'twitter'
+require 'facets/string/word_wrap'
require 'parseconfig'
+require 'twitter'
+require 'wannabe_bool'
module Augury
class Fortune
- def initialize
- # TODO: add a check for the file and warn the user
- @config = ParseConfig.new(File.expand_path('~/.augury.conf'))
+ def initialize(username, path, width=nil, append=nil)
+ begin
+ @config = ParseConfig.new(File.expand_path('~/.augury.cfg'))
+ rescue Errno::EACCES
+ @config = ParseConfig.new
+ end
+
+ augury_config = @config.params['augury'] || {}
+ @username = username
+ @path = path
+ @width = width || augury_config['width'] || 72
+ @append = append || augury_config['append'].to_b || false
+
+ twitter_config = @config.params['twitter']
+ raise Augury::TwitterConfigError unless twitter_config
@twitter = Twitter::REST::Client.new do |config|
- config.consumer_key = @config.params['consumer_key']
- config.consumer_secret = @config.params['consumer_secret']
- config.access_token = @config.params['access_token']
- config.access_token_secret = @config.params['access_token_secret']
+ 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
end
- def tweet_texts(username='SeinfeldToday')
- @twitter.user_timeline(username).flat_map { |tweet| tweet.full_text }
+ def tweet_texts
+ @twitter.user_timeline(@username).flat_map { |tweet| tweet.full_text }
+ end
+
+ def format_fortune(tweets)
+ tweets.flat_map { |tweet| tweet.word_wrap(@width) }.join("%\n")
+ end
+
+ def write_fortune(text)
+ # Write out the file
+ begin
+ mode = @append ? 'a' : 'w'
+ file = File.open(@path, mode)
+ file.write("%\n") if @append
+ file.write(text)
+ rescue IOError => e
+ puts e
+ ensure
+ file.close unless file.nil?
+ end
+ # Create the dat file too
+ `strfile #{@path} #{@path}.dat`
end
end
end