class Twitter2Jabber::TwitterClient

Constants

BATCH_SIZE

Attributes

client[R]
config[R]
spec[R]

Public Class Methods

new(gw, config) click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 36
def initialize(gw, config)
  @gw, @config, @client = gw, config, Twitter::REST::Client.new(
    consumer_key:        @spec = config[:consumer_token],
    consumer_secret:     config[:consumer_secret],
    access_token:        config[:access_token],
    access_token_secret: config[:access_secret]
  )
end

Public Instance Methods

connect() click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 47
def connect
  client.verify_credentials
  log 'connected'
rescue Twitter::Error => err
  raise "Can't connect to Twitter with ID '#{spec}': #{err}"
end
disconnect() click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 54
def disconnect
  @client = nil
  log 'disconnected'
end
process_html(text) click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 76
def process_html(text)
  text.gsub(/(?:\A|\P{Word})@(\p{Word}+)/,
            '@<a href="https://twitter.com/\1">\1</a>')
      .gsub(/(?:\A|\P{Word})#(\p{Word}+)/,
            '<a href="https://search.twitter.com/search?q=%23\1">#\1</a>')
end
process_message(text) click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 72
def process_message(text)
  text.gsub(%r{https?://\S+}) { |match| LongURL.expand(match) rescue match }
end
process_text(text) click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 83
def process_text(text)
  text
end
tweets(since_id = nil) { |tweet| ... } click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 59
def tweets(since_id = nil)
  get_tweets(since_id).reverse_each { |tweet|
    log since_id = tweet.id
    yield tweet
    sleep 1
  }

  since_id
rescue Twitter::Error, Timeout::Error
rescue => err
  warn "#{err} (#{err.class})"
end

Private Instance Methods

get_tweets(since_id = nil) click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 89
def get_tweets(since_id = nil)
  options, buffer = { count: BATCH_SIZE }, []
  options[:since_id] = since_id if since_id

  loop {
    buffer.concat(batch = client.home_timeline(options))

    break if batch.empty? || !since_id ||
      since_id >= options[:max_id] = batch.last.id - 1
  }

  buffer
end
log(msg) click to toggle source
# File lib/twitter2jabber/twitter_client.rb, line 103
def log(msg)
  @gw.log("TWITTER #{msg}")
end