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

Returns a new instance of Search.



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

def initialize(q=nil, o={})
  clear
  @user_agent = o[:user_agent] ? o[:user_agent] : "Twitter Ruby Gem"
  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



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

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

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



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

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

#eachObject



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

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

#fetch(force = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/twitter/search.rb', line 94

def fetch(force=false)
  if @fetch.nil? || force
    query = @query.dup
    query[:q] = query[:q].join(' ')
    response = self.class.get('http://search.twitter.com/search.json', {:query => query, :format => :json, :headers => {'user-agent' => @user_agent}})
    @fetch = Mash.new(response)
  end
  
  @fetch
end

#from(user) ⇒ Object



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

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.



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

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

#hashed(tag) ⇒ Object

adds filtering based on hash tag ie: #twitter



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

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



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

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

#max(id) ⇒ Object



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

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

#page(num) ⇒ Object

Which page of results to fetch



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

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

#per_page(num) ⇒ Object

Limits the number of results per page



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

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

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



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

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.



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

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

#to(user) ⇒ Object



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

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

#user_agent(agent = nil) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/twitter/search.rb', line 109

def user_agent(agent=nil)
  if agent.nil?
    @user_agent
  else
    @user_agent = agent
    self
  end
end