Class: T::CLI

Inherits:
Thor
  • Object
show all
Extended by:
Forwardable
Includes:
Collectable, Printable, Requestable, Utils
Defined in:
lib/t/cli.rb

Constant Summary collapse

DEFAULT_NUM_RESULTS =
20
DIRECT_MESSAGE_HEADINGS =
["ID", "Posted at", "Screen name", "Text"].freeze
MAX_SEARCH_RESULTS =
100
TREND_HEADINGS =
["WOEID", "Parent ID", "Type", "Name", "Country"].freeze

Constants included from Printable

Printable::LIST_HEADINGS, Printable::MONTH_IN_SECONDS, Printable::TWEET_HEADINGS, Printable::USER_HEADINGS

Constants included from Collectable

T::Collectable::MAX_NUM_RESULTS, T::Collectable::MAX_PAGE

Instance Method Summary collapse

Methods included from Collectable

#collect_with_count, #collect_with_max_id, #collect_with_page

Constructor Details

#initializeCLI

Returns a new instance of CLI.



37
38
39
40
# File 'lib/t/cli.rb', line 37

def initialize(*)
  @rcfile = T::RCFile.instance
  super
end

Instance Method Details

#accountsObject



43
44
45
46
47
48
49
50
51
# File 'lib/t/cli.rb', line 43

def accounts
  @rcfile.path = options["profile"] if options["profile"]
  @rcfile.profiles.each do |profile|
    say profile[0]
    profile[1].each_key do |key|
      say "  #{key}#{@rcfile.active_profile[0] == profile[0] && @rcfile.active_profile[1] == key ? ' (active)' : nil}"
    end
  end
end

#authorizeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/t/cli.rb', line 55

def authorize
  @rcfile.path = options["profile"] if options["profile"]
  if @rcfile.empty?
    say "Welcome! Before you can use t, you'll first need to register an"
    say "application with Twitter. Just follow the steps below:"
    say "  1. Sign in to the Twitter Application Management site and click"
    say '     "Create New App".'
    say "  2. Complete the required fields and submit the form."
    say "     Note: Your application must have a unique name."
    say "  3. Go to the Permissions tab of your application, and change the"
    say '     Access setting to "Read, Write and Access direct messages".'
    say "  4. Go to the Keys and Access Tokens tab to view the consumer key"
    say "     and secret which you'll need to copy and paste below when"
    say "     prompted."
  else
    say "It looks like you've already registered an application with Twitter."
    say "To authorize a new account, just follow the steps below:"
    say "  1. Sign in to the Twitter Developer site."
    say "  2. Select the application for which you'd like to authorize an account."
    say "  3. Copy and paste the consumer key and secret below when prompted."
  end
  say
  ask "Press [Enter] to open the Twitter Developer site."
  say
  require "launchy"
  open_or_print("https://apps.twitter.com", dry_run: options["display-uri"])
  key = ask "Enter your API key:"
  secret = ask "Enter your API secret:"
  consumer = OAuth::Consumer.new(key, secret, site: Twitter::REST::Request::BASE_URL)
  request_token = consumer.get_request_token
  uri = generate_authorize_uri(consumer, request_token)
  say
  say "In a moment, you will be directed to the Twitter app authorization page."
  say "Perform the following steps to complete the authorization process:"
  say "  1. Sign in to Twitter."
  say '  2. Press "Authorize app".'
  say "  3. Copy and paste the supplied PIN below when prompted."
  say
  ask "Press [Enter] to open the Twitter app authorization page."
  say
  open_or_print(uri, dry_run: options["display-uri"])
  pin = ask "Enter the supplied PIN:"
  access_token = request_token.get_access_token(oauth_verifier: pin.chomp)
  oauth_response = access_token.get("/1.1/account/verify_credentials.json?skip_status=true")
  screen_name = oauth_response.body.match(/"screen_name"\s*:\s*"(.*?)"/).captures.first
  @rcfile[screen_name] = {
    key => {
      "username" => screen_name,
      "consumer_key" => key,
      "consumer_secret" => secret,
      "token" => access_token.token,
      "secret" => access_token.secret,
    },
  }
  @rcfile.active_profile = {"username" => screen_name, "consumer_key" => key}
  say "Authorization successful."
end

#block(user, *users) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/t/cli.rb', line 115

