Class: SocialFeedAgregator::FacebookReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FacebookReader

Returns a new instance of FacebookReader.



10
11
12
13
14
15
16
17
# File 'lib/social_feed_agregator/facebook_reader.rb', line 10

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

  @name =       options[:facebook_user_name]
  @app_id =     options[:facebook_app_id]
  @app_secret = options[:facebook_app_secret]      
end

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



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

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



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

def app_secret
  @app_secret
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#get_feeds(options = {}) ⇒ Object



19
20
21
22
23
24
25
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
# File 'lib/social_feed_agregator/facebook_reader.rb', line 19

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

  from_date = options[:from_date] || DateTime.new(1970,1,1) 
  
  feeds, i, count_per_request, items = [], 0, 25, 0

  parts = (count.to_f / count_per_request).ceil

  oauth = Koala::Facebook::OAuth.new(@app_id, @app_secret)      
  graph = Koala::Facebook::API.new oauth.get_app_access_token      
  posts = graph.get_connections(@name, "posts")

  begin
    i+=1
    posts.each do |post|    
      items+=1
      break if items > count

      # Break if the date is less
      if DateTime.parse(post['created_time']) <= from_date
        i = parts
        break
      end

      feed = fill_feed post

      block_given? ? yield(feed) : feeds << feed          
    end       
  end while (posts = posts.next_page) && (i < parts)
  feeds
end