Class: SocialFeedAgregator::TwitterReader

Inherits:
BaseReader
  • Object
show all
Defined in:
lib/social_feed_agregator/twitter_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TwitterReader

Returns a new instance of TwitterReader.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/social_feed_agregator/twitter_reader.rb', line 14

def initialize(options={})
  super(options)
  options.replace(SocialFeedAgregator.default_options.merge(options))

  @consumer_key = options[:twitter_consumer_key]
  @consumer_secret = options[:twitter_consumer_secret]
  @oauth_token = options[:twitter_oauth_token]
  @token_secret = options[:twitter_oauth_token_secret]
  @name = options[:twitter_user_name]

end

Instance Attribute Details

#consumer_keyObject

Returns the value of attribute consumer_key.



8
9
10
# File 'lib/social_feed_agregator/twitter_reader.rb', line 8

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



8
9
10
# File 'lib/social_feed_agregator/twitter_reader.rb', line 8

def consumer_secret
  @consumer_secret
end

#twitter_nameObject

Returns the value of attribute twitter_name.



8
9
10
# File 'lib/social_feed_agregator/twitter_reader.rb', line 8

def twitter_name
  @twitter_name
end

#twitter_oauth_tokenObject

Returns the value of attribute twitter_oauth_token.



8
9
10
# File 'lib/social_feed_agregator/twitter_reader.rb', line 8

def twitter_oauth_token
  @twitter_oauth_token
end

#twitter_oauth_token_secretObject

Returns the value of attribute twitter_oauth_token_secret.



8
9
10
# File 'lib/social_feed_agregator/twitter_reader.rb', line 8

def twitter_oauth_token_secret
  @twitter_oauth_token_secret
end

Instance Method Details

#get_feeds(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/social_feed_agregator/twitter_reader.rb', line 26

def get_feeds(options={})
  super(options)
  @name = options[:name] if options[:name]
  count = options[:count] || 100

  from_date = options[:from_date] || DateTime.new(1970,1,1) 
  
  client = ::Twitter.configure do |config|
    config.consumer_key = @consumer_key
    config.consumer_secret = @consumer_secret
    config.oauth_token = @oauth_token
    config.oauth_token_secret = @token_secret
  end
              
  feeds, i = [], 0
  count_per_request =  200 #::Twitter::REST::API::Timelines::MAX_TWEETS_PER_REQUEST      

  opts = {count: count < count_per_request ? count : count_per_request}

  parts = (count.to_f / count_per_request).ceil

  while (statuses = client.user_timeline(@name, opts)) && i < parts do
    i+=1                

    statuses.each do |status|                
      
      # Break if the date is less
      if status.created_at <= from_date
        i = parts
        break
      end
      
      feed = fill_feed status
      
      block_given? ? yield(feed) : feeds << feed     
      
      new_count = count - count_per_request * i
      opts[:count] = new_count < count_per_request ? new_count : count_per_request
      opts[:max_id] = status.id.to_s          
    end       
  end
  feeds
end