def block(user, *users)
  blocked_users, number = fetch_users(users.unshift(user), options) do |users_to_block|
    client.block(users_to_block)
  end
  say "@#{@rcfile.active_profile[0]} blocked #{pluralize(number, 'user')}."
  say
  say "Run `#{File.basename($PROGRAM_NAME)} delete block #{blocked_users.collect { |blocked_user| "@#{blocked_user.screen_name}" }.join(' ')}` to unblock."
end

#direct_messagesObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/t/cli.rb', line 131

def direct_messages
  count = options["number"] || DEFAULT_NUM_RESULTS
  opts = {}
  opts[:include_entities] = !!options["decode_uris"]
  direct_messages = collect_with_count(count) do |count_opts|
    client.direct_messages_received(count_opts.merge(opts))
  end
  users = direct_messages.empty? ? [] : client.users(direct_messages.map(&:sender_id))
  users_hash = users.each_with_object({}) { |user, hash| hash[user.id] = user }

  direct_messages.reverse! if options["reverse"]
  if options["csv"]
    require "csv"
    say DIRECT_MESSAGE_HEADINGS.to_csv unless direct_messages.empty?
    direct_messages.each do |direct_message|
      sender = users_hash[direct_message.sender_id] || Twitter::NullObject.new
      say [direct_message.id, csv_formatted_time(direct_message), sender.screen_name, decode_full_text(direct_message, options["decode_uris"])].to_csv
    end
  elsif options["long"]
    array = direct_messages.collect do |direct_message|
      sender = users_hash[direct_message.sender_id] || Twitter::NullObject.new
      [direct_message.id, ls_formatted_time(direct_message), "@#{sender.screen_name}", decode_full_text(direct_message, options["decode_uris"]).gsub(/\n+/, " ")]
    end
    format = options["format"] || Array.new(DIRECT_MESSAGE_HEADINGS.size) { "%s" }
    print_table_with_headings(array, DIRECT_MESSAGE_HEADINGS, format)
  else
    direct_messages.each do |direct_message|
      sender = users_hash[direct_message.sender_id] || Twitter::NullObject.new
      print_message(sender.screen_name, direct_message.text)
    end
  end
end

#direct_messages_sentObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/t/cli.rb', line 172

def direct_messages_sent
  count = options["number"] || DEFAULT_NUM_RESULTS
  opts = {}
  opts[:include_entities] = !!options["decode_uris"]
  direct_messages = collect_with_count(count) do |count_opts|
    client.direct_messages_sent(count_opts.merge(opts))
  end
  direct_messages.reverse! if options["reverse"]
  if options["csv"]
    require "csv"
    say DIRECT_MESSAGE_HEADINGS.to_csv unless direct_messages.empty?
    direct_messages.each do |direct_message|
      say [direct_message.id, csv_formatted_time(direct_message), direct_message.recipient.screen_name, decode_full_text(direct_message, options["decode_uris"])].to_csv
    end
  elsif options["long"]
    array = direct_messages.collect do |direct_message|
      [direct_message.id, ls_formatted_time(direct_message), "@#{direct_message.recipient.screen_name}", decode_full_text(direct_message, options["decode_uris"]).gsub(/\n+/, " ")]
    end
    format = options["format"] || Array.new(DIRECT_MESSAGE_HEADINGS.size) { "%s" }
    print_table_with_headings(array, DIRECT_MESSAGE_HEADINGS, format)
  else
    direct_messages.each do |direct_message|
      print_message(direct_message.recipient.screen_name, direct_message.text)
    end
  end
end

#dm(user, message) ⇒ Object



202
203
204
205
206
207
# File 'lib/t/cli.rb', line 202

def dm(user, message)
  require "t/core_ext/string"
  recipient = options["id"] ? client.user(user.to_i) : client.user(user.strip_ats)
  client.create_direct_message_event(recipient, message)
  say "Direct Message sent from @#{@rcfile.active_profile[0]} to @#{recipient.screen_name}."
end

#does_contain(user_list, user = nil) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/t/cli.rb', line 212

def does_contain(user_list, user = nil)
  owner, list_name = extract_owner(user_list, options)
  user = if user.nil?
    @rcfile.active_profile[0]
  else
    require "t/core_ext/string"
    options["id"] ? client.user(user.to_i).screen_name : user.strip_ats
  end
  if client.list_member?(owner, list_name, user)
    say "Yes, #{list_name} contains @#{user}."
  else
    abort "No, #{list_name} does not contain @#{user}."
  end
