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
15
# File 'lib/twitter/search.rb', line 10

def initialize(q=nil, options={})
  @options = options
  clear
  containing(q) if q && q.strip != ""
  self.class.base_uri(options[:api_endpoint]) if options[:api_endpoint]
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



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

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

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



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

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

#eachObject



134
135
136
137
138
# File 'lib/twitter/search.rb', line 134

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

#fetch(force = false) ⇒ Object



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

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



144
145
146
147
148
149
150
# File 'lib/twitter/search.rb', line 144

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



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

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

#geocode(lat, long, range) ⇒ Object

Ranges like 25km and 50mi work.



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

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



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

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



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

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

#max(id) ⇒ Object



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

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

#next_page?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/twitter/search.rb', line 140

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

#page(num) ⇒ Object

Which page of results to fetch



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

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

#per_page(num) ⇒ Object

Limits the number of results per page



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

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

#phrase(phrase) ⇒ Object

Search for a phrase instead of a group of words



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

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

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



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

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

#result_type(result_type) ⇒ Object

popular|recent



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

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.



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

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



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

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

#to(user, exclude = false) ⇒ Object



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

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



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

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

#user_agentObject



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

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