Class: T::List

Inherits:
Thor
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, Collectable, Printable, Requestable
Defined in:
lib/t/list.rb

Constant Summary collapse

DEFAULT_NUM_RESULTS =
20
MAX_SCREEN_NAME_SIZE =
20
MAX_USERS_PER_LIST =
500
MAX_USERS_PER_REQUEST =
100

Constants included from Requestable

Requestable::DEFAULT_HOST, Requestable::DEFAULT_PROTOCOL

Instance Method Summary collapse

Methods included from Requestable

#base_url, #client, #host, included, #protocol

Methods included from Printable

#build_long_list, #build_long_status, #build_long_user, included, #print_csv_list, #print_csv_status, #print_csv_user, #print_in_columns, #print_lists, #print_status, #print_statuses, #print_users

Methods included from Collectable

#collect_with_cursor, #collect_with_max_id

Constructor Details

#initializeList

Returns a new instance of List.



29
30
31
32
# File 'lib/t/list.rb', line 29

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

Instance Method Details

#add(list, user, *users) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/t/list.rb', line 36

def add(list, user, *users)
  users.unshift(user)
  if options['id']
    users.map!(&:to_i)
  else
    users.map!(&:strip_ats)
  end
  users.in_groups_of(MAX_USERS_PER_REQUEST, false).threaded_each do |user_id_group|
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.list_add_members(list, user_id_group)
    end
  end
  number = users.length
  say "@#{@rcfile.active_profile[0]} added #{number} #{number == 1 ? 'member' : 'members'} to the list \"#{list}\"."
  say
  if options['id']
    say "Run `#{File.basename($0)} list remove --id #{list} #{users.join(' ')}` to undo."
  else
    say "Run `#{File.basename($0)} list remove #{list} #{users.map{|user| "@#{user}"}.join(' ')}` to undo."
  end
end

#create(list, description = "") ⇒ Object



60
61
62
63
64
65
# File 'lib/t/list.rb', line 60

def create(list, description="")
  opts = description.blank? ? {} : {:description => description}
  opts.merge!(:mode => 'private') if options['private']
  client.list_create(list, opts)
  say "@#{@rcfile.active_profile[0]} created the list \"#{list}\"."
end

#information(list) ⇒ Object



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
# File 'lib/t/list.rb', line 69

def information(list)
  owner, list = list.split('/')
  if list.nil?
    list = owner
    owner = @rcfile.active_profile[0]
  else
    owner = if options['id']
      owner.to_i
    else
      owner.strip_ats
    end
  end
  list = client.list(owner, list)
  if options['csv']
    say ["ID", "Description", "Slug", "Screen name", "Created at", "Members", "Subscribers", "Following", "Mode", "URL"].to_csv
    say [list.id, list.description, list.slug, list.user.screen_name, list.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), list.member_count, list.subscriber_count, list.following?, list.mode, "https://twitter.com#{list.uri}"].to_csv
  else
    array = []
    array << ["ID", list.id.to_s]
    array << ["Description", list.description] unless list.description.nil?
    array << ["Slug", list.slug]
    array << ["Screen name", "@#{list.user.screen_name}"]
    created_at = list.created_at > 6.months.ago ? list.created_at.strftime("%b %e %H:%M") : list.created_at.strftime("%b %e  %Y")
    array << ["Created at", created_at]
    array << ["Members", number_with_delimiter(list.member_count)]
    array << ["Subscribers", number_with_delimiter(list.subscriber_count)]
    status = list.following ? "Following" : "Not following"
    array << ["Status", status]
    array << ["Mode", list.mode]
    array << ["URL", "https://twitter.com#{list.uri}"]
    print_table(array)
  end
end

#members(list) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/t/list.rb', line 116

def members(list)
  owner, list = list.split('/')
  if list.nil?
    list = owner
    owner = @rcfile.active_profile[0]
  else
    owner = if options['id']
      owner.to_i
    else
      owner.strip_ats
    end
  end
  users = collect_with_cursor do |cursor|
    client.list_members(owner, list, :cursor => cursor, :include_entities => false, :skip_status => true)
  end
  print_users(users)
end

#remove(list, user, *users) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/t/list.rb', line 136

def remove(list, user, *users)
  users.unshift(user)
  if options['id']
    users.map!(&:to_i)
  else
    users.map!(&:strip_ats)
  end
  users.in_groups_of(MAX_USERS_PER_REQUEST, false).threaded_each do |user_id_group|
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.list_remove_members(list, user_id_group)
    end
  end
  number = users.length
  say "@#{@rcfile.active_profile[0]} removed #{number} #{number == 1 ? 'member' : 'members'} from the list \"#{list}\"."
  say
  if options['id']
    say "Run `#{File.basename($0)} list add --id #{list} #{users.join(' ')}` to undo."
  else
    say "Run `#{File.basename($0)} list add #{list} #{users.map{|user| "@#{user}"}.join(' ')}` to undo."
  end
end

#timeline(list) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/t/list.rb', line 164

def timeline(list)
  owner, list = list.split('/')
  if list.nil?
    list = owner
    owner = @rcfile.active_profile[0]
  else
    owner = if options['id']
      owner.to_i
    else
      owner.strip_ats
    end
  end
  per_page = options['number'] || DEFAULT_NUM_RESULTS
  statuses = client.list_timeline(owner, list, :include_entities => false, :per_page => per_page)
  print_statuses(statuses)
end