end

#does_follow(user1, user2 = nil) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/t/cli.rb', line 230

def does_follow(user1, user2 = nil)
  abort "No, you are not following yourself." if user2.nil? && @rcfile.active_profile[0].casecmp(user1).zero?
  abort "No, @#{user1} is not following themself." if user1 == user2
  require "t/core_ext/string"
  thread1 = if options["id"]
    Thread.new { client.user(user1.to_i).screen_name }
  else
    Thread.new { user1.strip_ats }
  end
  thread2 = if user2.nil?
    Thread.new { @rcfile.active_profile[0] }
  elsif options["id"]
    Thread.new { client.user(user2.to_i).screen_name }
  else
    Thread.new { user2.strip_ats }
  end
  user1 = thread1.value
  user2 = thread2.value
  if client.friendship?(user1, user2)
    say "Yes, @#{user1} follows @#{user2}."
  else
    abort "No, @#{user1} does not follow @#{user2}."
  end
end

#favorite(status_id, *status_ids) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/t/cli.rb', line 257

def favorite(status_id, *status_ids)
  status_ids.unshift(status_id)
  status_ids.collect!(&:to_i)
  require "retryable"
  favorites = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.favorite(status_ids)
  end
  number = favorites.length
  say "@#{@rcfile.active_profile[0]} favorited #{pluralize(number, 'tweet')}."
  say
  say "Run `#{File.basename($PROGRAM_NAME)} delete favorite #{status_ids.join(' ')}` to unfavorite."
end

#favorites(user = nil) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/t/cli.rb', line 281

def favorites(user = nil)
  count = options["number"] || DEFAULT_NUM_RESULTS
  opts = {}
  opts[:exclude_replies] = true if options["exclude"] == "replies"
  opts[:include_entities] = !!options["decode_uris"]
  opts[:include_rts] = false if options["exclude"] == "retweets"
  opts[:max_id] = options["max_id"] if options["max_id"]
  opts[:since_id] = options["since_id"] if options["since_id"]
  if user
    require "t/core_ext/string"
    user = options["id"] ? user.to_i : user.strip_ats
  end
  tweets = collect_with_count(count) do |count_opts|
    client.favorites(user, count_opts.merge(opts))
  end
  print_tweets(tweets)
end

#follow(user, *users) ⇒ Object



302
303
304
305
306
307
308
309
# File 'lib/t/cli.rb', line 302

def follow(user, *users)
  followed_users, number = fetch_users(users.unshift(user), options) do |users_to_follow|
    client.follow(users_to_follow)
  end
  say "@#{@rcfile.active_profile[0]} is now following #{pluralize(number, 'more user')}."
  say
  say "Run `#{File.basename($PROGRAM_NAME)} unfollow #{followed_users.collect { |followed_user| "@#{followed_user.screen_name}" }.join(' ')}` to stop."
end

#followers(user = nil) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/t/cli.rb', line 367

def followers(user = nil)
  if user
    require "t/core_ext/string"
    user = options["id"] ? user.to_i : user.strip_ats
  end
  follower_ids = client.follower_ids(user).to_a
  require "retryable"
  users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(follower_ids)
  end
  print_users(users)
end

#followings(user = nil) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/t/cli.rb', line 319

def followings(user = nil)
  if user
    require "t/core_ext/string"
    user = options["id"] ? user.to_i : user.strip_ats
  end
  following_ids = client.friend_ids(user).to_a
  require "retryable"
  users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(following_ids)
  end
  print_users(users)
end

#followings_following(user1, user2 = nil) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/t/cli.rb', line 340

def followings_following(user1, user2 = nil)
  require "t/core_ext/string"
  user1 = options["id"] ? user1.to_i : user1.strip_ats
  user2 = if user2.nil?
    @rcfile.active_profile[0]
  else
    options["id"] ? user2.to_i : user2.strip_ats
  end
  follower_ids = Thread.new { client.follower_ids(user1).to_a }
  following_ids = Thread.new { client.friend_ids(user2).to_a }
  followings_following_ids = follower_ids.value & following_ids.value
  require "retryable"
  users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(followings_following_ids)
  end
  print_users(users)
end

#friends(user = nil) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/t/cli.rb', line 388

