jnunemaker / twitter

API wrapper for Twitter and Twitter Search API's

This URL has Read+Write access

twitter / lib / twitter / search.rb
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 1 module Twitter
2 class Search
3 include HTTParty
4 include Enumerable
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 5
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 6 attr_reader :result, :query
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 7
e8fbad6a » jnunemaker 2009-06-26 Added user agent option to ... 8 def initialize(q=nil, options={})
9 @options = options
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 10 clear
11 containing(q) if q && q.strip != ''
12 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 13
e8fbad6a » jnunemaker 2009-06-26 Added user agent option to ... 14 def user_agent
15 @options[:user_agent] || 'Ruby Twitter Gem'
16 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 17
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 18 def from(user)
19 @query[:q] << "from:#{user}"
20 self
21 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 22
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 23 def to(user)
24 @query[:q] << "to:#{user}"
25 self
26 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 27
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 28 def referencing(user)
29 @query[:q] << "@#{user}"
30 self
31 end
32 alias :references :referencing
33 alias :ref :referencing
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 34
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 35 def containing(word)
36 @query[:q] << "#{word}"
37 self
38 end
39 alias :contains :containing
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 40
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 41 # adds filtering based on hash tag ie: #twitter
42 def hashed(tag)
43 @query[:q] << "##{tag}"
44 self
45 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 46
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 47 # lang must be ISO 639-1 code ie: en, fr, de, ja, etc.
48 #
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 49 # when I tried en it limited my results a lot and took
50 # out several tweets that were english so i'd avoid
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 51 # this unless you really want it
52 def lang(lang)
53 @query[:lang] = lang
54 self
55 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 56
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 57 # Limits the number of results per page
58 def per_page(num)
59 @query[:rpp] = num
60 self
61 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 62
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 63 # Which page of results to fetch
64 def page(num)
65 @query[:page] = num
66 self
67 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 68
69 # Only searches tweets since a given id.
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 70 # Recommended to use this when possible.
71 def since(since_id)
72 @query[:since_id] = since_id
73 self
74 end
75
9dcd3408 » ch0wda 2009-05-04 adding since_date/until_dat... 76 # From the advanced search form, not documented in the API
77 # Format YYYY-MM-DD
78 def since_date(since_date)
79 @query[:since] = since_date
80 self
81 end
82
83 # From the advanced search form, not documented in the API
84 # Format YYYY-MM-DD
85 def until_date(until_date)
86 @query[:until] = until_date
87 self
88 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 89
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 90 # Ranges like 25km and 50mi work.
43eaaca2 » Toby Matejovsky 2009-10-05 Corrected order of "lat" an... 91 def geocode(lat, long, range)
92 @query[:geocode] = [lat, long, range].join(',')
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 93 self
94 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 95
e79cc1fd » jnunemaker 2009-04-14 Added max to search to spec... 96 def max(id)
97 @query[:max_id] = id
98 self
99 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 100
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 101 # Clears all the query filters to make a new search
102 def clear
103 @fetch = nil
104 @query = {}
105 @query[:q] = []
106 self
107 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 108
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 109 def fetch(force=false)
110 if @fetch.nil? || force
096d56ed » jnunemaker 2009-04-08 Fixed that search was ignor... Comment 111 query = @query.dup
112 query[:q] = query[:q].join(' ')
e8fbad6a » jnunemaker 2009-06-26 Added user agent option to ... 113 response = self.class.get('http://search.twitter.com/search.json', :query => query, :format => :json, :headers => {'User-Agent' => user_agent})
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 114 @fetch = Hashie::Mash.new(response)
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 115 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 116
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 117 @fetch
118 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 119
9c329cfc » jnunemaker 2009-04-03 Added search api wrapper an... 120 def each
121 fetch()['results'].each { |r| yield r }
122 end
123 end
365f8378 » hassox 2009-11-12 Changes the Mash gem for th... Comment 124 end