nakajima-twitter_oauth 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/twitter_oauth/search.rb +28 -4
  2. metadata +1 -1
@@ -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
- search_result.results = search_result.results.collect{|x| OpenStruct.new(x)}
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakajima-twitter_oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Taylor