def friends(user = nil)
  user = if user
    require "t/core_ext/string"
    options["id"] ? user.to_i : user.strip_ats
  else
    client.verify_credentials.screen_name
  end
  following_ids = Thread.new { client.friend_ids(user).to_a }
  follower_ids = Thread.new { client.follower_ids(user).to_a }
  friend_ids = following_ids.value & follower_ids.value
  require "retryable"
  users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(friend_ids)
  end
  print_users(users)
end

#groupies(user = nil) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/t/cli.rb', line 413

def groupies(user = nil)
  user = if user
    require "t/core_ext/string"
    options["id"] ? user.to_i : user.strip_ats
  else
    client.verify_credentials.screen_name
  end
  follower_ids = Thread.new { client.follower_ids(user).to_a }
  following_ids = Thread.new { client.friend_ids(user).to_a }
  groupie_ids = (follower_ids.value - following_ids.value)
  require "retryable"
  users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(groupie_ids)
  end
  print_users(users)
end

#intersection(first_user, *users) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/t/cli.rb', line 440

def intersection(first_user, *users)
  users.push(first_user)
  # If only one user is specified, compare to the authenticated user
  users.push(@rcfile.active_profile[0]) if users.size == 1
  require "t/core_ext/string"
  options["id"] ? users.collect!(&:to_i) : users.collect!(&:strip_ats)
  sets = parallel_map(users) do |user|
    case options["type"]
    when "followings"
      client.friend_ids(user).to_a
    when "followers"
      client.follower_ids(user).to_a
    end
  end
  intersection = sets.reduce(:&)
  require "retryable"
  users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(intersection)
  end
  print_users(users)
end

#leaders(user = nil) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/t/cli.rb', line 471

def leaders(user = nil)
  user = if user
    require "t/core_ext/string"
    options["id"] ? user.to_i : user.strip_ats
  else
    client.verify_credentials.screen_name
  end
  following_ids = Thread.new { client.friend_ids(user).to_a }
  follower_ids = Thread.new { client.follower_ids(user).to_a }
  leader_ids = (following_ids.value - follower_ids.value)
  require "retryable"
  users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(leader_ids)
  end
  print_users(users)
end

#lists(user = nil) ⇒ Object



496
497
498
499
500
501
502
503
504
505
# File 'lib/t/cli.rb', line 496

def lists(user = nil)
  lists = if user
    require "t/core_ext/string"
    user = options["id"] ? user.to_i : user.strip_ats
    client.lists(user)
  else
    client.lists
  end
  print_lists(lists)
end

#matrixObject



508
509
510
511
512
513
514
# File 'lib/t/cli.rb', line 508

def matrix
  opts = {count: MAX_SEARCH_RESULTS, include_entities: false}
  tweets = client.search("lang:ja", opts)
  tweets.each do |tweet|
    say(tweet.text.gsub(/[^\u3000\u3040-\u309f]/, "").reverse, %i[bold green on_black], false)
  end
end

#mentionsObject



523
524
525
526
527
528
529
530
531
# File 'lib/t/cli.rb', line 523

def mentions
  count = options["number"] || DEFAULT_NUM_RESULTS
  opts = {}
  opts[:include_entities] = !!options["decode_uris"]
  tweets = collect_with_count(count) do |count_opts|
    client.mentions(count_opts.merge(opts))
  end
  print_tweets(tweets)
end

#mute(user, *users) ⇒ Object



536
537
538
539
540
541
542
543
# File 'lib/t/cli.rb', line 536

def mute(user, *users)
  muted_users, number = fetch_users(users.unshift(user), options) do |users_to_mute|
    client.mute(users_to_mute)
  end
  say "@#{@rcfile.active_profile[0]} muted #{pluralize(number, 'user')}."
  say
  say "Run `#{File.basename($PROGRAM_NAME)} delete mute #{muted_users.collect { |muted_user| "@#{muted_user.screen_name}" }.join(' ')}` to unmute."
end

#mutedObject



552
553
554
555
556
557
558
559
# File 'lib/t/cli.rb', line 552

def muted
  muted_ids = client.muted_ids.to_a
  require "retryable"
  muted_users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(muted_ids)
  end
  print_users(muted_users)
end

#open(user) ⇒ Object



566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/t/cli.rb', line 566

