Class: TwitterObject
- Inherits:
-
Object
show all
- Defined in:
- lib/userstream/twitter_object.rb
Constant Summary
collapse
- SINGLE_USER_URL =
"http://api.twitter.com/1/users/show/%s.json"
"http://api.twitter.com/1/statuses/show/%s.json"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(base, data = nil) {|_self| ... } ⇒ TwitterObject
Returns a new instance of TwitterObject.
8
9
10
11
12
|
# File 'lib/userstream/twitter_object.rb', line 8
def initialize(base, data = nil)
@base, @data = base, data
from_json(data) if data
yield self if block_given?
end
|
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
6
7
8
|
# File 'lib/userstream/twitter_object.rb', line 6
def base
@base
end
|
#data ⇒ Object
Returns the value of attribute data.
6
7
8
|
# File 'lib/userstream/twitter_object.rb', line 6
def data
@data
end
|
Class Method Details
.attrs ⇒ Object
46
47
48
|
# File 'lib/userstream/twitter_object.rb', line 46
def self.attrs
const_get(:ATTRS)
end
|
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/userstream/twitter_object.rb', line 28
def self.(*attrs)
attrs.each do |attr|
module_eval "
def #{attr}=(#{attr})
@#{attr} = if #{attr}.is_a?(Hash)
Userstream::Tweet.new(base, #{attr})
else
Userstream::Tweet.new(base) {|t| t.id = #{attr}}
end
end
"
end
end
|
.user_writer(*attrs) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/userstream/twitter_object.rb', line 14
def self.user_writer(*attrs)
attrs.each do |attr|
module_eval "
def #{attr}=(#{attr})
@#{attr} = if #{attr}.is_a?(Hash)
Userstream::User.new(base, #{attr})
else
Userstream::User.new(base) {|u| u.id = #{attr}}
end
end
"
end
end
|
Instance Method Details
#from_json(data) ⇒ Object
42
43
44
|
# File 'lib/userstream/twitter_object.rb', line 42
def from_json(data)
self.class.attrs.each { |a| self.send(:"#{a}=", data[a.to_s]) }
end
|
84
85
86
87
88
89
90
91
92
|
# File 'lib/userstream/twitter_object.rb', line 84
def (loading_user, ids)
ids = Array(ids).uniq
data = {}
if ids.empty?
yield data
else
(loading_user, ids, data) { yield data }
end
end
|
#get_user_data(loading_user, ids) ⇒ Object
111
112
113
114
115
116
117
118
119
|
# File 'lib/userstream/twitter_object.rb', line 111
def get_user_data(loading_user, ids)
ids = Array(ids).uniq
data = {}
if ids.empty?
yield data
else
load_user_data(loading_user, ids, data) { yield data }
end
end
|
#load_all(loading_user, &block) ⇒ Object
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
|
# File 'lib/userstream/twitter_object.rb', line 50
def load_all(loading_user, &block)
attrs = self.class.attrs
if respond_to?(:loaded?) && !loaded?
if respond_to?(:user_loadable_id)
from_json(get_user_data(loading_user, user_loadable_id)[user_loadable_id])
load_all(&block)
elsif respond_to?(:tweet_loadable_id)
from_json((loading_user, )[])
load_all(&block)
end
else
= {}
user_ids = {}
attrs.each do |a|
obj = send(a)
if obj.respond_to?(:loaded?) && !obj.loaded?
if obj.respond_to?(:user_loadable_id)
user_ids[a] = obj.user_loadable_id
elsif obj.respond_to?(:tweet_loadable_id)
[a] = obj.
end
end
end
(loading_user, .values) { ||
.each{|k,v| self.send(:"#{k}=", [v])}
user_data = get_user_data(loading_user, user_ids.values) { |user_data|
user_ids.each{|k,v| self.send(:"#{k}=", user_data[v])}
yield self
}
}
end
end
|
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/userstream/twitter_object.rb', line 94
def (loading_user, ids, data, &block)
id = ids.shift
if (id)
parser = Yajl::Parser.new
parser.on_parse_complete = proc { |parsed|
data[id] = parsed
(ids, data, &block)
}
http = base.get_connection(loading_user, "http://api.twitter.com/1/statuses/show/%s.json" % id, :get)
http.stream { |chunk|
parser << chunk
}
else
yield
end
end
|
#load_user_data(loading_user, ids, data) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/userstream/twitter_object.rb', line 121
def load_user_data(loading_user, ids, data)
parser = Yajl::Parser.new
parser.on_parse_complete = proc { |parsed|
parsed.each do |user|
data[user["id"]] = user
end
yield
}
http = base.get_connection(loading_user, "http://api.twitter.com/1/users/lookup.json", :post, :body => {'user_id' => ids.join(',')})
http.stream { |chunk|
parser << chunk
}
end
|