Class: Twitter::Search

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HTTParty
Defined in:
lib/twitter/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(q = nil) ⇒ Search

Returns a new instance of Search.



12
13
14
15
# File 'lib/twitter/search.rb', line 12

def initialize(q=nil)
  clear
  containing(q) if q && q.strip != ''
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



10
11
12
# File 'lib/twitter/search.rb', line 10

def query
  @query
end

#resultObject (readonly)

Returns the value of attribute result.



10
11
12
# File 'lib/twitter/search.rb', line 10

def result
  @result
end

Instance Method Details

#clearObject

Clears all the query filters to make a new search



83
84
85
86
87
# File 'lib/twitter/search.rb', line 83

def clear
  @query = {}
  @query[:q] = []
  self
end

#containing(word) ⇒ Object Also known as: contains



34
35
36
37
# File 'lib/twitter/search.rb', line 34

def containing(word)
  @query[:q] << "#{word}"
  self
end

#eachObject



95
96
97
98
# File 'lib/twitter/search.rb', line 95

def each
  @result = fetch()
  @result['results'].each { |r| yield r }
end

#fetchObject

If you want to get results do something other than iterate over them.



90
91
92
93
# File 'lib/twitter/search.rb', line 90

def fetch
  @query[:q] = @query[:q].join(' ')
  SearchResultInfo.new_from_hash(self.class.get('/search.json', {:query => @query}))
end

#from(user) ⇒ Object



17
18
19
20
# File 'lib/twitter/search.rb', line 17

def from(user)
  @query[:q] << "from:#{user}"
  self
end

#geocode(long, lat, range) ⇒ Object

Search tweets by longitude, latitude and a given range. Ranges like 25km and 50mi work.



77
78
79
80
# File 'lib/twitter/search.rb', line 77

def geocode(long, lat, range)
  @query[:geocode] = [long, lat, range].join(',')
  self
end

#hashed(tag) ⇒ Object

adds filtering based on hash tag ie: #twitter



41
42
43
44
# File 'lib/twitter/search.rb', line 41

def hashed(tag)
  @query[:q] << "##{tag}"
  self
end

#lang(lang) ⇒ Object

lang must be ISO 639-1 code ie: en, fr, de, ja, etc.

when I tried en it limited my results a lot and took out several tweets that were english so i’d avoid this unless you really want it



51
52
53
54
# File 'lib/twitter/search.rb', line 51

def lang(lang)
  @query[:lang] = lang
  self
end

#page(num) ⇒ Object

Which page of results to fetch



63
64
65
66
# File 'lib/twitter/search.rb', line 63

def page(num)
  @query[:page] = num
  self
end

#per_page(num) ⇒ Object

Limits the number of results per page



57
58
59
60
# File 'lib/twitter/search.rb', line 57

def per_page(num)
  @query[:rpp] = num
  self
end

#referencing(user) ⇒ Object Also known as: references, ref



27
28
29
30
# File 'lib/twitter/search.rb', line 27

def referencing(user)
  @query[:q] << "@#{user}"
  self
end

#since(since_id) ⇒ Object

Only searches tweets since a given id. Recommended to use this when possible.



70
71
72
73
# File 'lib/twitter/search.rb', line 70

def since(since_id)
  @query[:since_id] = since_id
  self
end

#to(user) ⇒ Object



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

def to(user)
  @query[:q] << "to:#{user}"
  self
end