def open(user)
  require "launchy"
  if options["id"]
    user = client.user(user.to_i)
    open_or_print(user.uri, dry_run: options["display-uri"])
  elsif options["status"]
    status = client.status(user.to_i, include_my_retweet: false)
    open_or_print(status.uri, dry_run: options["display-uri"])
  else
    require "t/core_ext/string"
    open_or_print("https://twitter.com/#{user.strip_ats}", dry_run: options["display-uri"])
  end
end

#reach(tweet_id) ⇒ Object



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/t/cli.rb', line 581

def reach(tweet_id)
  require "t/core_ext/string"
  require "set"
  status_thread = Thread.new { client.status(tweet_id.to_i, include_my_retweet: false) }
  threads = []
  client.retweeters_ids(tweet_id.to_i).each do |retweeter_id|
    threads << Thread.new(retweeter_id) do |user_id|
      client.follower_ids(user_id).to_a
    end
  end
  status = status_thread.value
  threads << Thread.new(status.user.id) do |user_id|
    client.follower_ids(user_id).to_a
  end
  reach = ::Set.new
  threads.each { |thread| reach += thread.value }
  reach.delete(status.user.id)
  say number_with_delimiter(reach.size)
end

#reply(status_id, message = nil) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/t/cli.rb', line 605

def reply(status_id, message = nil)
  message = T::Editor.gets if message.to_s.empty?
  status = client.status(status_id.to_i, include_my_retweet: false)
  users = Array(status.user.screen_name)
  if options["all"]
    users += extract_mentioned_screen_names(status.full_text)
    users.uniq!
  end
  users.delete(@rcfile.active_profile[0])
  require "t/core_ext/string"
  users.collect!(&:prepend_at)
  opts = {in_reply_to_status_id: status.id, trim_user: true}
  add_location!(options, opts)
  reply = if options["file"]
    client.update_with_media("#{users.join(' ')} #{message}", File.new(File.expand_path(options["file"])), opts)
  else
    client.update("#{users.join(' ')} #{message}", opts)
  end
  say "Reply posted by @#{@rcfile.active_profile[0]} to #{users.join(' ')}."
  say
  say "Run `#{File.basename($PROGRAM_NAME)} delete status #{reply.id}` to delete."
end

#report_spam(user, *users) ⇒ Object



630
631
632
633
634
635
# File 'lib/t/cli.rb', line 630

def report_spam(user, *users)
  _, number = fetch_users(users.unshift(user), options) do |users_to_report|
    client.report_spam(users_to_report)
  end
  say "@#{@rcfile.active_profile[0]} reported #{pluralize(number, 'user')}."
end

#retweet(status_id, *status_ids) ⇒ Object



639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/t/cli.rb', line 639

def retweet(status_id, *status_ids)
  status_ids.unshift(status_id)
  status_ids.collect!(&:to_i)
  require "retryable"
  retweets = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.retweet(status_ids, trim_user: true)
  end
  number = retweets.length
  say "@#{@rcfile.active_profile[0]} retweeted #{pluralize(number, 'tweet')}."
  say
  say "Run `#{File.basename($PROGRAM_NAME)} delete status #{retweets.collect(&:id).join(' ')}` to undo."
end

#retweets(user = nil) ⇒ Object



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/t/cli.rb', line 661

def retweets(user = nil)
  count = options["number"] || DEFAULT_NUM_RESULTS
  opts = {}
  opts[:include_entities] = !!options["decode_uris"]
  tweets = if user
    require "t/core_ext/string"
    user = options["id"] ? user.to_i : user.strip_ats
    collect_with_count(count) do |count_opts|
      client.retweeted_by_user(user, count_opts.merge(opts))
    end
  else
    collect_with_count(count) do |count_opts|
      client.retweeted_by_me(count_opts.merge(opts))
    end
  end
  print_tweets(tweets)
end

#retweets_of_meObject



687
688
689
690
691
692
693
694
695
# File 'lib/t/cli.rb', line 687

def retweets_of_me
  count = options["number"] || DEFAULT_NUM_RESULTS
  opts = {}
  opts[:include_entities] = !!options["decode_uris"]
  tweets = collect_with_count(count) do |count_opts|
    client.retweets_of_me(count_opts.merge(opts))
  end
  print_tweets(tweets)
end

#rulerObject



700
701
702
703
# File 'lib/t/cli.rb', line 700

def ruler
  markings = "----|".chars.cycle.take(140).join
  say "#{' ' * options['indent'].to_i}#{markings}"
