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.



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

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.



6
7
8
# File 'lib/twitter/search.rb', line 6

def query
  @query
end

#resultObject (readonly)

Returns the value of attribute result.



6
7
8
# File 'lib/twitter/search.rb', line 6

def result
  @result
end

Instance Method Details

#clearObject

Clears all the query filters to make a new search



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

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

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



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

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

#eachObject



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

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

#fetch(force = false) ⇒ Object



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

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



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

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

#geocode(lat, long, range) ⇒ Object

Search tweets by latitude, longitude, and a given range. Ranges like 25km and 50mi work.



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

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

#hashed(tag) ⇒ Object

adds filtering based on hash tag ie: #twitter



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

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



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

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

#max(id) ⇒ Object



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

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

#page(num) ⇒ Object

Which page of results to fetch



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

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

#per_page(num) ⇒ Object

Limits the number of results per page



58
59
60
61
# File 'lib/twitter/search.rb', line 58

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

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



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

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.



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

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

#to(user) ⇒ Object



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

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

#user_agentObject



14
15
16
# File 'lib/twitter/search.rb', line 14

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