Class: SocialProfile::People::Twitter

Inherits:
SocialProfile::Person show all
Defined in:
lib/social_profile/people/twitter.rb

Constant Summary collapse

MAX_ATTEMPTS =
3

Instance Attribute Summary

Attributes inherited from SocialProfile::Person

#access_token, #options, #uid

Instance Method Summary collapse

Methods inherited from SocialProfile::Person

#album!, #fetch_album, #find_album, #find_or_create_album, #followers_count, get, #initialize, #share_photo!, #tag_object!

Constructor Details

This class inherits a constructor from SocialProfile::Person

Instance Method Details

#collect_with_max_id(collection = [], max_id = nil, &block) ⇒ Object



41
42
43
44
45
# File 'lib/social_profile/people/twitter.rb', line 41

def collect_with_max_id(collection=[], max_id=nil, &block)
  response = yield(max_id)
  collection += response
  response.empty? ? collection.flatten : collect_with_max_id(collection, response.last.id - 1, &block)
end

#fetch_friends_countObject



13
14
15
# File 'lib/social_profile/people/twitter.rb', line 13

def fetch_friends_count
  client.user.followers_count
end

#friends_countObject

Get friends count



9
10
11
# File 'lib/social_profile/people/twitter.rb', line 9

def friends_count
  @friends_count ||= fetch_friends_count
end

#get_all_tweets(uid, options = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/social_profile/people/twitter.rb', line 34

def get_all_tweets(uid, options = {})
  collect_with_max_id do |max_id|
    options[:max_id] = max_id unless max_id.nil?
    last_posts(uid, options)
  end
end

#last_posts(uid, options = {}) ⇒ Object

Get last limited tweets from user_timeline, max 200 by query



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/social_profile/people/twitter.rb', line 19

def last_posts(uid, options = {})
  params = { 
    :count => 200, 
    :exclude_replies => true, 
    :trim_user => true,
    :include_rts => false
  }

  params.merge!(options)

  with_atterms do 
    client.user_timeline(uid, params)  
  end     
end

#with_attermsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/social_profile/people/twitter.rb', line 47

def with_atterms
  num_attempts = 0

  begin
    num_attempts += 1
    yield if block_given?
  rescue ::Twitter::Error::TooManyRequests => error
    if num_attempts <= MAX_ATTEMPTS
      # NOTE: Your process could go to sleep for up to 15 minutes but if you
      # retry any sooner, it will almost certainly fail with the same exception.
      sleep error.rate_limit.reset_in
      retry
    else
      raise
    end
  end
      
end