class Myun2::TwitterShell::Ls

Attributes

client[R]

Public Class Methods

new(client, *params) click to toggle source
# File lib/myun2/twitter_shell/ls.rb, line 8
def initialize(client, *params)
  @client = client

  if params && params.length > 0
    case
    when params[0] == '-m' || params[0] == 'mention' || params[0] == 'mentions'
      params.shift
      timeline = mentions_timeline(*params)
    when params[0][0] != '-'
      user_name = params.shift
      timeline = user_timeline(user_name, *params)
    end
  end
  timeline ||= home_timeline(*params)

  timeline.each do |t|
    puts format(t, *params)
  end
end

Public Instance Methods

format(tweet, *params) click to toggle source
# File lib/myun2/twitter_shell/ls.rb, line 40
def format(tweet, *params)
  u = tweet['user']
  user_name = u['name']
  screen_name = u['screen_name']
  text = tweet['text']

  if params && params.length > 0 && params[0][0] == '-' && params[0].include?('l')
    datetime = tweet['created_at']
    "#{datetime} \e[38;5;#{u['id'] % 255}m<#{screen_name}:#{user_name}>\e[0m: #{text}"
  else
    "\e[38;5;#{u['id'] % 255}m<#{user_name}>\e[0m: #{text[0..30]}"
  end
end
home_timeline(*params) click to toggle source
# File lib/myun2/twitter_shell/ls.rb, line 36
def home_timeline(*params)
  client.home_timeline(params_to_options *params)
end
mentions_timeline(*params) click to toggle source
# File lib/myun2/twitter_shell/ls.rb, line 32
def mentions_timeline(*params)
  client.mentions(params_to_options *params)
end
user_timeline(screen_name, *params) click to toggle source
# File lib/myun2/twitter_shell/ls.rb, line 28
def user_timeline(screen_name, *params)
  client.user_timeline({ screen_name: screen_name }.merge(params_to_options *params))
end

Private Instance Methods

params_to_options(*params) click to toggle source
# File lib/myun2/twitter_shell/ls.rb, line 55
def params_to_options(*params)
  options = {}
  params.each do |param|
    options[:count] = $1.to_i if /\A-c=(\d+)\Z/ =~ param
  end
  options
end