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



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

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



100
101
102
103
# File 'lib/twitter/search.rb', line 100

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.



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

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

#filter(filter) ⇒ Object



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

def filter(filter)
  @query[:q] << "filter:#{filter}"
  self
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.



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

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

#hashed(tag) ⇒ Object

adds filtering based on hash tag ie: #twitter



46
47
48
49
# File 'lib/twitter/search.rb', line 46

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



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

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

#page(num) ⇒ Object

Which page of results to fetch



68
69
70
71
# File 'lib/twitter/search.rb', line 68

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

#per_page(num) ⇒ Object

Limits the number of results per page



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

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.



75
76
77
78
# File 'lib/twitter/search.rb', line 75

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