end

#status(status_id) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/t/cli.rb', line 710

def status(status_id) # rubocop:disable Metrics/CyclomaticComplexity
  opts = {include_my_retweet: false}
  opts[:include_entities] = !!options["decode_uris"]
  status = client.status(status_id.to_i, opts)
  location = if status.place?
    if status.place.name? && status.place.attributes? && status.place.attributes[:street_address] && status.place.attributes[:locality] && status.place.attributes[:region] && status.place.country?
      [status.place.name, status.place.attributes[:street_address], status.place.attributes[:locality], status.place.attributes[:region], status.place.country].join(", ")
    elsif status.place.name? && status.place.attributes? && status.place.attributes[:locality] && status.place.attributes[:region] && status.place.country?
      [status.place.name, status.place.attributes[:locality], status.place.attributes[:region], status.place.country].join(", ")
    elsif status.place.full_name? && status.place.attributes? && status.place.attributes[:region] && status.place.country?
      [status.place.full_name, status.place.attributes[:region], status.place.country].join(", ")
    elsif status.place.full_name? && status.place.country?
      [status.place.full_name, status.place.country].join(", ")
    elsif status.place.full_name?
      status.place.full_name
    else
      status.place.name
    end
  elsif status.geo?
    reverse_geocode(status.geo)
  end
  status_headings = ["ID", "Posted at", "Screen name", "Text", "Retweets", "Favorites", "Source", "Location"]
  if options["csv"]
    require "csv"
    say status_headings.to_csv
    say [status.id, csv_formatted_time(status), status.user.screen_name, decode_full_text(status, options["decode_uris"]), status.retweet_count, status.favorite_count, strip_tags(status.source), location].to_csv
  elsif options["long"]
    array = [status.id, ls_formatted_time(status), "@#{status.user.screen_name}", decode_full_text(status, options["decode_uris"]).gsub(/\n+/, " "), status.retweet_count, status.favorite_count, strip_tags(status.source), location]
    format = options["format"] || Array.new(status_headings.size) { "%s" }
    print_table_with_headings([array], status_headings, format)
  else
    array = []
    array << ["ID", status.id.to_s]
    array << ["Text", decode_full_text(status, options["decode_uris"]).gsub(/\n+/, " ")]
    array << ["Screen name", "@#{status.user.screen_name}"]
    array << ["Posted at", "#{ls_formatted_time(status, :created_at, false)} (#{time_ago_in_words(status.created_at)} ago)"]
    array << ["Retweets", number_with_delimiter(status.retweet_count)]
    array << ["Favorites", number_with_delimiter(status.favorite_count)]
    array << ["Source", strip_tags(status.source)]
    array << ["Location", location] unless location.nil?
    print_table(array)
  end
end

#timeline(user = nil) ⇒ Object



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/t/cli.rb', line 765

def timeline(user = nil)
  count = options["number"] || DEFAULT_NUM_RESULTS
  opts = {}
  opts[:exclude_replies] = true if options["exclude"] == "replies"
  opts[:include_entities] = !!options["decode_uris"]
  opts[:include_rts] = false if options["exclude"] == "retweets"
  opts[:max_id] = options["max_id"] if options["max_id"]
  opts[:since_id] = options["since_id"] if options["since_id"]
  if user
    require "t/core_ext/string"
    user = options["id"] ? user.to_i : user.strip_ats
    tweets = collect_with_count(count) do |count_opts|
      client.user_timeline(user, count_opts.merge(opts))
    end
  else
    tweets = collect_with_count(count) do |count_opts|
      client.home_timeline(count_opts.merge(opts))
    end
  end
  print_tweets(tweets)
end

#trend_locationsObject



804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/t/cli.rb', line 804

def trend_locations
  places = client.trend_locations
  unless options["unsorted"]
    places = case options["sort"]
    when "country"
      places.sort_by { |place| place.country.downcase }
    when "parent"
      places.sort_by { |place| place.parent_id.to_i }
    when "type"
      places.sort_by { |place| place.place_type.downcase }
    when "woeid"
      places.sort_by { |place| place.woeid.to_i }
    else
      places.sort_by { |place| place.name.downcase }
    end
  end
  places.reverse! if options["reverse"]
  if options["csv"]
    require "csv"
    say TREND_HEADINGS.to_csv unless places.empty?
    places.each do |place|
      say [place.woeid, place.parent_id, place.place_type, place.name, place.country].to_csv
    end
  elsif options["long"]
    array = places.collect do |place|
      [place.woeid, place.parent_id, place.place_type, place.name, place.country]
    end
    format = options["format"] || Array.new(TREND_HEADINGS.size) { "%s" }
    print_table_with_headings(array, TREND_HEADINGS, format)
  else
    print_attribute(places, :name)
  end
