Class: Cinch::Plugins::Twitter

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/twitter.rb

Constant Summary collapse

TWITTER_URL_BASE =
'https://twitter.com/'
ERROR_TPL =
Cinch::Formatting.format(:bold, 'Uhoh! ยป') + ' %s'

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Twitter

Returns a new instance of Twitter.



20
21
22
23
24
25
26
27
28
# File 'lib/cinch/plugins/twitter.rb', line 20

def initialize *args
  super
  ::Twitter.configure do |c|
    c.consumer_key = config[:access_keys][:consumer_key]
    c.consumer_secret = config[:access_keys][:consumer_secret]
    c.oauth_token = config[:access_keys][:oauth_token]
    c.oauth_token_secret = config[:access_keys][:oauth_token_secret]
  end
end

Instance Method Details

#execute_id(m, id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cinch/plugins/twitter.rb', line 59

def execute_id m, id
  tweet = ::Twitter.status(id)
  
  #return m.reply ERROR_TPL % "#{user.screen_name}'s tweets are protected." if user.protected?
  
  m.reply format_tweet(tweet)
rescue ::Twitter::Error::NotFound => e
  m.reply ERROR_TPL % "#{id} doesn't exist."
rescue ::Twitter::Error => e
  m.reply ERROR_TPL % "#{e.message.gsub(/user/i, id)}. (#{e.class})" 
end

#execute_tweet(m, username, offset) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cinch/plugins/twitter.rb', line 32

def execute_tweet m, username, offset
  offset ||= 0
  
  user = ::Twitter.user(username)
  
  return m.reply ERROR_TPL % "#{user.screen_name}'s tweets are protected." if user.protected?
  return m.reply "#{user.screen_name} is lame because they haven't tweeted yet!" if user.status.nil?
  
  # Getting the user's 20 latest tweets if our offset is greater than 0:
  if offset.to_i > 0
    tweets = ::Twitter.user_timeline(user)
    return m.reply ERROR_TPL % "You cannot backtrack more than #{tweets.count.pred} tweets before the current tweet." if offset.to_i > tweets.count.pred
    tweet = tweets[offset.to_i]
  # Otherwise, just get the latest tweet from the user object.
  else
    tweet = user.status
  end
  m.reply format_tweet(tweet)
rescue ::Twitter::Error::NotFound => e
  m.reply ERROR_TPL % "#{username} doesn't exist."
rescue ::Twitter::Error => e
  m.reply ERROR_TPL % "#{e.message.gsub(/user/i, username)}. (#{e.class})"
end