Class: T::Stream

Inherits:
Thor
  • Object
show all
Includes:
Printable, Requestable, Utils
Defined in:
lib/t/stream.rb

Constant Summary collapse

TWEET_HEADINGS_FORMATTING =
[
  "%-18s",  # Add padding to maximum length of a Tweet ID
  "%-12s",  # Add padding to length of a timestamp formatted with ls_formatted_time
  "%-20s",  # Add padding to maximum length of a Twitter screen name
  "%s",     # Last element does not need special formatting
].freeze
STREAM_FIELDS =
"tweet.fields=author_id,created_at,entities,text&expansions=author_id&user.fields=username,name".freeze

Constants included from Utils

Utils::DISTANCE_THRESHOLDS

Constants included from RequestableAPI

RequestableAPI::BASE_URL, RequestableAPI::BASE_URL_UPLOAD, RequestableAPI::BASE_URL_V1, RequestableAPI::DEFAULT_NUM_RESULTS, RequestableAPI::FORM_HEADERS, RequestableAPI::JSON_HEADERS, RequestableAPI::MAX_PAGE, RequestableAPI::MAX_SEARCH_RESULTS, RequestableAPI::V2_LIST_FIELDS, RequestableAPI::V2_PLACE_FIELDS, RequestableAPI::V2_TWEET_EXPANSIONS, RequestableAPI::V2_TWEET_FIELDS, RequestableAPI::V2_USER_EXPANSIONS, RequestableAPI::V2_USER_FIELDS

Constants included from Printable

Printable::LISTS_SORT_MAP, Printable::LIST_HEADINGS, Printable::MONTH_IN_SECONDS, Printable::TWEET_HEADINGS, Printable::USERS_SORT_MAP, Printable::USER_HEADINGS

Instance Method Summary collapse

Methods included from RequestableAPI

#setup_requestable_api!

Methods included from RequestableAPI::AccountEndpoints

#x_before_request, #x_filter, #x_sample, #x_settings, #x_trend_locations, #x_trends, #x_update_profile, #x_update_profile_background_image, #x_update_profile_image

Methods included from RequestableAPI::ListEndpoints

#x_add_list_members, #x_create_list, #x_destroy_list, #x_list, #x_list_member?, #x_list_members, #x_list_timeline, #x_lists, #x_remove_list_members

Methods included from RequestableAPI::DMEndpoints

#x_create_direct_message_event, #x_destroy_direct_message, #x_direct_message, #x_direct_messages_received, #x_direct_messages_sent

Methods included from RequestableAPI::Mutations

#x_block, #x_destroy_status, #x_favorite, #x_follow, #x_mute, #x_muted_ids, #x_report_spam, #x_retweet, #x_unblock, #x_unfavorite, #x_unfollow, #x_unmute

Methods included from RequestableAPI::TweetEndpoints

#x_favorites, #x_home_timeline, #x_mentions, #x_retweeted_by_me, #x_retweeted_by_user, #x_retweeters_ids, #x_retweets_of_me, #x_search, #x_status, #x_update, #x_update_with_media, #x_user_timeline

Methods included from RequestableAPI::UserEndpoints

#x_follower_ids, #x_friend_ids, #x_friendship?, #x_user, #x_user_search, #x_users, #x_verify_credentials

Constructor Details

#initializeStream

Returns a new instance of Stream.



24
25
26
27
# File 'lib/t/stream.rb', line 24

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

Instance Method Details

#allObject



33
34
35
36
# File 'lib/t/stream.rb', line 33

def all
  print_stream_headings
  stream_tweets("tweets/sample/stream") { |tweet| print_stream_tweet(tweet) }
end

#list(user_list) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/t/stream.rb', line 44

def list(user_list)
  owner, list_name = extract_owner(user_list, options)
  require "t/list"
  list_obj = T::List.new
  list_obj.options = list_obj.options.merge(options)
  list_obj.options = list_obj.options.merge(reverse: true)
  list_obj.options = list_obj.options.merge(format: TWEET_HEADINGS_FORMATTING)
  list_obj.timeline(user_list)
  members = fetch_list_members_v1(owner, list_name)
  user_ids = members.collect { |member| member["id"] }
  rule_ids = setup_stream_rules([{value: user_ids.map { |id| "from:#{id}" }.join(" OR ")}])
  stream_tweets("tweets/search/stream") { |tweet| print_stream_tweet(tweet) }
ensure
  remove_stream_rules(rule_ids || [])
end

#matrixObject



62
63
64
65
66
67
68
69
70
# File 'lib/t/stream.rb', line 62

def matrix
  rule_ids = setup_stream_rules([{value: "の lang:ja"}])
  stream_tweets("tweets/search/stream") do |tweet|
    text = (tweet["text"] || tweet["full_text"] || "").gsub(/[^\u3000\u3040-\u309f]/, "").reverse
    say(text, %i[bold green on_black], false) unless text.empty?
  end
ensure
  remove_stream_rules(rule_ids || [])
end

#search(keyword, *keywords) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/t/stream.rb', line 76

def search(keyword, *keywords)
  keywords.unshift(keyword)
  require "t/search"
  search_obj = T::Search.new
  search_obj.options = search_obj.options.merge(options)
  search_obj.options = search_obj.options.merge(reverse: true)
  search_obj.options = search_obj.options.merge(format: TWEET_HEADINGS_FORMATTING)
  search_obj.all(keywords.join(" OR "))
  rule_ids = setup_stream_rules([{value: keywords.join(" OR ")}])
  stream_tweets("tweets/search/stream") { |tweet| print_stream_tweet(tweet) }
ensure
  remove_stream_rules(rule_ids || [])
end

#timelineObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/t/stream.rb', line 94

def timeline
  require "t/cli"
  cli = T::CLI.new
  cli.options = cli.options.merge(options)
  cli.options = cli.options.merge(reverse: true)
  cli.options = cli.options.merge(format: TWEET_HEADINGS_FORMATTING)
  cli.timeline
  username = @rcfile.active_profile[0]
  rule_ids = setup_stream_rules([{value: "from:#{username} OR to:#{username}"}])
  stream_tweets("tweets/search/stream") { |tweet| print_stream_tweet(tweet) }
ensure
  remove_stream_rules(rule_ids || [])
end

#users(user_id, *user_ids) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/t/stream.rb', line 112

def users(user_id, *user_ids)
  user_ids.unshift(user_id)
  user_ids.collect!(&:to_i)
  print_stream_headings
  rule_ids = setup_stream_rules([{value: user_ids.map { |id| "from:#{id}" }.join(" OR ")}])
  stream_tweets("tweets/search/stream") { |tweet| print_stream_tweet(tweet) }
ensure
  remove_stream_rules(rule_ids || [])
end