end


790
791
792
793
794
795
# File 'lib/t/cli.rb', line 790

def trends(woe_id = 1)
  opts = {}
  opts[:exclude] = "hashtags" if options["exclude-hashtags"]
  trends = client.trends(woe_id, opts)
  print_attribute(trends, :name)
end

#unfollow(user, *users) ⇒ Object



841
842
843
844
845
846
847
848
# File 'lib/t/cli.rb', line 841

def unfollow(user, *users)
  unfollowed_users, number = fetch_users(users.unshift(user), options) do |users_to_unfollow|
    client.unfollow(users_to_unfollow)
  end
  say "@#{@rcfile.active_profile[0]} is no longer following #{pluralize(number, 'user')}."
  say
  say "Run `#{File.basename($PROGRAM_NAME)} follow #{unfollowed_users.collect { |unfollowed_user| "@#{unfollowed_user.screen_name}" }.join(' ')}` to follow again."
end

#update(message = nil) ⇒ Object



853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'lib/t/cli.rb', line 853

def update(message = nil)
  message = T::Editor.gets if message.to_s.empty?
  opts = {trim_user: true}
  add_location!(options, opts)
  status = if options["file"]
    client.update_with_media(message, File.new(File.expand_path(options["file"])), opts)
  else
    client.update(message, opts)
  end
  say "Tweet posted by @#{@rcfile.active_profile[0]}."
  say
  say "Run `#{File.basename($PROGRAM_NAME)} delete status #{status.id}` to delete."
end

#users(user, *users) ⇒ Object



876
877
878
879
880
881
882
883
884
885
# File 'lib/t/cli.rb', line 876

def users(user, *users)
  users.unshift(user)
  require "t/core_ext/string"
  options["id"] ? users.collect!(&:to_i) : users.collect!(&:strip_ats)
  require "retryable"
  users = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
    client.users(users)
  end
  print_users(users)
end

#versionObject



889
890
891
892
# File 'lib/t/cli.rb', line 889

def version
  require "t/version"
  say T::Version
end

#whoamiObject



934
935
936
937
938
939
940
941
# File 'lib/t/cli.rb', line 934

def whoami
  if @rcfile.active_profile && @rcfile.active_profile[0]
    user = @rcfile.active_profile[0]
    whois(user)
  else
    warn "You haven't authorized an account, run `t authorize` to get started."
  end
end

#whois(user) ⇒ Object



901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# File 'lib/t/cli.rb', line 901

def whois(user)
  require "t/core_ext/string"
  opts = {}
  opts[:include_entities] = !!options["decode_uris"]
  user = options["id"] ? user.to_i : user.strip_ats
  user = client.user(user, opts)
  if options["csv"] || options["long"]
    print_users([user])
  else
    array = []
    array << ["ID", user.id.to_s]
    array << ["Since", "#{ls_formatted_time(user, :created_at, false)} (#{time_ago_in_words(user.created_at)} ago)"]
    array << ["Last update", "#{decode_full_text(user.status, options['decode_uris']).gsub(/\n+/, ' ')} (#{time_ago_in_words(user.status.created_at)} ago)"] unless user.status.nil?
    array << ["Screen name", "@#{user.screen_name}"]
    array << [user.verified? ? "Name (Verified)" : "Name", user.name] unless user.name.nil? # rubocop:disable Metrics/BlockNesting
    array << ["Tweets", number_with_delimiter(user.statuses_count)]
    array << ["Favorites", number_with_delimiter(user.favorites_count)]
    array << ["Listed", number_with_delimiter(user.listed_count)]
    array << ["Following", number_with_delimiter(user.friends_count)]
    array << ["Followers", number_with_delimiter(user.followers_count)]
    array << ["Bio", user.description.gsub(/\n+/, " ")] unless user.description.nil?
    array << ["Location", user.location] unless user.location.nil?
    array << ["URL", user.website] unless user.website.nil?
    print_table(array)
  end
end