class SocialFeedAgregator::Facebook

Attributes

app_id[RW]
app_secret[RW]
name[RW]

Public Class Methods

new(options={}) click to toggle source
Calls superclass method BaseReader::new
# 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

Public Instance Methods

get_feeds(options={}) { |feed| ... } click to toggle source
Calls superclass method BaseReader#get_feeds
# 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

Private Instance Methods

fill_feed(post) click to toggle source
# File lib/social_feed_agregator/facebook_reader.rb, line 56
def fill_feed(post)
  Feed.new(
    feed_type: :facebook,
    feed_id: post['id'],                
    user_id: post['from']['id'],
    user_name: post['from']['name'],        
    permalink: "http://www.facebook.com/#{post['id'].gsub('_', '/posts/')}",
    description: post['description'],
    name: post['name'],
    picture_url: post['picture'],
    link: post['link'],
    caption: post['caption'],        
    message: post['message'],      
    created_at: DateTime.parse(post["created_time"]),
    type: post['type']
  )
end