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, agent = "Twitter Ruby Gem") ⇒ Search

Returns a new instance of Search.



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

def initialize(q=nil, agent="Twitter Ruby Gem")
  clear
  @user_agent = agent
  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

#user_agentObject (readonly)

Returns the value of attribute user_agent.



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

def user_agent
  @user_agent
end

Instance Method Details

#clearObject

Clears all the query filters to make a new search



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

def clear
  @fetch = nil
  @query = {}
  @query[:q] = []
  @user_agent = "Twitter Ruby Gem"
  self
end

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



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

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

#eachObject



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

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

#fetch(force = false) ⇒ Object



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

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



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

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.



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

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

#hashed(tag) ⇒ Object

adds filtering based on hash tag ie: #twitter



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

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



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

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

#max(id) ⇒ Object



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

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

#page(num) ⇒ Object

Which page of results to fetch



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

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

#per_page(num) ⇒ Object

Limits the number of results per page



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

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

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



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

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.



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

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

#to(user) ⇒ Object



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

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