Class: T::Search

Inherits:
Thor
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, Printable, Requestable
Defined in:
lib/t/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

Methods included from Printable

included, #print_in_columns, #print_status_list, #print_user_list

Constructor Details

#initializeSearch

Returns a new instance of Search.



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

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

Instance Method Details

#all(query) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/t/search.rb', line 29

def all(query)
  rpp = options['number'] || DEFAULT_NUM_RESULTS
  statuses = client.search(query, :include_entities => false, :rpp => rpp)
  if options['long']
    array = statuses.map do |status|
      created_at = status.created_at > 6.months.ago ? status.created_at.strftime("%b %e %H:%M") : status.created_at.strftime("%b %e  %Y")
      [number_with_delimiter(status.id), created_at, "@#{status.from_user}", status.text.gsub(/\n+/, ' ')]
    end
    if STDOUT.tty?
      headings = ["ID", "Posted at", "Screen name", "Text"]
      array.unshift(headings) unless statuses.empty?
    end
    print_table(array)
  else
    statuses.each do |status|
      say "#{status.from_user.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text.gsub(/\n+/, ' ')} (#{time_ago_in_words(status.created_at)} ago)"
    end
  end
end

#favorites(query) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/t/search.rb', line 50

def favorites(query)
  statuses = 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.flatten.compact
  print_status_list(statuses)
end

#mentions(query) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/t/search.rb', line 63

def mentions(query)
  statuses = 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.flatten.compact
  print_status_list(statuses)
end

#retweets(query) ⇒ Object



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

def retweets(query)
  statuses = 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.flatten.compact
  print_status_list(statuses)
end

#timeline(query) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/t/search.rb', line 89

def timeline(query)
  statuses = 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.flatten.compact
  print_status_list(statuses)
end

#user(screen_name, query) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/t/search.rb', line 102

def user(screen_name, query)
  screen_name = screen_name.strip_ats
  statuses = 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.flatten.compact
  print_status_list(statuses)
end