public
Description: API wrapper for Twitter and Twitter Search API's
Homepage: http://twitter.rubyforge.org/
Clone URL: git://github.com/jnunemaker/twitter.git
Click here to lend your support to: twitter and make a donation at www.pledgie.com !
twitter / lib / twitter / search.rb
100644 111 lines (93 sloc) 2.408 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module Twitter
  class Search
    include HTTParty
    include Enumerable
    
    attr_reader :result, :query
    
    def initialize(q=nil, options={})
      @options = options
      clear
      containing(q) if q && q.strip != ''
    end
    
    def user_agent
      @options[:user_agent] || 'Ruby Twitter Gem'
    end
    
    def from(user)
      @query[:q] << "from:#{user}"
      self
    end
    
    def to(user)
      @query[:q] << "to:#{user}"
      self
    end
    
    def referencing(user)
      @query[:q] << "@#{user}"
      self
    end
    alias :references :referencing
    alias :ref :referencing
    
    def containing(word)
      @query[:q] << "#{word}"
      self
    end
    alias :contains :containing
    
    # adds filtering based on hash tag ie: #twitter
    def hashed(tag)
      @query[:q] << "##{tag}"
      self
    end
    
    # 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
    def lang(lang)
      @query[:lang] = lang
      self
    end
    
    # Limits the number of results per page
    def per_page(num)
      @query[:rpp] = num
      self
    end
    
    # Which page of results to fetch
    def page(num)
      @query[:page] = num
      self
    end
    
    # Only searches tweets since a given id.
    # Recommended to use this when possible.
    def since(since_id)
      @query[:since_id] = since_id
      self
    end
    
    # Search tweets by latitude, longitude, and a given range.
    # Ranges like 25km and 50mi work.
    def geocode(lat, long, range)
      @query[:geocode] = [lat, long, range].join(',')
      self
    end
    
    def max(id)
      @query[:max_id] = id
      self
    end
    
    # Clears all the query filters to make a new search
    def clear
      @fetch = nil
      @query = {}
      @query[:q] = []
      self
    end
    
    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
    
    def each
      fetch()['results'].each { |r| yield r }
    end
  end
end