class Tweep::Config

Constants

DOWS
TIME_REGEX

Attributes

auth[R]
nick[R]
retweeters[R]
schedule[R]
tweets[R]

Public Class Methods

new(file, index) click to toggle source
# File lib/tweep/config.rb, line 12
def initialize(file, index)
  config = YAML::load(File.read(file))

  @nick = File.basename(file, '.*')
  @index = index
  
  load_auth config
  load_schedule config
  load_retweeters config

  @tweets = (config['tweets'] || []).map(&:strip)
end

Private Class Methods

read_hour(hour) click to toggle source
# File lib/tweep/config.rb, line 106
def self.read_hour(hour)
  return unless hour =~ TIME_REGEX
  h, m = if $1 && $2
    h, m = $1.split(':').map(&:to_i)
    h = 0 if 12 == h
    h += 12 if 'P' == $2.upcase[0, 1]
    [h, m]
  else
    $3.split(':').map(&:to_i)
  end
  m ||= 0
  "%02d:%02d" % [h, m]
end

Public Instance Methods

has_auth?() click to toggle source
# File lib/tweep/config.rb, line 25
def has_auth?
  4 == @auth.values.reject(&:blank?).size
end
has_tweets?() click to toggle source
# File lib/tweep/config.rb, line 29
def has_tweets?
  @tweets.any?
end
next_tweet() click to toggle source
# File lib/tweep/config.rb, line 33
def next_tweet
  idx = @index.next_tweet_index(@nick)
  idx = 0 if idx.to_i >= @tweets.size
  [@tweets[idx], idx]
end
now_is_a_good_time?() click to toggle source
# File lib/tweep/config.rb, line 39
def now_is_a_good_time?
  now = Time.now
  (0..@allowed_delay.to_i).any? do |offset|
    time = now - offset * 60
    (@schedule[time.wday] || []).include?(time.strftime('%H:%M'))
  end
end
should_get_retweeted_by?(retweeter) click to toggle source
# File lib/tweep/config.rb, line 47
def should_get_retweeted_by?(retweeter)
  if (result = @index.retweet_timely?(@nick, retweeter))
    @index.next_retweet_in! @nick, retweeter, @retweeters[retweeter]
  else
    @index.retweet_will_wait! @nick, retweeter
  end
  result
end

Private Instance Methods

load_auth(config) click to toggle source
# File lib/tweep/config.rb, line 70
def load_auth(config)
  @auth = config.slice('consumer_key', 'consumer_secret', 'oauth_token', 'oauth_token_secret')
end
load_retweeters(config) click to toggle source
# File lib/tweep/config.rb, line 74
def load_retweeters(config)
  @retweeters = {}
  if (shortcut = config['retweeter'])
    @retweeters[shortcut.to_s] = 0
  end
  (config['retweeters'] || {}).each do |nick, pattern|
    wait = 0
    if pattern[/^\s*every\s+(\d+|other)(?:\s+tweets?)\s*$/i]
      wait = 'other' == $1 ? 1 : [$1.to_i - 1, 0].max
    end
    @retweeters[nick] = wait
  end
end
load_schedule(config) click to toggle source
# File lib/tweep/config.rb, line 88
def load_schedule(config)
  @schedule = (config['schedule'] || {}).inject({}) do |acc, (dow, hours)|
    key = DOWS.index(dow.to_s.downcase)
    if !key && dow.to_s[/^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/]
      key = Date.civil($1.to_i, $2.to_i, $3.to_i) rescue nil
    end
    if !key && 'allowed_delay' == dow
      @allowed_delay = hours.to_i
    elsif key
      hours = hours.split(',').map(&:strip).reject(&:empty?)
      hours = hours.map { |hour| self.class.read_hour(hour) }.compact
      acc[key] = hours if hours
    end
    acc
  end
  
end