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) ⇒ Search

Returns a new instance of Search.



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

def initialize(q=nil)
  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



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

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

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



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

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

#eachObject



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

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

#fetch(force = false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/twitter/search.rb', line 91

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)
    @fetch = Mhash.new(response)
  end
  
  @fetch
end

#from(user) ⇒ Object



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

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.



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

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

#hashed(tag) ⇒ Object

adds filtering based on hash tag ie: #twitter



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

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



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

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

#max(id) ⇒ Object



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

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

#page(num) ⇒ Object

Which page of results to fetch



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

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

#per_page(num) ⇒ Object

Limits the number of results per page



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

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

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



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

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.



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

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

#to(user) ⇒ Object



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

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