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, options = {}) ⇒ Search

Returns a new instance of Search.



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

def initialize(q=nil, options={})
  @options = options
  clear
  containing(q) if q && q.strip != ""
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



8
9
10
# File 'lib/twitter/search.rb', line 8

def query
  @query
end

#resultObject (readonly)

Returns the value of attribute result.



8
9
10
# File 'lib/twitter/search.rb', line 8

def result
  @result
end

Instance Method Details

#clearObject

Clears all the query filters to make a new search



116
117
118
119
120
121
# File 'lib/twitter/search.rb', line 116

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

#containing(word, exclude = false) ⇒ Object Also known as: contains



37
38
39
40
# File 'lib/twitter/search.rb', line 37

def containing(word, exclude=false)
  @query[:q] << "#{exclude ? "-" : ""}#{word}"
  self
end

#eachObject



133
134
135
# File 'lib/twitter/search.rb', line 133

def each
  fetch()["results"].each{|r| yield r}
end

#fetch(force = false) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/twitter/search.rb', line 123

def fetch(force=false)
  if @fetch.nil? || force
    query = @query.dup
    query[:q] = query[:q].join(" ")
    perform_get(query)
  end

  @fetch
end

#fetch_next_pageObject



141
142
143
144
145
146
147
# File 'lib/twitter/search.rb', line 141

def fetch_next_page
  if next_page?
    s = Search.new(nil, :user_agent => user_agent)
    s.perform_get(fetch()["next_page"][1..-1])
    s
  end
end

#from(user, exclude = false) ⇒ Object



20
21
22
23
# File 'lib/twitter/search.rb', line 20

def from(user, exclude=false)
  @query[:q] << "#{exclude ? "-" : ""}from:#{user}"
  self
end

#geocode(lat, long, range) ⇒ Object

Ranges like 25km and 50mi work.



105
106
107
108
# File 'lib/twitter/search.rb', line 105

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

#hashed(tag, exclude = false) ⇒ Object

adds filtering based on hash tag ie: #twitter



44
45
46
47
# File 'lib/twitter/search.rb', line 44

def hashed(tag, exclude=false)
  @query[:q] << "#{exclude ? "-" : ""}\##{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



60
61
62
63
# File 'lib/twitter/search.rb', line 60

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

#max(id) ⇒ Object



110
111
112
113
# File 'lib/twitter/search.rb', line 110

def max(id)
  @query[:max_id] = id
  self
end

#next_page?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/twitter/search.rb', line 137

def next_page?
  !!fetch()["next_page"]
end

#page(num) ⇒ Object

Which page of results to fetch



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

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

#per_page(num) ⇒ Object

Limits the number of results per page



72
73
74
75
# File 'lib/twitter/search.rb', line 72

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

#phrase(phrase) ⇒ Object

Search for a phrase instead of a group of words



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

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

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



30
31
32
33
# File 'lib/twitter/search.rb', line 30

def referencing(user, exclude=false)
  @query[:q] << "#{exclude ? "-" : ""}@#{user}"
  self
end

#result_type(result_type) ⇒ Object

popular|recent



66
67
68
69
# File 'lib/twitter/search.rb', line 66

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

#since(since_id) ⇒ Object

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



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

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

#since_date(since_date) ⇒ Object

From the advanced search form, not documented in the API Format YYYY-MM-DD



92
93
94
95
# File 'lib/twitter/search.rb', line 92

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

#to(user, exclude = false) ⇒ Object



25
26
27
28
# File 'lib/twitter/search.rb', line 25

def to(user, exclude=false)
  @query[:q] << "#{exclude ? "-" : ""}to:#{user}"
  self
end

#until_date(until_date) ⇒ Object

From the advanced search form, not documented in the API Format YYYY-MM-DD



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

def until_date(until_date)
  @query[:until] = until_date
  self
end

#user_agentObject



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

def user_agent
  @options[:user_agent] || "Ruby Twitter Gem"
end