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.



11
12
13
14
15
16
17
18
# File 'lib/twitter/search.rb', line 11

def initialize(q=nil, options={})
  @options = options
  clear
  containing(q) if q && q.strip != ""
  endpoint_url = options[:api_endpoint]
  endpoint_url = "#{endpoint_url}/search" if endpoint_url && !endpoint_url.include?("/search")
  self.class.base_uri(endpoint_url) if endpoint_url
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

Instance Method Details

#clearObject

Clears all the query filters to make a new search



120
121
122
123
124
125
# File 'lib/twitter/search.rb', line 120

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

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



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

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

#eachObject



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

def each
  results = fetch()['results']
  return if results.nil?
  results.each {|r| yield r}
end

#fetch(force = false) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/twitter/search.rb', line 127

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



147
148
149
150
151
152
153
# File 'lib/twitter/search.rb', line 147

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



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

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

#geocode(lat, long, range) ⇒ Object

Ranges like 25km and 50mi work.



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

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



48
49
50
51
# File 'lib/twitter/search.rb', line 48

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



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

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

#max(id) ⇒ Object



114
115
116
117
# File 'lib/twitter/search.rb', line 114

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

#next_page?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/twitter/search.rb', line 143

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

#page(num) ⇒ Object

Which page of results to fetch



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

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

#per_page(num) ⇒ Object

Limits the number of results per page



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

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

#phrase(phrase) ⇒ Object

Search for a phrase instead of a group of words



54
55
56
57
# File 'lib/twitter/search.rb', line 54

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

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



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

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

#result_type(result_type) ⇒ Object

popular|recent



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

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.



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

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



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

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

#to(user, exclude = false) ⇒ Object



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

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



103
104
105
106
# File 'lib/twitter/search.rb', line 103

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

#user_agentObject



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

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