Class: TweetMsx

Inherits:
Object
  • Object
show all
Defined in:
lib/tweet_msx.rb

Overview

end

Defined Under Namespace

Classes: NotConfigured

Constant Summary collapse

CONFIG_FILENAME =
File.expand_path('~/.openMSX-builder-TweetMSX.yaml')
DEFAULTS =
{
  :client => {
    :consumer_key => '',
    :consumer_secret => '',
    :token => '',
    :secret => '',
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_level = Logger::FATAL) ⇒ TweetMsx

Returns a new instance of TweetMsx.



26
27
28
29
30
# File 'lib/tweet_msx.rb', line 26

def initialize(log_level=Logger::FATAL)
  @log = Logger.new(STDOUT)
  @log.level = log_level
  @client = TwitterOAuth::Client.new(config[:client])
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



25
26
27
# File 'lib/tweet_msx.rb', line 25

def client
  @client
end

#twitter_downObject (readonly)

Returns the value of attribute twitter_down.



25
26
27
# File 'lib/tweet_msx.rb', line 25

def twitter_down
  @twitter_down
end

Instance Method Details

#configObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/tweet_msx.rb', line 32

def config
  create_default_config unless File.exist?(CONFIG_FILENAME)
  @config ||= YAML.load_file(CONFIG_FILENAME)
  @log.debug @config.to_yaml
  if @config == DEFAULTS
    @log.error "TweetMSX config at #{CONFIG_FILENAME} matches DEFAULTS"
    raise NotConfigured.new("You need to set up your config file at #{CONFIG_FILENAME} first")
  end
  @config
end

#create_default_configObject



43
44
45
46
47
48
49
# File 'lib/tweet_msx.rb', line 43

def create_default_config
  system("mkdir -p #{File.dirname(CONFIG_FILENAME)}")
  @log.debug "Creating default config at #{CONFIG_FILENAME}"
  File.open(CONFIG_FILENAME,'w') do |f|
    f.write DEFAULTS.to_yaml
  end
end

#update(message) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tweet_msx.rb', line 51

def update(message)
  @log.info "Tweeting message:\n #{message}"
  @log.debug "[#{message.size} characters]"
  if @client.rate_limit_status == 0
    @log.error "You've exceeded your rate limit"
    return nil
  end
  ret = @client.update(message.to_s)
  @log.info(ret.to_yaml) unless ret.nil?
  nil
rescue SocketError
  @log.error "Could not send '#{message}'. Twitter or your connection might be down."
  nil
end