lib/t/list.rb in t-0.9.7 vs lib/t/list.rb in t-0.9.8

- old
+ new

@@ -1,20 +1,13 @@ -require 'active_support/core_ext/array/grouping' -require 't/format_helpers' -require 'csv' -# 'fastercsv' required on Ruby versions < 1.9 -require 'fastercsv' unless Array.new.respond_to?(:to_csv) -require 'retryable' -require 't/collectable' -require 't/core_ext/enumerable' -require 't/core_ext/string' -require 't/printable' -require 't/rcfile' -require 't/requestable' require 'thor' module T + autoload :Collectable, 't/collectable' + autoload :FormatHelpers, 't/format_helpers' + autoload :Printable, 't/printable' + autoload :RCFile, 't/rcfile' + autoload :Requestable, 't/requestable' class List < Thor include T::Collectable include T::Printable include T::Requestable include T::FormatHelpers @@ -33,15 +26,20 @@ desc "add LIST USER [USER...]", "Add members to a list." method_option "id", :aliases => "-i", :type => "boolean", :default => false, :desc => "Specify input as Twitter user IDs instead of screen names." def add(list, user, *users) users.unshift(user) + require 't/core_ext/string' if options['id'] users.map!(&:to_i) else users.map!(&:strip_ats) end + require 'active_support/core_ext/array/grouping' + require 't/core_ext/enumerable' + require 'retryable' + require 'twitter' 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 @@ -55,12 +53,12 @@ end end desc "create LIST [DESCRIPTION]", "Create a new list." method_option "private", :aliases => "-p", :type => :boolean - def create(list, description="") - opts = description.blank? ? {} : {:description => description} + def create(list, description=nil) + opts = description ? {:description => description} : {} opts.merge!(:mode => 'private') if options['private'] client.list_create(list, opts) say "@#{@rcfile.active_profile[0]} created the list \"#{list}\"." end @@ -70,18 +68,21 @@ owner, list = list.split('/') if list.nil? list = owner owner = @rcfile.active_profile[0] else + require 't/core_ext/string' owner = if options['id'] owner.to_i else owner.strip_ats end end list = client.list(owner, list) if options['csv'] + require 'csv' + require 'fastercsv' unless Array.new.respond_to?(:to_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, csv_formatted_time(list), 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] @@ -115,10 +116,11 @@ owner, list = list.split('/') if list.nil? list = owner owner = @rcfile.active_profile[0] else + require 't/core_ext/string' owner = if options['id'] owner.to_i else owner.strip_ats end @@ -131,15 +133,20 @@ desc "remove LIST USER [USER...]", "Remove members from a list." method_option "id", :aliases => "-i", :type => "boolean", :default => false, :desc => "Specify input as Twitter user IDs instead of screen names." def remove(list, user, *users) users.unshift(user) + require 't/core_ext/string' if options['id'] users.map!(&:to_i) else users.map!(&:strip_ats) end + require 'active_support/core_ext/array/grouping' + require 't/core_ext/enumerable' + require 'retryable' + require 'twitter' 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 @@ -163,9 +170,10 @@ owner, list = list.split('/') if list.nil? list = owner owner = @rcfile.active_profile[0] else + require 't/core_ext/string' owner = if options['id'] owner.to_i else owner.strip_ats end