Class: T::CLI::Search

Inherits:
Thor
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, Pager, Requestable
Defined in:
lib/t/cli/search.rb

Constant Summary collapse

DEFAULT_NUM_RESULTS =
20
MAX_PAGES =
16
MAX_NUM_RESULTS =
200
MAX_SCREEN_NAME_SIZE =
20

Constants included from Requestable

Requestable::DEFAULT_HOST, Requestable::DEFAULT_PROTOCOL

Instance Method Summary collapse

Methods included from Requestable

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

Constructor Details

#initializeSearch

Returns a new instance of Search.



23
24
25
26
# File 'lib/t/cli/search.rb', line 23

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

Instance Method Details

#all(query) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/t/cli/search.rb', line 31

def all(query)
  defaults = {:include_entities => false}
  defaults.merge!(:rpp => options['number']) if options['number']
  timeline = client.search(query, defaults)
  timeline.reverse! if options['reverse']
  page unless T.env.test?
  timeline.each do |status|
    say "#{status.from_user.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
  end
end

#favorites(query) ⇒ Object



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

def favorites(query)
  timeline = 1.upto(MAX_PAGES).threaded_map do |page|
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.favorites(:page => page, :count => MAX_NUM_RESULTS).select do |status|
        /#{query}/i.match(status.text)
      end
    end
  end
  page unless T.env.test?
  timeline.flatten.compact.each do |status|
    say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
  end
end

#mentions(query) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/t/cli/search.rb', line 59

def mentions(query)
  timeline = 1.upto(MAX_PAGES).threaded_map do |page|
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.mentions(:page => page, :count => MAX_NUM_RESULTS).select do |status|
        /#{query}/i.match(status.text)
      end
    end
  end
  page unless T.env.test?
  timeline.flatten.compact.each do |status|
    say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
  end
end

#retweets(query) ⇒ Object



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

def retweets(query)
  timeline = 1.upto(MAX_PAGES).threaded_map do |page|
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.retweeted_by(:page => page, :count => MAX_NUM_RESULTS).select do |status|
        /#{query}/i.match(status.text)
      end
    end
  end
  page unless T.env.test?
  timeline.flatten.compact.each do |status|
    say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
  end
end

#timeline(query) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/t/cli/search.rb', line 91

def timeline(query)
  timeline = 1.upto(MAX_PAGES).threaded_map do |page|
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.home_timeline(:page => page, :count => MAX_NUM_RESULTS).select do |status|
        /#{query}/i.match(status.text)
      end
    end
  end
  page unless T.env.test?
  timeline.flatten.compact.each do |status|
    say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
  end
end

#user(screen_name, query) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/t/cli/search.rb', line 107

def user(screen_name, query)
  screen_name = screen_name.strip_at
  timeline = 1.upto(MAX_PAGES).threaded_map do |page|
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.user_timeline(screen_name, :page => page, :count => MAX_NUM_RESULTS).select do |status|
        /#{query}/i.match(status.text)
      end
    end
  end
  page unless T.env.test?
  timeline.flatten.compact.each do |status|
    say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
  end
end