Class: TwitterObject

Inherits:
Object
  • Object
show all
Defined in:
lib/chirpstream/twitter_object.rb

Constant Summary collapse

SINGLE_USER_URL =
"http://api.twitter.com/1/users/show/%s.json"
SINGLE_TWEET_URL =
"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.

Yields:

  • (_self)

Yield Parameters:

  • _self (TwitterObject)

    the object that the method was called on



8
9
10
11
12
# File 'lib/chirpstream/twitter_object.rb', line 8

def initialize(base, data = nil)
  @base = base
  from_json(data) if data
  yield self if block_given?
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



6
7
8
# File 'lib/chirpstream/twitter_object.rb', line 6

def base
  @base
end

Class Method Details

.attrsObject



46
47
48
# File 'lib/chirpstream/twitter_object.rb', line 46

def self.attrs
  const_get(:ATTRS)
end

.tweet_writer(*attrs) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chirpstream/twitter_object.rb', line 28

def self.tweet_writer(*attrs)
  attrs.each do |attr|
    module_eval "
    def #{attr}=(#{attr})
      @#{attr} = if #{attr}.is_a?(Hash)
        Chirpstream::Tweet.new(base, #{attr})
      else
        Chirpstream::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/chirpstream/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)
        Chirpstream::User.new(base, #{attr})
      else
        Chirpstream::User.new(base) {|u| u.id = #{attr}}
      end
    end
    "
  end
end

Instance Method Details

#from_json(data) ⇒ Object



42
43
44
# File 'lib/chirpstream/twitter_object.rb', line 42

def from_json(data)
  self.class.attrs.each { |a| self.send(:"#{a}=", data[a.to_s]) }
end

#get_tweet_data(loading_user, ids) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/chirpstream/twitter_object.rb', line 84

def get_tweet_data(loading_user, ids)
  ids = Array(ids).uniq
  data = {}
  if ids.empty?
    yield data
  else
    load_tweet_data(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/chirpstream/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/chirpstream/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(get_tweet_data(loading_user, tweet_loadable_id)[tweet_loadable_id])
      load_all(&block)
    end
  else
    tweet_ids = {}
    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)
          tweet_ids[a] = obj.tweet_loadable_id
        end
      end
    end
    get_tweet_data(loading_user, tweet_ids.values) { |tweet_data|
      tweet_ids.each{|k,v| self.send(:"#{k}=", tweet_data[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

#load_tweet_data(loading_user, ids, data, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chirpstream/twitter_object.rb', line 94

def load_tweet_data(loading_user, ids, data, &block)
  id = ids.shift
  if (id)
    parser = Yajl::Parser.new
    parser.on_parse_complete = proc { |parsed|
      data[id] = parsed
      load_tweet_data(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/chirpstream/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