Class: T::Search

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

Constant Summary collapse

DEFAULT_NUM_RESULTS =
20
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

#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

#initializeSearch

Returns a new instance of Search.



26
27
28
29
# File 'lib/t/search.rb', line 26

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

Instance Method Details

#all(query) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/t/search.rb', line 35

def all(query)
  rpp = options['number'] || DEFAULT_NUM_RESULTS
  statuses = client.search(query, :include_entities => false, :rpp => rpp)
  if options['csv']
    say ["ID", "Posted at", "Screen name", "Text"].to_csv unless statuses.empty?
    statuses.each do |status|
      say [status.id, status.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), status.from_user, HTMLEntities.new.decode(status.text)].to_csv
    end
  elsif 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")
      [status.id, created_at, "@#{status.from_user}", HTMLEntities.new.decode(status.text).gsub(/\n+/, ' ')]
    end
    if STDOUT.tty?
      headings = ["ID", "Posted at", "Screen name", "Text"]
      array.unshift(headings) unless statuses.empty?
      print_table(array, :truncate => true)
    else
      print_table(array)
    end
  else
    say unless statuses.empty?
    statuses.each do |status|
      if STDOUT.tty? && !options['no-color']
        say("   #{Thor::Shell::Color::BOLD}@#{status.from_user}", :yellow)
        print_wrapped(HTMLEntities.new.decode(status.text), :indent => 3)
      else
        say("   @#{status.from_user}")
        print_wrapped(HTMLEntities.new.decode(status.text), :indent => 3)
      end
      say
    end
  end
end

#favorites(query) ⇒ Object



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

def favorites(query)
  opts = {:count => MAX_NUM_RESULTS}
  statuses = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.favorites(opts)
    end
  end.flatten.compact
  statuses = statuses.select do |status|
    /#{query}/i.match(status.text)
  end
  print_statuses(statuses)
end

#list(list, query) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/t/search.rb', line 92

def list(list, query)
  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
  opts = {:count => MAX_NUM_RESULTS}
  statuses = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.list_timeline(owner, list, opts)
    end
  end.flatten.compact
  statuses = statuses.select do |status|
    /#{query}/i.match(status.text)
  end
  print_statuses(statuses)
end

#mentions(query) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/t/search.rb', line 120

def mentions(query)
  opts = {:count => MAX_NUM_RESULTS}
  statuses = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.mentions(opts)
    end
  end.flatten.compact
  statuses = statuses.select do |status|
    /#{query}/i.match(status.text)
  end
  print_statuses(statuses)
end

#retweets(query) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/t/search.rb', line 138

def retweets(query)
  opts = {:count => MAX_NUM_RESULTS}
  statuses = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.retweeted_by(opts)
    end
  end.flatten.compact
  statuses = statuses.select do |status|
    /#{query}/i.match(status.text)
  end
  print_statuses(statuses)
end

#timeline(query) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/t/search.rb', line 156

def timeline(query)
  opts = {:count => MAX_NUM_RESULTS}
  statuses = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.home_timeline(opts)
    end
  end.flatten.compact
  statuses = statuses.select do |status|
    /#{query}/i.match(status.text)
  end
  print_statuses(statuses)
end

#user(user, query) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/t/search.rb', line 175

def user(user, query)
  user = if options['id']
    user.to_i
  else
    user.strip_ats
  end
  opts = {:count => MAX_NUM_RESULTS}
  statuses = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
      client.user_timeline(user, opts)
    end
  end.flatten.compact
  statuses = statuses.select do |status|
    /#{query}/i.match(status.text)
  end
  print_statuses(statuses)
end