nakajima-twitter_oauth 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/twitter_oauth/search.rb +28 -4
- metadata +1 -1
data/lib/twitter_oauth/search.rb
CHANGED
@@ -3,16 +3,40 @@ require 'ostruct'
|
|
3
3
|
|
4
4
|
module TwitterOAuth
|
5
5
|
class Client
|
6
|
-
|
6
|
+
|
7
7
|
def search(q, options={})
|
8
8
|
options[:page] ||= 1
|
9
9
|
options[:per_page] ||= 20
|
10
10
|
response = open("http://search.twitter.com/search.json?q=#{URI.escape(q)}&page=#{options[:page]}&rpp=#{options[:per_page]}&since_id=#{options[:since_id]}")
|
11
11
|
search_result = JSON.parse(response.read)
|
12
12
|
search_result = OpenStruct.new(search_result)
|
13
|
-
|
13
|
+
|
14
|
+
klass = make_klass(search_result.results.first)
|
15
|
+
|
16
|
+
search_result.results = search_result.results.collect do |res|
|
17
|
+
klass.new(res)
|
18
|
+
end
|
19
|
+
|
14
20
|
search_result
|
15
21
|
end
|
16
|
-
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# Returns an OpenStruct-esque class that doesn't whine about calling #id
|
26
|
+
def make_klass(result)
|
27
|
+
keys = result.keys.map(&:to_sym)
|
28
|
+
Class.new do
|
29
|
+
attr_accessor *keys
|
30
|
+
|
31
|
+
def initialize(hash)
|
32
|
+
hash.each { |key,val| send("#{key}=", val) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_missing(*args)
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
17
41
|
end
|
18
|
-
end
|
42
|
+
end
|